0

我使用字符串构建了一个“INSERT INTO”MySQL 查询,但是这个查询只更新列“date”并返回“0”作为列“id”的值

$c= "2013-11-29 12:00:00";//whole code
$d= "2013-11-30 12:00:00";
$date_3 = date("Y-m-d g:i:s", strtotime("$c"));
$date_4 = date("Y-m-d g:i:s", strtotime("$d"));
$results = array($date_1);
$i = $date_3;
$allCane="";
while ($i <= $date_4) {
$i = date("Y-m-d g:i:s", strtotime($i));
array_push($results, $i);
$k= $i . "\n";
$chunks = str_split($k, 19);
$nexstring = join('\')', $chunks);
$cane = implode(', (\'{$id}\', \'', str_split($nexstring, 21));
$allCane .= $cane; // appends $cane to $allCane
$i = date("Y-m-d g:i:s",strtotime("+1 day", strtotime($i)));
}
$string ='(\'{$id}\', \' '.$allCane;
$string=substr($string,0,-12);
echo $string;//('{$id}', ' 2013-11-29 12:00:00'), ('{$id}', ' 2013-11-30 12:00:00')
$id=54;

$insert = mysql_query("INSERT INTO events (id, date) VALUES $string");
// i build the query using $string. 

$insert的语法是正确的,但实际上我无法更新“id”列。

有什么方法可以将工作$id放入字符串中?

4

1 回答 1

1

Variables包含在single quotes('') 中的内容不会被解析,因此,double quotes在将变量名包含在字符串中时,请始终使用 ("")。

在下面使用

$insert = mysql_query("INSERT INTO events (id, date) VALUES ('{$id}', ' 2013-11-29 12:00:00'), ('{$id}', ' 2013-11-30 12:00:00')");
于 2013-11-14T16:10:32.307 回答