关于如何使用 PHP 表单更新 Mysql 数据库的好教程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2466975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:34:50  来源:igfitidea点击:

Good tutorial on how to update your Mysql database with a PHP form?

phpmysql

提问by php

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

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

回答by Sampson

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>

This form will send the data over to our submit.phpscript 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 POSTsuper array in PHP (this is not the case if you are using file uploaders). So within our submit.phppage, we can print the ID and Value values like this:

此表单会将数据发送到submit.php我们可以处理的脚本中,并将其传递到我们的数据库中。由于我们的表单方法是“post”,我们所有的值都将通过POSTPHP 中的super 数组发送(如果您使用的是文件上传器,则情况并非如此)。所以在我们的submit.php页面中,我们可以像这样打印 ID 和 Value 值:

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

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():

您需要小心将用户提交的值直接传递到您的查询中,因此最好使用如下函数清理数据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}";

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:

这是一个很好的时机,不要声明我不鼓励您在实时环境中使用此示例代码。您将需要查找 sql 注入,以及如何避免它们。我在这里提供的代码只是一个例子。输入我们的值后,将运行的查询实际上如下所示:

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() );

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.

我们在这里所做的就是将我们的 mysql-user-account 凭据存储在数组中,并将它们传递给连接函数。这段代码应该是不言自明的,但如果不清楚,请告诉我。

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

一旦你有了它,你就可以运行你的查询了。请记住,我们将它存储在一个名为 的数组中$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.

就是这样。你做到了!假设没有出错,数据现在已在您的数据库中更新。您可以通过多种方式增加通过此脚本提供给用户的信息。另外值得注意的是,您甚至需要在允许脚本运行之前清理您的数据 - 如果它是不可接受的数据(有人试图注入他们自己的查询),您将想要将其吐回。

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

查看PHP 文档中MySQL 函数以获取更多好东西,如果您有更具体的问题,请务必返回此处!