我在mysql数据库中插入数据时遇到问题。 错误显示如下: “您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以便在第1行附近使用正确的语法'''”
<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$fathername = $_POST['fathername'];
$mothername = $_POST['Firstname'];
$fatherjob = $_POST['fatherjob'];
$motherjob = $_POST['motherjob'];
$phone = $_POST['phone'];
$mobile1 = $_POST['mobile1'];
$mobile2 = $_POST['mobile2'];
$address = $_POST['address'];
$id=1;
$roll=1;
$sex = $_POST['sex'];
$bloodgroup = $_POST['bloodgroup'];
$class = $_POST['class'];
$section = $_POST['section'];
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$birthday=$day."-".$month."-".$year;
$hostname_localhost ="localhost";
$database_localhost ="school";
$username_localhost ="root";
$password_localhost ="";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_localhost, $localhost);
$sql = "INSERT INTO student_info VALUES ('".$id."',".$firstname."','".$lastname."','".$sex."','".$bloodgroup."','".$fathername."','".$mothername ."','".$fatherjob."','".$motherjob."','".$address."','".$birthday."','".$phone."','".$mobile1."','".$mobile2."','".$class."','".$section."','".$roll."')";
mysql_query($sql) or die(mysql_error());
echo "updated succesfully";
?>
顺便说一句,我在我的表中有2列名为id和roll,应该自动生成...在这种情况下,它是正确的sql ????
答案 0 :(得分:1)
是的是语法错误 正确的语法是:
$sql = "INSERT INTO (id,firstname) VALUES (1,'Oussaki')";
你必须指定要在其上插入的字段..祝你好运
答案 1 :(得分:0)
最好的方法是使用提及下列列的查询:
$sql = "INSERT INTO student_info (id, firstname, ..... ) VALUES ('".$id."',".$firstname."','".$lastname."','".$sex."','".$bloodgroup."','".$fathername."','".$mothername ."','".$fatherjob."','".$motherjob."','".$address."','".$birthday."','".$phone."','".$mobile1."','".$mobile2."','".$class."','".$section."','".$roll."')";
答案 2 :(得分:-1)
你在这里犯了错误
$sql = "INSERT INTO student_info VALUES ('".$id."',".$firstname."','".$lastname."','".$sex."','".$bloodgroup."','".$fathername."','".$mothername ."','".$fatherjob."','".$motherjob."','".$address."','".$birthday."','".$phone."','".$mobile1."','".$mobile2."','".$class."','".$section."','".$roll."')";
要在MYSQL中插入数据,您必须首先确认要插入的column
和哪个值。
因此,在声明VALUES
之前,您必须先指定要存储的列$id $firstname
。
这同样适用于其他人。
此外,$id
和其他人之前和之后不需要“点”。
我希望您的数据库包含id firstname
等列。
例如正确的代码
$sql = "INSERT INTO student_info (id,firstname) VALUES ('$id',$firstname');