从下拉列表中插入数据到mySql表


inserting data from dropdown list in mySql table

我想做一个表单,其中用户插入数据,如姓,名,年龄,邮件和位置,这些值在数据库中更新。所有变量都是通过文本插入的,但位置变量是一个下拉列表。form.php文件如下所示:

<html>
<body>
<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname">
Lastname: <input type="text" name="lastname">
Age: <input type="text" name="age">
mail:<input type="text" name="mail">
Location:<select name="education">
<option value="option1">option1</option>
<option value="option2">option2</option>
<option value="option3">option3</option></select>:<br />
<input type="submit">
</form>
</body>
</html>
下面是insert.php文件:
<?php
$firstname = trim(mysql_real_escape_string($_POST['firstname']));
$lastname = trim(mysql_real_escape_string($_POST['lastname']));
$age = trim(mysql_real_escape_string($_POST['age']));
$mail = trim(mysql_real_escape_string($_POST['mail']));
$location = trim(mysql_real_escape_string($_POST['location']));

$sql="INSERT INTO persons (firstname, lastname, age, mail, location)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]', '$_POST[mail]','$_POST[location]')";

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>

当我尝试使用这个时,我得到一个消息说:

注意:未定义的索引:位置在C:'xampp'htdocs'family_guy'insert.php第11行

注意:未定义的索引:位置在C:'xampp'htdocs'family_guy'insert.php第16行新增1条记录

除location外的所有值都保存在数据库中。请帮我解决这个问题。

第一:无法在$_POST['location']中获得Location值的原因是表单字段命名为education。换成location就行了。- HTML标签中的name属性是$_POST数组的关键字-因此它们必须匹配(即使使用大小写)。

第二:SQL注入可能与您的代码。您转义了用户输入的数据$firstname = trim(mysql_real_escape_string($_POST['firstname']));,但是在$sql中使用了未转义的数据。你必须使用

$sql="INSERT INTO persons (firstname, lastname, age, mail, location)
VALUES
('$firstname','$lastname','$age', '$mail','$location')";

或更好的准备语句:http://php.net/manual/de/mysqli.quickstart.prepared-statements.php

第三:你不应该混合mysqli_*和mysql_* API调用

你用错词了

Location:<select name="education">

改为

Location:<select name="location">

您的位置字段名称为education。将其更改为location

:

Location:<select name="education">

应为

Location:<select name="location">

你的代码有一些问题。

<select name="education">$_POST['location']开始,这应该是你想要的<select name="location"> -你使用了错误的名称。

  • 接下来,是使用 mysql_ 为基础的函数=> mysql_real_escape_string

in和所有其他使用mysql_real_escape_string

$firstname = trim(mysql_real_escape_string($_POST['firstname']));

应该读作(同时对其他的应用相同的函数语法)

$firstname = trim(mysqli_real_escape_string($con,$_POST['firstname']));

mysqli_mysql_函数在执行代码期间(在同一页面上)或使用基于mysqli_的DB连接时不会混合在一起,这就是你的。

mysqli_query($con,$sql)

所以现在你需要确保在使用$firstname = trim(mysqli_real_escape_string($con,$_POST['firstname']));等之前你已经成功地放置了DB连接。

另外,使用$firstname等变量,而不是使用('$_POST[firstname]'...)";

('$firstname', '$lastname', '$age', '$mail', '$location') .

因为在使用$_POST[firstname]'等在你的值,你不使用mysql_real_escape_string()函数,也不使用POST数据;它只是躺在那里,什么也不做。

旁注关于mysql_real_escape_string() -应该基于mysqli_,如前所述。


话虽如此,还是使用准备好的语句,或者PDO使用准备好的语句,这样更安全;)

另外,将错误报告添加到打开<?php标记后的文件顶部。

error_reporting(E_ALL);
ini_set('display_errors', 1);

将触发/显示发现的任何错误。


相关功能链接:

  • mysqli_real_escape_string() ——你应该使用什么。

  • mysql_real_escape_string() ——不混合mysqli_