Jump to content

Can't add to database


unplugged_web
 Share

Recommended Posts

Please help me, I've no idea what's going wrong here. I'm trying to add to a database. This works:

mysql_query("INSERT INTO `images` (`id`, `class`, `foreign_id`, `title`, `filename`, `created`, `modified`, `order`, `category`) VALUES (NULL,'Bio','99','test','image.jpg',CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,'0','0')"); 

But this doesn't:

mysql_query("INSERT INTO `images` (`id`, `class`, `foreign_id`, `title`, `filename`, `created`, `modified`, `order`, `category`) VALUES (NULL, '$_POST[class]'', '$_POST[foreign_id]', '$_POST[name]', '$_POST[pic]', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, '$_POST[order]', '$_POST[category]')"); 

All of the fields have the correct values, but for some reason it just won't let me do it. I've sent three days trying to get it sorted, but I think I'm just going round in circles now.

Link to comment
Share on other sites

Did you add code to check for errors from MySQL over those three days? I see one syntax error, but there might also be unescaped quotes in the data. Use mysql_error to check for errors, and print out the query to verify what it says. There's no reason to spend 3 days debugging a syntax error when you can have MySQL tell you exactly what the problem is.

Link to comment
Share on other sites

it will not report errors in mysql implictly unless you tell it to do so. what php myadmin do is display errors when there is error in mysql. mysql_error() is the function which tells about the errors.

Link to comment
Share on other sites

I can't believe nobody's mentioned escaping data. You are using the POST variables directly in the query, which is a very bad idea. Google "SQL injection" if you want to see why it's a bad idea. You should be using mysql_real_escape_string to escape those values before using them in your query.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...