Skip to content
Advertisement

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 ”

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.

Advertisement

Answer

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')");
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement