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?

8
  • Please don't use mysql_* functions anymore, they are deprecated. See Why shouldn't I use mysql_* functions in PHP? for details. Instead you should learn about prepared statements and use either PDO or MySQLi. If you can't decide which, this article will help you. If you pick PDO, here is a good tutorial. Commented Nov 14, 2013 at 16:07
  • How are you creating your $string? Commented Nov 14, 2013 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! Commented Nov 14, 2013 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. Commented Nov 14, 2013 at 16:16
  • Or you need to use str_replace to replace '$id' with the value of $id. Commented Nov 14, 2013 at 16:21

1 Answer 1

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')");
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.