1

I am trying to simple insert comma separated value in table field.But I don't get any cause why it's not working.

<?php

$array = array('1', '2', '3');
$comma_separated = implode(",", $array);

echo $comma_separated; 

$sql = "INSERT INTO like(user_id,like_data)
VALUES ('1','$comma_separated')";

if ($connection->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $connection->error;
}

?>

Here like_data field I have taken varchar.

It is giving sql syntax error.

Alimon Karim
  • 4,354
  • 10
  • 43
  • 68

3 Answers3

2

You have to put ` around LIKE since it's a reserved word in SQL! So it should look like this:

INSERT INTO `like`
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • Voted you up because you were first to spot the issue and put it in a comment even though your answer came late and that the question is now closed :) – Hanky Panky Dec 03 '14 at 06:56
  • @Hanky웃Panky Thanks :D I always forget to mark a question as a Duplicate! And if i answered it i'm like asking my self: Wait, wasn't this question asked before. (Have to remember my self more to thing about this) – Rizier123 Dec 03 '14 at 06:58
1

LIKE is a MySQL reserved keyword.

MySQL reserved Keywords

Solutions:

1) avoid using like as table name.

2) Add Database name before like: dbName.like

3) Add a backtick (`) against the like :

INSERT INTO `like`(user_id,like_data) VALUES ('1','$comma_separated')
Pupil
  • 23,834
  • 6
  • 44
  • 66
1

You must not use the keywords for the table name

if you use them you just have to have them in backticks like this

INSERT INTO `like` (user_id,like_data) VALUES ('1','$comma_separated')
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95