0

我有一段代码无法开始工作。需要将 $num 变量传递给 mysql 查询,以便它使用 $num 更新最新记录。

当我设置 $num_d = '1234'; 时代码有效

如何将 $num 变量传递给查询语句

 $test_message ="$full, $t_phone_number, $f_phone_number";

//explode sms message into variables
list($name, $status, $timecode, $latitude, $longitude, $num, $city, $state, $t_phoneb, $t_phone, $f_phone) = explode("," , $test_message);

print_r(explode(',', "$test_message"));

$num_ext = explode(",", $test_message);
$num_d = (string)$num_ext[5];

// Update database table
 mysql_query("UPDATE msg SET timecode_off= '$timecode' WHERE (num= '$num_d') ORDER BY id DESC LIMIT 1");
4

5 回答 5

0

$num_ext 中的索引“5”不存在。如果您确定 num_d 始终位于展开数组的末尾,您可以使用

$num_d = (string) end($num_ext);
于 2013-10-24T22:33:03.593 回答
0

首先删除 ORDER BY 子句。那是选择语句。接下来,在将所有字符串存储到数据库之前回显所有字符串,以了解应该存储什么以及实际存储什么。如果输出没有给您提供问题所在的线索,请在此处发布输出。

于 2013-10-24T22:59:07.770 回答
0

如果$test_message 格式正确,您应该可以$num直接使用:

mysql_query("UPDATE msg SET timecode_off= '$timecode' WHERE (num= '$num') LIMIT 1");

于 2013-10-24T22:31:19.813 回答
0
$test_message ="$full, $t_phone_number, $f_phone_number";
$num_ext = explode(",", $test_message);

结果是:

$num_ext[0] = $full;
$num_ext[1] = $t_phone_number;
$num_ext[2] = $f_phone_number; and so on

$num_d = $num_ext[5];
$sql = "UPDATE msg SET timecode_off= '$timecode' WHERE num='$num_d' ORDER BY id DESC LIMIT 1"
$query = mysql_query($sql, $connection);
于 2013-10-24T22:31:53.300 回答
0

你的代码看起来有问题,还有三行做同样的事情

/* Here, what is the $full format? I looks like a string with coma separated */
$test_message ="{$full}, {$t_phone_number}, {$f_phone_number}";

/* Mapping the array without spaces */
$array = array_filter(array_map('trim', explode(',', $test_message)));

/* Update database table */
 mysql_query("UPDATE msg SET timecode_off= '{$array[2]}' WHERE (num= '{$array[5]}');") or die (mysql_error());

我想,这段代码必须有效,但是我不知道$full结构,另一个需要注意的是 ORDER BY 限制,即 SELECT 子句,没有 UPDATE 子句,可能你需要一个 WHERE

于 2013-10-24T22:34:43.127 回答