0

我正在尝试在 PHP 中创建一个更新函数,但记录似乎没有随着更新而改变。我创建了一个 JSON 对象来保存传递给这个文件的值,并且根据我运行的 Firebug Lite 控制台,这些值输出得很好,所以可能是 sql 端有问题。谁能发现问题?我会很感激帮助!

<?php

$var1 = $_REQUEST['action']; // We dont need action for this tutorial, but in a complex code you need a way to determine ajax action nature
$jsonObject = json_decode($_REQUEST['outputJSON']); // Decode JSON object into readable PHP object

$name = $jsonObject->{'name'}; // Get name from object
$desc = $jsonObject->{'desc'}; // Get desc from object
$did = $jsonObject->{'did'};// Get id object


mysql_connect("localhost","root","");  // Conect to mysql, first parameter is location, second is mysql username and a third one is a mysql password
@mysql_select_db("findadeal") or die( "Unable to select database"); // Connect to database called test


$query = "UPDATE deal SET dname = {'$name'}, desc={'$desc'} WHERE dealid = {'$did'}";
$add = mysql_query($query);
$num = mysql_num_rows($add);

if($num != 0) {

echo "true";

} else {

echo "false";

} 

?>
4

3 回答 3

1

您需要像反引号一样转义MySQL 中的保留字desc

UPDATE deal 
SET dname = {'$name'}, `desc`= {'$desc'} ....
                       ^----^--------------------------here
于 2013-02-19T18:06:37.000 回答
1

你需要mysql_affected_rows()更新后使用不是mysql_num_rows

于 2013-02-19T18:08:33.953 回答
1

我相信你在滥用花括号。单引号应该放在它们的外面。:

"UPDATE deal SET dname = {'$name'}, desc={'$desc'} WHERE dealid = {'$did'}"

变成

"UPDATE deal SET dname = '{$name}', desc='{$desc}' WHERE dealid = '{$did}'"

附带说明一下,使用任何 mysql_* 函数在安全方面都不是很好。我建议查看 php 的mysqlipdo扩展。

于 2013-02-19T18:09:14.480 回答