1

Here's my insert query.

$insertQuery = "INSERT INTO timeline (id_str, from_user, text, timestamp, location) VALUES ($resultsAry[$x]['id_str'], $resultsAry[$x]['from_user'], $resultsAry[$x]['text'], $datetime, $locationAry[$i]['place'])";

The values passing to the query seem correct (Checked by echo-ing all the values). But I got the following error.

Insert failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '['id_str'], Array['from_user'], Array['text'], 2011-05-23 18:58:27, Array['place' at line 1

Please help?

1
  • I think you need to quote your values. Commented May 23, 2011 at 11:07

3 Answers 3

2

To have PHP parse your array keys correctly you can encapsulate them with {} like this:

$insertQuery = "INSERT INTO timeline (id_str, from_user, text, timestamp, location) VALUES ({$resultsAry[$x]['id_str']}, {$resultsAry[$x]['from_user']}, {$resultsAry[$x]['text']}, $datetime, {$locationAry[$i]['place']})";
Sign up to request clarification or add additional context in comments.

Comments

2

Do yourself a favour and at the very least escape the values to secure the data. Concatenate the values into the query:

$insertQuery = "
INSERT INTO timeline (
   id_str, 
   from_user,
   text,
   timestamp, 
   location
) 
VALUES (
   '" . mysql_real_escape_string( $resultsAry[$x]['id_str'] ) . "',
   '" . mysql_real_escape_string( $resultsAry[$x]['from_user'] . "',
   '" . mysql_real_escape_string( $resultsAry[$x]['text'] . "',
   '" . mysql_real_escape_string( $dateTime ) . "',
   '" . mysql_real_escape_string( $locationAry[$i]['place']) . "'
);";

echo $insertQuery;

Comments

1

try this:

$insertQuery = "INSERT INTO timeline (id_str, from_user, text, timestamp, location) VALUES ('".$resultsAry[$x]['id_str']."', '".$resultsAry[$x]['from_user']."', '".$resultsAry[$x]['text']."', '".$datetime."', '".$locationAry[$i]['place']."')";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.