Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm having a really weird issue as well as a stressful time trying to figure this out.

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.

The error mysqli_error reports is ALWAYS, even if there is nothing what so ever related to mysql (tried it nested further down the page, and on different pages, and on it's own page) on line 2, the following: 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 and I just cannot make sense of it.

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.

I really don't know what else to try. Any ideas would be great.

EDIT: Thank you all, it was the missing ) after the values! If only mysqli_error could have at least hinted towards this rather than giving me nothing of use.

share|improve this question
 
just try to put table name troubleshooting_files in `s –  lord_linus Jul 16 at 11:01
 
@lord_linus unfortunately, that still produces the same error –  Nathan Jul 16 at 11:03
 
Hey if you got the answer then please accept an answer and give upvotes to answers –  lord_linus Jul 16 at 11:12
 
Have to wait 2 minutes just FYI –  Nathan Jul 16 at 11:13
add comment

3 Answers

up vote 2 down vote accepted

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')");
share|improve this answer
 
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 –  Nathan Jul 16 at 11:10
add comment

you have missing brackets

   VALUES ('title', 'asset', 'image', 'cat', 'file') ");
                                                   ^^--------here missing bracket
share|improve this answer
 
Changing the test will make no difference as mysql_query failing will === false anyway. –  Anigel Jul 16 at 11:06
add comment

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|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.