2013-03-14 116 views
0

我在將數據插入到mysql數據庫時遇到問題。 錯誤是表明這樣的: 「您的SQL語法錯誤;檢查對應於你的MySQL服務器版本使用附近的正確語法手冊‘1’)」第1" 行MYSQL插入數據

<?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"; 

?> 

順便說一句,我ahve 2列在我的表中命名ID和滾動應該自動生成...在這種情況下,它是正確的SQL?

回答

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."')"; 
1

是的,有一個語法錯誤 正確的語法爲:

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

,你必須指定要插入就可以了..領域,所有的好運氣

-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');