2013-06-25 19 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语法无效...