0

这是我的代码。

<?php

$a = bacon;
$b = 20;
$c = 30;

$link = mysql_connect('localhost','root',''); 
mysql_select_db("test", $link);

$sql = "UPDATE share SET price=".
         PrepSQL($b) . ", place=" .
         PrepSQL($c) . ", time=CURRENT_TIMESTAMP where num=1 and RID=(select IID from ingredient where Ingredient='" . PrepSQL($a) . "')";

mysql_query($sql);

function PrepSQL($value)
{
    // Stripslashes
    if(get_magic_quotes_gpc())
    {
        $value = stripslashes($value);
    }

    // Quote
    $value = "'" . mysql_real_escape_string($value) . "'";

    return($value);
}
?>

我发现上面的代码不能更新数据库表。但是,如果我将 where 条件更改为 where num=1 和 RID=(select IID from ingredients where Ingredient= 'bacon' )" 那么,一切正常。那么,我的代码有什么问题吗?非常感谢!

4

3 回答 3

0

如果它应该是一个字符串,那么它需要引号。

$a = 'bacon';
于 2013-03-20T09:24:43.560 回答
0

你应该使用:

<?php

$a = "bacon";
$b = 20;
$c = 30;

$link = mysql_connect('localhost','root',''); 
mysql_select_db("test", $link);

$sql = "UPDATE share SET price=".
         PrepSQL($b) . ", place=" .
         PrepSQL($c) . ", time=CURRENT_TIMESTAMP where num=1 and RID=(select IID from ingredient where Ingredient='" . PrepSQL($a) . "')";

mysql_query($sql);

function PrepSQL($value)
{
    // Stripslashes
    if(get_magic_quotes_gpc())
    {
        $value = stripslashes($value);
    }

    // Quote
    $value = "'" . mysql_real_escape_string($value) . "'";

    return($value);
}
?>
于 2013-03-20T09:25:45.010 回答
0

你的子查询

"[...] (select IID from ingredient where Ingredient='" . PrepSQL($a) . "')"

真的应该

"[...] (select IID from ingredient where Ingredient=" . PrepSQL($a) . ")"

因为调用PrepSQL已经为您添加了单引号。

另外,请避免使用mysql_*php 函数,因为它们现在已被弃用!有关更多选项,请参阅:http ://www.php.net/manual/en/mysqlinfo.api.choosing.php

编辑:

另外,您应该有一个可能未定义的bacon常量"bacon"

于 2013-03-20T09:26:07.353 回答