MySQL查询中的PHP变量来自列表分解

MySQL查询中的PHP变量来自列表分解,php,mysql,Php,Mysql,我有一段代码无法使用。$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, $cit

我有一段代码无法使用。$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");
如果$test_消息的格式正确,您应该能够直接使用$num,如下所示:

mysql_queryUPDATE msg SET timecode_off='$timecode',其中num='$num'限制1

结果是:

$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);

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

$num_d = (string) end($num_ext);

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

/* 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子句,no for UPDATE子句,可能需要WHERE

首先删除ORDER BY子句。这是用于select语句的。接下来,在将字符串存储到数据库中之前,回显所有字符串,以了解应该存储的内容和实际存储的内容。如果输出没有给您错误的线索,请将输出张贴在此处。

您无法知道$num_ext[0]==$full-如果它包含逗号会怎么样?如果您在他张贴$num_ext[0]==$full是true$test_message=$full、$t_phone_number、$f_phone_number时分解此字符串$num_ext=explode,,$test_消息;上面没有他贴的绳子。我们需要看看这是怎么回事。在$full中有逗号。这就是这些变量来自$name、$status、$timecode、$latitude、$longitude、$num、$city、$state、$t\u phoneb的原因。请发布位于上面第1行之后的:var\u dump$test\u消息的结果。ORDER BY子句存在的原因是选择$numOk的最新记录。“更新”一次仅对一行起作用。如果需要更新多行,则需要一个循环结构。如上所述,在升级之前尝试一个SELECT查询以获取propr id,或者,根据mySql文档中列出的条件在数据库中创建一个视图:,然后在视图之外进行更新。$full结构是bbb,UP,2013-09-18 11:23:56,111.9999,123.2345,1234,加利福尼亚州圣地亚哥,2131234,我尝试了SELECT MAXid,但没有成功。我明白了,如果您可以在更新之前执行SELECT查询,可能会更好。我的意思是,首先,获取要更新的行,然后更新itOk,这样我就可以在子查询中更新该行。是否需要“修剪”?因为$full的一部分包含圣地亚哥或墨西哥城这样的项目,所以您需要两个不同的查询,一个用于获取需要更新的行,另一个用于执行更新。我不理解查询的原因,因为您有和id,但您没有更新,您的更新基于“num”,“num”字段的含义是什么?您在查询中寻找什么?。在执行查询之前,请尝试理解这一点,因为它看起来有点混乱。它不在末尾,它从末尾算起是3。
/* 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());