我在php中创建了代码,如下所示

时间:2015-05-01 03:10:02

标签: php syntax syntax-error

//name length
            if(strlen($name)>100 || strlen($name)<3)
                exit("name must be <3 and >100");


            //message length
            if(strlen($message)>100 || strlen($message)<3)
                exit("message must be <3 and >100");

                $date = date("d-m-y");

            //Database
            $connection= mysqli_connect('localhost','root','','zatona');

            $querycheck=mysqli_query($connection,'INSERT INTO `message`(`name`, `message`, `date`) VALUES ('C:\\wamp\\www\\1\\{$name}', '$message', '$date')');

            if($querycheck == true)
            {
                //message
                echo "message added";
            }
            else{
                //error message
                echo "Error adding new message";
            }
                mysqli_close($connection);
        }

但它给出了错误语法错误:

  

意外&#39; $ name&#39; (T_VARIABLE)

怎么解决?使用wamp服务器

1 个答案:

答案 0 :(得分:3)

您应该在查询字符串中转义单引号'或使用双"

 $querycheck = mysqli_query($connection,"INSERT INTO `message` (`name`, `message`, `date`) VALUES ('C:\\wamp\\www\\1\\$name', '$message', '$date')");