0

我在 mysql 数据库中插入数据时遇到问题。错误显示如下:“您的 SQL 语法有错误;请检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '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 吗????

4

3 回答 3

1

是的,有一个语法错误正确的语法是:

$sql = "INSERT INTO (id,firstname) VALUES (1,'Oussaki')";

您必须指定要在其上插入的字段..祝您好运

于 2013-03-14T23:10:12.563 回答
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."')";
于 2013-03-14T23:11:16.527 回答
-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');
于 2017-05-22T22:45:05.967 回答