2013-06-25 38 views
1

經過長時間的搜尋沒有能夠找到解決SQL語法錯誤和取消定義索引錯誤

Undefined index: coursename in C:\wamp\www\StudentInformationProject\Student_new\courseinsert.php on line 17

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1'>

這裏之後是代碼

if(isset($_POST["button"])) 
{ 
    $sql="INSERT INTO course(courseid, coursename, comment, coursekey) 
    VALUES('".$_POST['courseid']."','".$_POST['coursename']."', 
    '".$_POST['comment']."','".$_POST['coursekey']."')"; 

    if (!mysql_query($sql,$con)) 
    { 
     die('Error: ' . mysql_error()); 
    } 
    else 
    { 
     echo "1 record Inserted Successfully..."; 
    } 
} 
+2

「未定義的索引:coursename」 - 您似乎沒有在發佈課程名稱。另外,我希望你能儘快學習SQL注入。 – Blorgbeard

+0

嘗試''echo''sql'並查看是否可以看到它有什麼問題 – Blorgbeard

+0

正如@Blorgbeard所說,檢查[this](http://stackoverflow.com/questions/60174/how-to-prevent-sql步噴射功能於PHP?RQ = 1) –

回答

1

你的一個代變量中有一個雙引號。鑑於該錯誤消息,它可能是這樣的:

foo "1" bar 

您應該由他們加倍逃脫這樣的字符,所以它看起來像:

foo ""1"" bar 
0

這可能是你在評論值包含一個單引號,這將使SQL語法無效...