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 ''

12,864

Solution 1

The closing bracket for the VALUES is missing...

$try = mysqli_query($link,"INSERT INTO troubleshooting_files (`title`, `asset`, `image`, `category`, `file`)
VALUES ('title', 'asset', 'image', 'cat', 'file')");

Solution 2

You have missing brackets:

VALUES ('title', 'asset', 'image', 'cat', 'file') ");
                                                ^^--------here missing bracket

Solution 3

you missed a bracket for the values()

$try = mysqli_query($link,"INSERT INTO troubleshooting_files (`title`, `asset`, `image`, `category`, `file`)
VALUES ('title', 'asset', 'image', 'cat', 'file')");
Share:
12,864
Nathan
Author by

Nathan

Updated on June 04, 2022

Comments

  • Nathan
    Nathan almost 2 years

    Using a form, I was trying to insert data into my database. The initial SQL query had variables for values, but it didn't work. After trying a few things and using mysqli_error I wasn't able to find the problem. I then replaced all the variables with strings, still, my data does not insert.

    I've tried the below code nested amongst other bits of PHP, doesn't work. I tried it on a different page, didn't work. So I stripped everything and left just the mysql_connect link and the mysqli_query and it still will not insert.

    This is the MySQL error I am getting:

    error - 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 '' at line 2

    My code below is as follows:

    $link = mysqli_connect("host", "username", "password", "database") or die("cannot connect"));
    $try = mysqli_query($link,"INSERT INTO troubleshooting_files (`title`, `asset`, `image`, `category`, `file`)
    VALUES ('title', 'asset', 'image', 'cat', 'file'");
    //$try = mysqli_query($link,"INSERT INTO troubleshooting_users (`name`)
    //VALUES ('title'");
    if($try === false){
        echo 'error - ';
        echo mysqli_error($link);
    }   else{
            echo 'all good';
        }
    

    I have tried entering just one field, I tried without the `s in the field names, I tried a different table name and it doesn't work. phpMyAdmin inserts data into the tables fine. I also have PHP code in a different directory on the server that inserts data fine into this database, although that code still uses the deprecated mysql_query at the minute. I also have code that inserts rows fine on this server into a different database. I assume the database must be fine if PMA can insert data okay, and elsewhere in my script, other mysqli_query's work fine as I can fetch objects and update tables fine.

  • Anigel
    Anigel almost 11 years
    Changing the test will make no difference as mysql_query failing will === false anyway.
  • Nathan
    Nathan almost 11 years
    I actually can't believe that. But thank you, that was the problem! Wish mysqli_error could have made finding that slightly easier! As I saw your answer first I'll accept it