2014-10-01 65 views
-2
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 'table(name,email,pass,price) VALUES('abhi','[email protected]','213123','67')' at line 1. 

這裏abhi是[email protected]的值是email的值.2313123是pass.67的值。這些值被髮送到insert.php fron form.php。這個錯誤是在插入查詢中的insert.php中,似乎沒事。所以plz的人幫助我。如何在php中處理這個錯誤?

there are two files. 
1.form.php 
<html> 
<head><title>Form</title></head> 
<body> 
<?php 
echo '<form method="post" action="insert.php"> 
<label>Name:</label> 
<input type="text" name="name" /> 

<label>Email:</label> 
<input type="text" name="email" /> 

<label>Password:</label> 
<input type="password" name="pass" /> 

<label>Price:</label> 
<input type="text" name="price" /> 

<input type="submit" name="Submit" value="Submit"/> 
</form>' 
?> 
</body> 
</html> 

Second file is insert .php 

<?php 
$con=mysqli_connect('localhost','root','','product') ; 

if(mysqli_connect_errno()) 
{ 
echo "Failed to connect to mysql".mysqli_connect_error(); 
} 


$name=$_POST['name']; 
$email=$_POST['email']; 
$pass=$_POST['pass']; 
$price=$_POST['price']; 


$query = "INSERT INTO table(name,email,pass,price) VALUES('$name','$email','$pass','$price')"; 
if(!mysqli_query($con,$query)) 
{ 
die('Error: '.mysqli_error($con)); 
} 
echo "1 record added"; 
mysqli_close($con); 
?> 
+1

和你的表名是'table'?你能找到更好的名字而不是'table'嗎? – 2014-10-01 10:09:31

+1

@ user3717775注意S​​QL注入! – Biffen 2014-10-01 10:10:34

回答

0

表格不是表格的有效名稱(因爲它是保留名稱)。您可以(最好)將表格重命名爲另一個名稱,或者使用INSERT INTO table和`-signs arround表格。

0

reserved word。永遠不能使用保留字作爲名稱表。