Jump to content

Error inserting data into table


nloding

Recommended Posts

I'm just trying to insert some form data into my table.  I am successfully connection to the database.  The table name is "pieces".  It has the following fields in the following order:

id / title / auth / pages / genre / format / desc / time / filename

id is auto_incremented, time is using the NOW() feature, so I'm putting data into the other fields.  Here's my code:

[code]
//insert them variables into that there db
$query = "INSERT INTO pieces (title, auth, pages, genre, format, desc, filename) VALUES ('$title', '$author', '$pages', '$genre', '$format', '$desc', '$file')";
$result = $mysqli->query($query);

//check to make sure the result is true
if($result) {
echo "<p><span class=\"bold\"><u>Upload was successful!</u> ".$title." is now part of the library!</span></p>\n";
}
else {
echo "<p>Something went wrong: ".$mysqli->error."</p>";
exit;
}
[/code]

And here's the error I get:

[quote]Something went wrong: 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 'desc, filename) VALUES ('Test 4', 'Test 4', '4', 's_nonfiction', 'word', 'Test t' at line 1[/quote]

I've double-checked all the spellings, variable names, and usage of apostrophes, and all seems to be in order.  What am I missing?

PS: Why isn't my code all colorful and exciting!?
Link to comment
https://forums.phpfreaks.com/topic/35997-error-inserting-data-into-table/
Share on other sites

[code]
<?php

//insert them variables into that there db
$result = mysql_query( "INSERT INTO pieces (title, auth, pages, genre, format, desc, filename) VALUES ('$title', '$author', '$pages', '$genre', '$format', '$desc', '$file')");


//check to make sure the result is true
if($result) {
echo "<p><span class=\"bold\"><u>Upload was successful!</u> ".$title." is now part of the library!</span></p>\n";
}
else {
echo "<p>Something went wrong: ".$mysqli->error."</p>";
exit;
}

?>
[/code]
Thanks poco, but that doesn't help.  I'm using mysqli, and what you typed was the exact same thing I already had except not using mysqli.

So what's wrong with my query?

EDIT: Is it because I skip the id and time fields?  Would adding "NULL" and "NOW()" to the INSERT query itself fix this?  Let's try while I wait for a reply!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.