Php 如何将表单脚本中的记录插入mysql数据库?
//如何修改以下代码,以便将插入到表单中的值插入到mysql数据库中?我可以很好地连接到我的数据库,我的数据库名为images,表名为personsPhp 如何将表单脚本中的记录插入mysql数据库?,php,mysql,Php,Mysql,//如何修改以下代码,以便将插入到表单中的值插入到mysql数据库中?我可以很好地连接到我的数据库,我的数据库名为images,表名为persons //This is my insert.php file <?php $con=mysqli_connect("localhost","root","anble","images"); // Check connection if (mysql
//This is my insert.php file
<?php
$con=mysqli_connect("localhost","root","anble","images");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$FirstName = mysqli_real_escape_string($_POST["FirstName"]);
$LastName = mysqli_real_escape_string($_POST["LastName"]);
$Age = mysqli_real_escape_string($_POST['Age']);
$sql="INSERT INTO Persons (Name, LastName, Age);
VALUES ($FirstName, $LastName, $Age)";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
// This is my form file
<html>
<body>
<form action="insert.php" method="post">
Firstname: <input name="FirstName" type="text" value="FirstName">
Lastname: <input name="LastName" type="text" value="LastName">
Age: <input name="Age" type="text" value="Age">
<input type="submit">
</form>
</body>
</html>
// This is the error
Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in C:\xampp \htdocs\check_php\insert.php on line 10
Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in C:\xampp\htdocs\check_php\insert.php on line 11
Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in C:\xampp\htdocs\check_php\insert.php on line 12
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; VALUES (, , )' at line 1
更改此项:
1 mysqli_real_escape_字符串需要2个必需参数
2$Firstname和$Lastname是字符串变量。在查询中使用时,它们应该用单引号括起来
$FirstName = mysqli_real_escape_string($con, $_POST["FirstName"]);
$LastName = mysqli_real_escape_string($con, $_POST["LastName"]);
$Age = mysqli_real_escape_string($con, $_POST['Age']);
$sql="INSERT INTO Persons (Name, LastName, Age);
VALUES ('$FirstName', '$LastName', $Age)";
卡恩奇:
// escape variables for security
$FirstName = mysqli_real_escape_string($_POST["FirstName"]);
$LastName = mysqli_real_escape_string($_POST["LastName"]);
$Age = mysqli_real_escape_string($_POST['Age']);
致:
mysqli_real_escape_字符串需要传递两个参数
1连接
2替罪羊串
您只通过了escapestring,需要在mysqli\u real\u escape\u字符串中提供链接标识符
您还需要在查询内的字符串变量中添加引号。
更改:
致:
在PHP手册中查找mysqli_real_escape_字符串,并查看它期望的参数。这是非常基本的东西,所以呢?错误消息准确地告诉您出了什么问题。看一下手册,你就会知道如何修理它。我15岁了。这是我的借口。你的是什么?你刚刚评论并回复了我,我15岁了,感谢你不仅浪费了我的时间,还浪费了其他人的时间;检查与您的MySQL服务器版本相对应的手册,以了解使用near'的正确语法;第1行$Firstname和$Lastname处的值Andrew、Brin、15'是字符串变量。在查询中使用时,它们应该用单引号括起来。请尝试我的更新答案,并尝试了解您的脚本@user3553512I中出现了什么问题。现在出现了以下错误-错误:您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以了解使用near'的正确语法;第1行的值“FirstName”、“LastName”、“Age”
// escape variables for security
$FirstName = mysqli_real_escape_string($con,$_POST["FirstName"]);
$LastName = mysqli_real_escape_string($con,,$_POST["LastName"]);
$Age = mysqli_real_escape_string($con,$_POST['Age']);
$sql="INSERT INTO Persons (Name, LastName, Age);
VALUES ($FirstName, $LastName, $Age)";
$sql="INSERT INTO Persons (Name, LastName, Age)
VALUES ('$FirstName', '$LastName', '$Age')";