2013-11-14 106 views
0

我建立使用字符串的“INSERT INTO” MySQL查询MySQL查询,但此查询的更新只列“日期”,并返回我的“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. 

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

任何方式使工作$ id成串?

+0

请不要”不再使用'mysql_ *'函数,它们已被弃用。请参阅[为什么我不应该在PHP中使用mysql_ *函数?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)以获取详细信息。相反,您应该了解[准备好的语句](http://bobby-tables.com/php.html)并使用[PDO](http://php.net/pdo)或[MySQLi](http:// php.net/mysqli)。如果你不能决定哪些,[这篇文章](http://php.net/manual/en/mysqlinfo.api.choosing.php)会帮助你。如果你选择PDO,[这里是一个很好的教程](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers)。 –

+0

你是如何创建'$ string'的? –

+0

在这种情况下,只需将你的SQL写入一个变量,而不是直接写入你的mysql命令。这样你可以输出生成查询并进行调试。调试就是一切! – ToBe

回答

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')"); 
+0

我试过了。不会工作,但现在我更新了整个代码 – user2987297