关于如何使用PHP表单更新Mysql数据库的好教程? [英] Good tutorial on how to update your Mysql database with a PHP form?

查看:115
本文介绍了关于如何使用PHP表单更新Mysql数据库的好教程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在寻找有关如何使用php表单更新mysql数据库的良好教程吗?

Looking for a good tutorial on how to update a mysql database using a php form?

推荐答案

更新数据非常简单.让我们从表单开始,供初学者使用:

Updating data can be pretty simple. Let's start with a form, for starters:

<form method="post" action="submit.php">
  <input type="text" name="id" value="12" />
  <input type="text" name="value" value="Jonathan" />
  <input type="submit" />
</form>

此表单会将数据发送到我们可以处理的submit.php脚本中,并将其传递到我们的数据库中.由于我们的表单方法是"post",因此我们所有的值都将通过PHP中的POST超级数组发送(如果您使用文件上传器,则不是这种情况).因此,在我们的submit.php页面中,我们可以打印ID和Value值,如下所示:

This form will send the data over to our submit.php script where we can handle it, and pass it into our database. Since our form method is "post," all of our values will be sent through the POST super array in PHP (this is not the case if you are using file uploaders). So within our submit.php page, we can print the ID and Value values like this:

print $_POST["id"]; // the name of the HTML element is the key
print $_POST["value"]; // again, note that we use the name as the key

在将用户提交的值直接传递到查询中时,您将需要小心,因此最好使用mysql_real_escape_string()这样的函数来清理数据:

You'll want to be careful about passing user-submitted values directly into your queries, so it's nice to clean up the data using a function like mysql_real_escape_string():

$id = mysql_real_escape_string( $_POST["id"] );
$value = mysql_real_escape_string( $_POST["value"] );

我们下一步要做的就是将它们放在查询中:

The next thing we'll want to do is place these in a query:

$sql = "UPDATE mytable SET value = '{$value}' WHERE id = {$id}";

这是个好时机,不要说我不鼓励您在实际环境中使用此示例代码.您将要查找sql​​-injections,以及如何避免它们.我在这里提供的代码仅是示例.输入我们的值后,将要运行的查询实际上如下所示:

This is a good time not state that I don't encourage you to use this example code in a live environment. You'll want to look up sql-injections, and how to avoid them. The code I'm providing here is merely an example. After our values are entered, the query that will be ran actually looks like this:

UPDATE mytable SET value = 'Jonathan' WHERE id = 12

现在,要运行此程序,我们需要连接到数据库.

Now, in order to run this, we need to be connected to a database.

$host = "localhost"; 
$user = "root"; 
$pass = "";
$database = "myDatabase";
$conn = mysql_connect($host, $user, $pass) or die( mysql_error() );
        mysql_select_db($database) or die( mysql_error() );

我们在这里要做的就是将我们的mysql-user-account凭据存储在数组中,并将它们传递给connect函数.这段代码应该是不言自明的,但是请让我知道它是否还不清楚.

All we're doing here is storing our mysql-user-account credentials in arrays, and passing them into a connect-function. This code should be pretty self-explanatory, but let me know if it's at all unclear.

一旦知道了,就可以运行查询了.请记住,我们将其存储在名为$sql:

Once you've got that, you're ready to run your query. Remember that we stored it in an array called $sql:

$result = mysql_query( $sql ) or die( mysql_error() );

就是这样.你做到了!假设没有任何问题,数据现在将在数据库中更新.您可以通过多种方式来增加通过此脚本提供给用户的信息.同样值得注意的是,您甚至在允许脚本运行之前都希望对数据进行清理-如果它不是可接受的数据(有人试图注入自己的查询),则需要将其吐回.

That's it. You did it! The data, assuming nothing went wrong, is now updated in your database. There are numerous ways you can increase the information provided back to the user via this script. Also worth noting is that you'll want to sanitize your data before even allowing the script to run - if it's not acceptable data (somebody trying to inject their own queries) you'll want to spit it back.

查看 PHP文档中的MySQL函数,以获取更多信息. ,如果有其他特定问题,请务必返回此处!

Check out the MySQL Functions in the PHP Documentation for more goodies, and be sure to return here when you have more specific questions!

这篇关于关于如何使用PHP表单更新Mysql数据库的好教程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆