How to post data to a database table using PHP MySQL Into
如何使用PHP MySQL Into将数据发布到数据库表
As MySql extension has been deprecated, the usual way to insert data into a database using php involves the use of mysqli(either procedural or oop version) or PDO.
由于不建议使用MySql扩展,因此使用php将数据插入数据库的通常方法涉及使用mysqli(过程版本或oop版本)或PDO。
Here we'll use mysqli. You can check the Php manual here: http://www.php.net/manual/en/book.mysqli.php
在这里,我们将使用mysqli。 您可以在此处查看Php手册: http : //www.php.net/manual/zh/book.mysqli.php
First of all, the database connection:
首先,数据库连接:
<?php
$host = "www.mysite.com";
$username = "marqusG";
$pwd = "12Ab34cD";
$db = "mydatabase";
$dbconn = mysqli_connect($host, $username, $pwd, $db);
if (mysqli_connect_errno()) {
echo "Connection to the database failed with the following error: " . mysqli_connect_error();
}
Now we can insert some data in the database:
现在我们可以在数据库中插入一些数据:
<?php
mysqli_query(
$dbconn,
"INSERT INTO coders (NickName, Language, Level) VALUES ('marqusG', 'PHP', 'Poor')"
);
As you guess, the most usual situation in the real world is inserting data from a form, so now we'll go to build a simple html form:
您可能猜到了,现实世界中最常见的情况是从表单中插入数据,因此现在我们将构建一个简单的html表单:
<form method="post">
NickName: <input type="text" name="nickname" />
Language: <input type="text" name="language" />
Level: <input type="text" name="level" />
<input type="submit" />
</form>
Note that omitting action for our form will make the form send data to the same page and later we'll see how to build the whole page. See now how the php code receives data and insert them into database (we'll use filter_input function available as PHP 5.2.0 to get form values from the $_POST array: http://www.php.net/manual/en/function.filter-input.php):
请注意,省略表单的动作会使表单将数据发送到同一页面,稍后我们将看到如何构建整个页面。 现在查看php代码如何接收数据并将其插入数据库(我们将使用PHP 5.2.0提供的filter_input函数从$ _POST数组获取表单值: http ://www.php.net/manual/zh/ function.filter-input.php ):
$nickname = filter_input(INPUT_POST, 'nickname');
$language = filter_input(INPUT_POST, 'language');
$level = filter_input(INPUT_POST, 'level');
$query = "INSERT INTO coders (NickName, Language, Level) VALUES ('$nickname', '$language', '$level');
if (!mysqli_query($dbconn, $query)) {
die("Error inserting data into database: " . mysqli_error($dbconn));
}
echo "Data inserted successfully!";
Finally we can build our page. You'll see I tried to gently present output to the user editing slightly the code above, but the core remains the same.
最后,我们可以建立页面。 您会看到我试图将输出略微呈现给用户,对上面的代码进行了稍微的编辑,但是核心保持不变。
<?php
//SET DATABASE CONNECTION
$host = "www.mysite.com";
$username = "marqusG";
$pwd = "12Ab34cD";
$db = "mydatabase";
$dbconn = mysqli_connect($host, $username, $pwd, $db);
if (mysqli_connect_errno()) {
echo "Connection to the database failed with the following error: " . mysqli_connect_error();
}
//CHECK IF FORM HAS BEEN SUBMITTED USING FILTER_HAS_VAR (http://www.php.net/manual/en/function.filter-has-var.php)
if (filter_has_var(INPUT_POST, 'nickname')) {
//GET VALUES FROM THE $_POST ARRAY USING FILTER_INPUT (http://www.php.net/manual/en/function.filter-input.php)
$nickname = filter_input(INPUT_POST, 'nickname');
$language = filter_input(INPUT_POST, 'language');
$level = filter_input(INPUT_POST, 'level');
if (!mysqli_query($dbconn, $query)) {
$output = "Error inserting data into database: " . mysqli_error($dbconn) . "<br />";
$output .= "Please, retry.<br />";
$output .= <<<FRM
<form method="post">
NickName: <input type="text" name="nickname" />
Language: <input type="text" name="language" />
Level: <input type="text" name="level" />
<input type="submit" />
</form>
FRM;
} else {
$output = "Data inserted successfully!";
$ouput .= "Following data have been added to your database:<br />";
$ouput .= "NickName: $nickname<br />";
$ouput .= "Language: $language<br />";
$ouput .= "Level: $level<br />";
}
}else{
$ouput = "Please, insert your data in the form below:<br />";
$output .= <<<FRM
<form method="post">
NickName: <input type="text" name="nickname" />
Language: <input type="text" name="language" />
Level: <input type="text" name="level" />
<input type="submit" />
</form>
FRM;
mysqli_close($dbconn);
}
?>
<html>
<body>
<?php echo $output; ?>
</body>
</html>