请帮助解决语法错误

时间:2013-08-01 23:22:58

标签: php mysql forms

我收到以下错误:


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 ''login' ('id', 'uname', email', 'pass') VALUES (NULL, 'X', 'X' at line 1

代码如下:


<?php

    require('config.php');
    if(isset($_POST['submit'])){

        //Perform the verification
        $email1 = $_POST['email1'];
        $email2 = $_POST['email2'];
        $pass1 = $_POST['pass1'];
        $pass2 = $_POST['pass2'];

        if($email1 == $email2){
            if($pass1 == $pass2){
                //All good. Carry on.

                $uname = mysql_escape_string($_POST['uname']);
                $email1 = mysql_escape_string($email1);
                $email2 = mysql_escape_string($email2);
                $pass1 = mysql_escape_string($pass1);
                $pass2 = mysql_escape_string($pass2);

                mysql_query("INSERT INTO 'login' 
                                ('id', 'uname', email', 'pass') 
                        VALUES (NULL, '$uname', '$email1', '$pass1')") or die(mysql_error());

            }else{
                echo "Passwords do not match.<br />";
                exit();
            }
        }else{
            echo "Emails do not match.<br /><br />";
            exit();
        }

请有人请我朝正确的方向推进

亲切的问候,

3 个答案:

答案 0 :(得分:5)

对表名和列名使用返回刻度而不是单引号。请仔细阅读以获得进一步说明:http://dev.mysql.com/doc/refman/5.0/en/identifiers.html

mysql_query("INSERT INTO `login` (`id`, `uname`, `email`, `pass`) VALUES (NULL, '$uname', '$email1', '$pass1')") or die(mysql_error());

为了指出其他问题,在您的查询中虽然“单引号”的使用不正确,但您在email列中缺少“单引号”。

另请注意,我们已弃用mysql_个扩展程序,因此请开始考虑实施mysqliPDO

答案 1 :(得分:0)

使用`character'转义字段和表名只是值分隔符。

答案 2 :(得分:0)

您应该使用反引号而不是撇号来转义表名和列名。