我正在尝试这段代码,但是当我运行它时没有任何反应,同时我无法显示数据库中的任何内容?有什么建议吗?
<?php
$con=mysqli_connect("localhost","root","","vb");
// 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['Forename']);
$lastname = mysqli_real_escape_string($_POST['Surname']);
$email = mysqli_real_escape_string($_POST['Email']);
$sql="INSERT INTO 'customer1' ('Forename', 'Surname', 'Email')
VALUES ($Forename, $Surname, $Email)";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
答案 0 :(得分:0)
您正在使用单个qoutes转义表名customer1
和列错误。您必须使用``反引号
$sql="INSERT INTO `customer1` (`Forename`, `Surname', `Email`)
VALUES ($Forename, $Surname, $Email)";
答案 1 :(得分:0)
问题在于您的SQL查询
$sql="INSERT INTO customer1 (Forename, Surname, Email)
VALUES ('$Forename', '$Surname', '$Email')";
从表名和列名中删除引号,并为所有值添加引号
答案 2 :(得分:0)
如果值为字符串,则在值中添加引号。否则,插入查询总是会失败。
试试这个:
$sql='INSERT INTO customer1 (`Forename`, `Surname`, `Email`)
VALUES ("'.$Forename.'","'.$Surname.'", "'.$Email.'")';
从表名和字段名中删除其他不必要的引号。