0

I build an "INSERT INTO" MySQL query using a string, but this query updates just column "date" and return me "0" as value of column "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 of $insert is correct, but actually I'm not able to update the column "id".

Any way to make working $id into the string?

  • Please don't use `mysql_*` functions anymore, they are deprecated. See [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) for details. Instead you should learn about [prepared statements](http://bobby-tables.com/php.html) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide which, [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you. If you pick PDO, [here is a good tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). – Marcel Korpel Nov 14 '13 at 16:07
  • How are you creating your `$string`? – gen_Eric Nov 14 '13 at 16:13
  • In such cases, just write your SQL into a variable and not directly into your mysql command. That way you can output the generates query and debug it. Debugging is everything! – ToBe Nov 14 '13 at 16:14
  • It seems your `$string` has the literal string `'$id'` in it. This won't magically be replaced by the `$id` variable. You need to set `$id` *before* you set `$string` and then add `$id` to it. – gen_Eric Nov 14 '13 at 16:16
  • Or you need to use `str_replace` to replace `'$id'` with the value of `$id`. – gen_Eric Nov 14 '13 at 16:21
  • @Rocket Hazmat. Yes you are right is literal. May u pls help I updated the whole code. – user2987297 Nov 14 '13 at 16:24
  • Just simply set `$id=54;` before the `while` loop, and then do: `implode(", ('{$id}', '", str_split($nexstring, 21));`. Note the double quotes. – gen_Eric Nov 14 '13 at 16:27
  • Ok @Rocket Hazmat it's working. Unfortunatly I can't vote your comments. Thanks – user2987297 Nov 14 '13 at 16:37

1 Answers1

1

Variables enclosed inside single quotes ('') aren't parsed, therefore, always use double quotes ("") when enclosing variable names in a string..

USE BELOW

$insert = mysql_query("INSERT INTO events (id, date) VALUES ('{$id}', ' 2013-11-29 12:00:00'), ('{$id}', ' 2013-11-30 12:00:00')");
Ghazanfar Mir
  • 3,493
  • 2
  • 26
  • 42