2014-09-28 64 views
-5

我收到以下錯誤:語法錯誤,意外'(T_ENCAPSED_AND_WHITESPACE),預計 ']'

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting ']' in C:\Program Files\EasyPHP-DevServer-14.1VC9\data\localweb\projects\post.php on line 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); 
?> 
+0

如果你直接在你的字符串中輸入變量,你應該**不要**在你的鍵中使用空格。 '電話號碼'是一個很大的nono。也就是說,使用PDO或MySQLi('mysql_'已棄用)。使用預準備的語句並綁定你的變量。如果你不打算進行SQL注入。 – h2ooooooo 2014-09-28 04:34:29

+0

請花點時間閱讀http://stackoverflow.com/help/how-to-ask,然後考慮修改您的問題。 – 2014-09-28 04:34:43

回答

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); 
?> 
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 

重要提示:不要訪問超級全球直接進入查詢,$ 郵政是超全局, 使用的mysqli功能或PDO,

在您使用查詢前正確地轉義您的數據,

閱讀對你的問題的評論,謝謝

相關問題