Question

I know there's couple of topics about why INSERT INTO doesn't work, but I didn't find any solutions for me.

So the problem is that I want to insert new record to my table. The code is:

    $query = "INSERT INTO cms_galleryrsrc(_filename) VALUES ('$filename')";
    mysqli_query($db, $query);

MySQL table looks like: cms_galleryrsrc[_id (int, AUTOINCREMENT); _filename (varchar(255))]

And it just doesn't do anythig...

Notice: for example UPDATE, SELECT * FROM works fine. Please, help me :)

Was it helpful?

Solution

While you have a limit that isn't technically needed in this instance, your code could be cut down to;

// Backtick is optional most of the time, it's personal preference for me.
$query = "INSERT INTO cms_galleryrsrc (`_filename`) VALUES ('$filename')";
mysqli_query($db, $query);

This leaves one final question; Have you confirmed $filename holds a value?

MySQL could be rejecting the query if filename doesn't allow null values, but $filename being blank gives it a null.

echo 'FN: ' . $filename;
exit;

above the query, then run and check output.

EDIT: After the reply from the OP, I checked the documents for MySQL and _filename is a reserved word.

OTHER TIPS

After table name space is missing, try this it should work, & you don't need Limit clause

$query = "INSERT INTO cms_galleryrsrc (_filename) VALUES ('$filename')";

You have a limit 1 clause in your query which will cause MySQL to throw an error.

While it is possible to have a limit clause on an insert statement, the format you have won't work - you would have to have a query limited.

This following shows a simple insert with your code:

mysql> select * from test1;
+------+-------+
| id   | varry |
+------+-------+
|    1 | aaa   |
|    1 | NULL  |
+------+-------+
2 rows in set (0.03 sec)

mysql> insert into test1 values (2, 'ab') limit 1;
ERROR 1064 (42000): 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 'limit
 1' at line 1
mysql> insert into test1 values (2, 'ab');
Query OK, 1 row affected (0.07 sec)

mysql> insert into test1 values(2, 'ab');
Query OK, 1 row affected (0.00 sec)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top