2

I'm updating my add post script but now I've a problem inserting things into mysql.

When I use this INSERT INTO code:

                    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                    $id=$_POST['id'];
                    $title=$_POST['title'];
                    $pic=$_POST['pic'];
                    $youtube=$_POST['youtube'];
                    $cat=$_POST['cat'];
                    $NSFW=$_POST['NSFW'];



                    // insert data to mysql
                    $sql = "INSERT INTO post(id, title, pic, youtube, cat, NSFW)VALUES('$id', '$title', '$pic', '$youtube', '$cat', '$NSFW')";
                    $result=mysql_query($sql);
                    }

I can INSERT things into my mysql, but I've added a new row which is called add

So If I add $add=$_POST['add']; and I change $sql

$sql = "INSERT INTO post(id, add, title, pic, youtube, cat, NSFW)VALUES('$id', '$add', '$title', '$pic', '$youtube', '$cat', '$NSFW')";

This doesn't want to insert anything any more.

EDIT
Thank you guys, didn't know about the 'add' function of mysql my bad laughed my self haha:P Didn't know since I'm new to this

NoobEditor
  • 15,563
  • 19
  • 81
  • 112

4 Answers4

0

ADD is a reserved word in MySQL. You need to enclose them in backticks.

Like this..

$sql = "INSERT INTO post(id, `add`, title, pic, youtube, cat, NSFW) VALUES ('$id', '$add', '$title', '$pic', '$youtube', '$cat', '$NSFW')";
                             ^   ^ //<--- Like that.
Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0

Add is a reserved keyword..

$sql = "INSERT INTO post(`id`,`add`,`title`,`pic`,`youtube`,`cat`,`NSFW`)
VALUES ('$id',
        '$add',
        '$title',
        '$pic',
        '$youtube',
        '$cat',
        '$NSFW')";
Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
0

Add needs to be prefixed and suffixed with ` to work as column name, otherwise its interpreted as keyword

$sql = "INSERT INTO post(id, `add`, title, pic, youtube, cat, NSFW)VALUES('$id', '$add', '$title', '$pic', '$youtube', '$cat', '$NSFW')";
rozklad
  • 442
  • 6
  • 11
0

Try this

$sql = "INSERT INTO post(`id`, `add`, `title`, `pic`, `youtube`, `cat`, `NSFW`) VALUES ('$id', '$add', '$title', '$pic', '$youtube', '$cat', '$NSFW')"

will works I hope.

Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
Mahendra Jella
  • 5,450
  • 1
  • 33
  • 38