2011-02-16 102 views
0

我有一堆在我的代码数据库调用的。这有什么错我的SQL查询?

其中之一是这样的:

mysql_query("INSERT INTO coupons (id, retailerName, description, savingsDetails, pictureURL, rating, qrPicture, zipCode, dateAdded, dateExp) VALUES ('$newID', '$retailerName', '$description', '$savingsDetails', '$pictureURL', '0', '$qrPicture', '$zipCode', '$dateAdded', '$dateExp')"); 

所有其他数据库的完美要求的作品,但是这一次根本不写入任何数据到表。它出什么问题了?

+1

一个更多的优惠券网站,哦,不。 – zerkms 2011-02-16 07:11:15

回答

4

检查从MySQL查询返回的。

$result = mysql_query($query); 
if ($result === false) { 
    // Prints the query and the mysql error to help you find the problem. 
    die($query.'<br/>'.mysql_error()); 
} 

如果查询有问题,它会告诉你它是什么。

mysql_query()成功则返回true和false的错误不返回东西查询。

http://php.net/manual/en/function.mysql-query.php

当使用mysql_query()生产,也要优雅检查错误的返回值,并退出,最好记录,以便它可以帮助你解决任何问题。

另外,当执行使用用户输入的查询时,请确保使用mysql_real_escape_string()来避免SQL注入,并且将保护包含单引号的输入,这会破坏当前的查询。

http://php.net/manual/en/function.mysql-real-escape-string.php

1

检查从查询返回的值,找出发生了什么 -

$result = mysql_query("INSERT INTO coupons (id, retailerName, description, 
       savingsDetails, pictureURL, rating, qrPicture, zipCode, dateAdded, 
       dateExp) VALUES ('$newID', '$retailerName', '$description', 
       '$savingsDetails', '$pictureURL', '0', '$qrPicture', '$zipCode', 
       '$dateAdded', '$dateExp')"); 

if($result) 
{ 
    // Your query succeeded 
} 
else 
{ 
    // Your query failed due to some error 
    die(mysql_error()); 
} 
0

你可以只写为mysql_query("INSERT INTO coupons (id, retailerName, description, savingsDetails, pictureURL, rating, qrPicture, zipCode, dateAdded, dateExp) VALUES ('$newID', '$retailerName', '$description', '$savingsDetails', '$pictureURL', '0', '$qrPicture', '$zipCode', '$dateAdded', '$dateExp')") or die(mysql_error());

如果你没有得到一个错误,它意味着它成功了。如果你得到一个错误,会直接从MySQL错误(所以它应该是相当详细)。

0

找到问题的另一种方法:记录您的sql查询,查看查询在运行时的样子。

$str = "INSERT INTO coupons ..."; 

echo $str; 

然后直接尝试那个查询到你的数据库,你会得到不对的东西。