语法错误,意外的'' (T_ENCAPSED_AND_WHITESPACE),期待']'

时间:2014-09-28 04:31:19

标签: php mysql

我收到以下错误:

  

解析错误:语法错误,意外''(T_ENCAPSED_AND_WHITESPACE),   在C:\ Program中期待']'   Files \ EasyPHP-DevServer-14.1VC9 \ data \ localweb \ projects \ post.php on   第13行

代码:

<?php 
 $con=mysql_connect("127.0.0.1", "root", ""); 
 mysql_select_db("registration");
if(!$con)
{
echo "Failed to Connect to MySql".mysqli_connect_error();
}
else
{
echo"success";
} 
$query="INSERT INTO feed 
VALUES('$_POST[Name]','$_POST[Department]','$_POST[Event]','$_POST[Email]','$_POST[Phone no]')";
mysql_query($query,$con);
 ?> 

2 个答案:

答案 0 :(得分:0)

尝试使用此代码来帮助您解决错误。这应该工作。按照指示。

<?php

//You might want to use a session for this. //
        session_start();
        $con=mysqli_connect("localhost", "root", "", "databasename");
        if (mysqli_connect_errno()) {
             echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
//Ensure the variables are the same as the ones in the database. There should be no spaces. //

        $name = mysqli_real_escape_string($con, $_POST['name']);
        $department = mysqli_real_escape_string($con, $_POST['department']);
        $email = mysqli_real_escape_string($con, $_POST['email']);
        $phone_number = mysqli_real_escape_string($con, $_POST['phone_number']);


    $sql="INSERT INTO users SET name='$name', department = '$department', email = '$email', phone_number = '$phone_number' WHERE id='" .$_SESSION['id']."'";

        if (!mysqli_query($con,$sql)) {
          die('Error: ' . mysqli_error($con));
        }
        header("Location: pagetobedirected.php");
        mysqli_close($con);
?>

答案 1 :(得分:0)

您的查询应该是

      $query="INSERT INTO feed (columnName_1, columnName2, columnName_3, columnName_4, columnName_5)
    VALUES('".$_POST['Name']."','".$_POST['Department']."','".$_POST['Event']."',
'".$_POST['Email']."',
    '".$_POST['phone_no']."')"; //cant use $_post['phone no'] means you cant give space, it has
//to be phone_no.
change columnName_1 and so with your originall column name in your database

重要提示:请勿直接访问超级全局查询,$ POST是超全局, 使用mysqli 功能或PDO,

在查询中使用之前正确转义数据

阅读有关您问题的评论,谢谢