0

此代码工作正常: $batsman1name 作为新值插入到数据库的正确行中。

for($count = 1; $count <= 22; ++$count)
{
    $setbattingid = 'batsman' . $count . 'battingid';
    $$setbattingid = mysql_real_escape_string($_POST[$setbattingid]);
    $setname = "batsman" . $count . "name";
    $$setname = mysql_real_escape_string($_POST[$setname]);
    $query = "UPDATE batting_new SET batsmanname = NULLIF('$batsman1name', '') WHERE battingid = '$batsman1battingid'";
    $result = mysql_query($query);
    if (!$result) die ("Database access failed: " . mysql_error());
}

使用此代码,数据库不会更新,但变量 $$setname 和 $$setbattingid 确实包含与上面的 $batsman1name 和 $batsman1battingid 相同的值。

for($count = 1; $count <= 22; ++$count)
{
    $setbattingid = 'batsman' . $count . 'battingid';
    $$setbattingid = mysql_real_escape_string($_POST[$setbattingid]);
    $setname = "batsman" . $count . "name";
    $$setname = mysql_real_escape_string($_POST[$setname]);
    $query = "UPDATE batting_new SET batsmanname = NULLIF('$$setname', '') WHERE battingid = '$$setbattingid'";
    $result = mysql_query($query);
    if (!$result) die ("Database access failed: " . mysql_error());
}

有任何想法吗?如果我没有很好地解释我的问题,请告诉我?谢谢。

4

1 回答 1

1

你应该使用:

$query = "UPDATE batting_new SET batsmanname = NULLIF('${$setname}', '') WHERE battingid = '${$setbattingid}'";

如此处所述:http: //php.net/manual/language.variables.variable.php

于 2012-10-09T08:59:07.407 回答