下午好。运行以下INSERT INTO语句时,我一直收到以下错误。
错误:您的SQL语法出错;检查手册 对应于您的MySQL服务器版本,以便使用正确的语法 在第2行',,,,,,,,)附近
我完全难过了。这可能与我在表中称为'id'的增量值有关吗?这是代码。非常感谢提前!!
<?php
$con=mysqli_connect("localhost","root","root","wexdemo");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$name = mysqli_real_escape_string($_POST['name']);
$address = mysqli_real_escape_string($_POST['address']);
$area = mysqli_real_escape_string($_POST['area']);
$postcode = mysqli_real_escape_string($_POST['postcode']);
$sector = mysqli_real_escape_string($_POST['sector']);
$subsector = mysqli_real_escape_string($_POST['subsector']);
$contact = mysqli_real_escape_string($_POST['contact']);
$position = mysqli_real_escape_string($_POST['position']);
$email = mysqli_real_escape_string($_POST['email']);
$telephone = mysqli_real_escape_string($_POST['telephone']);
$sql="INSERT INTO employers (name, address, area, postcode, sector, subsector, contact, position, email, telephone)
VALUES ($name, $address, $area, $postcode, $sector, $subsector, $contact, $position, $email, $telephone)";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
答案 0 :(得分:1)
插入查询值应该包含在'
单引号或"
双引号内。
$sql ="INSERT INTO employers (`name`, `address`, `area`, `postcode`, `sector`, `subsector`, `contact`, `position`, `email`, `telephone`)
VALUES ('$name', '$address', '$area', '$postcode', '$sector', '$subsector',
..^
'$contact', '$position', '$email', '$telephone')";
而不是
$sql="INSERT INTO employers (name, address, area, postcode, sector, subsector, contact, position, email, telephone)
VALUES ($name, $address, $area, $postcode, $sector, $subsector, $contact, $position, $email, $telephone)";
答案 1 :(得分:1)
如果列类型为字符串,则要插入的值必须是单引号或双引号。试试这段代码:
$sql="INSERT INTO employers (name, address, area, postcode, sector, subsector, contact, position, email, telephone)
VALUES ('$name', '$address', '$area', '$postcode', '$sector', '$subsector', '$contact', '$position', '$email', '$telephone')";
您可能不希望用引号括起来的唯一值是具有数值的值 - 即$postcode
和$telephone
答案 2 :(得分:0)
变量不应包含在“或”中;即使查询已填充并且看起来正确,我也会收到相同的错误 MyError: INSERT INTO dati_modelle VALUES (1, sonia, nota 1, 14, Height, 165, Bust, 60, Waist, 65, Hips, 60, Shoe, 33, Eyes, brown) 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“1, 14, Height, 165, Bust, 60, Waist, 65, Hips, 60, Shoe, 33, Eyes, brown)”附近使用正确的语法
答案 3 :(得分:-1)
$sql="INSERT INTO 雇主(姓名、地址、地区、邮政编码、部门、分部门、联系人、职位、电子邮件、电话) 值 ( " + "" + $name +"" + ", " + "" + $address +"" + ", " + "" + $area + "" + ", " + ""+ $sector + " " + ", " + ""+ $subsector+ "" + ", " + ""+$contact + "" + ", " + ""+$position +"" + ", " + ""+ $email + "" + ", " + "" + $电话 + "" + ")";