Lisa23 Posted September 28, 2010 Share Posted September 28, 2010 Hi i am trying to upload image and then image name inserted into mysql now i tried incase the user doesnt insert image to insert an automatic image name into the image field something like bellow the column name is image i tried after the sql insert an if stattment if no image insert xxx.jpg but didnt work help please mysql_query("INSERT INTO driversnew (id, name, location, date_of_birth, car_number, favourite_track, least_favourite_track, achievements, sponsors, email, image) VALUES ('$_POST[id]', '$_POST[name]', '$_POST[location]','$_POST[date_of_birth]', '$_POST[car_number]','$_POST[favourite_track]', '$_POST[least_favourite_track]','$_POST[achievements]', '$_POST[sponsors]','$_POST[email]', '$image_name','0')"); if ($image_name=='') { mysql_query("INSERT INTO driversnew (image) VALUES ('xxxx.jpg')"); } Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 28, 2010 Share Posted September 28, 2010 Put this before your query, then remove the second query entirely. $image_name = trim($image_name); if(empty($image_name)) { $image_name = 'xxxx.jpg'; } Quote Link to comment Share on other sites More sharing options...
joel24 Posted September 28, 2010 Share Posted September 28, 2010 for starters, you're inserting the image_name into the email field, not the image field and also, you need to wrap the values in {} when accessing an array value in quotes, and the index must be wrapped in single quotes i.e. $_POST[id] should be {$_POST['id']} for that code you need to verify an image is uploaded before you insert the row into the database, and do the changes to the code mentioned above... also it would probably be best to have a default value for that field (image), so that if no value is sent mysql automatically sets it to the default value. i.e. if ($image_name=='') { $image_name = "xxx.jpg"; } mysql_query("INSERT INTO driversnew (id, name, location, date_of_birth, car_number, favourite_track, least_favourite_track, achievements, sponsors, email, image) VALUES ('$_POST[id]', '$_POST[name]', '$_POST[location]','$_POST[date_of_birth]', '$_POST[car_number]','$_POST[favourite_track]', '$_POST[least_favourite_track]','$_POST[achievements]', '$_POST[sponsors]','$_POST[email]', '0')"); Quote Link to comment Share on other sites More sharing options...
Lisa23 Posted September 28, 2010 Author Share Posted September 28, 2010 i tried this way but didnt insert anything into the database if(isset($_POST['Submit']) && !$errors) { if ($image_name=='') { $image_name = "xxx.jpg"; } mysql_query("INSERT INTO driversnew (id, name, location, date_of_birth, car_number, favourite_track, least_favourite_track, achievements, sponsors, email, image) VALUES ('$_POST[id]', '$_POST[name]', '$_POST[location]','$_POST[date_of_birth]', '$_POST[car_number]','$_POST[favourite_track]', '$_POST[least_favourite_track]','$_POST[achievements]', '$_POST[sponsors]','$_POST[email]', '0')"); Quote Link to comment Share on other sites More sharing options...
Lisa23 Posted September 28, 2010 Author Share Posted September 28, 2010 this way insert the data but didnt insert the image value what going wrong if(isset($_POST['Submit']) && !$errors) { $image_name = trim($image_name); if(empty($image_name)) { $image_name = 'xxxx.jpg'; } mysql_query("INSERT INTO driversnew (id, name, location, date_of_birth, car_number, favourite_track, least_favourite_track, achievements, sponsors, email, image) VALUES ('$_POST[id]', '$_POST[name]', '$_POST[location]','$_POST[date_of_birth]', '$_POST[car_number]','$_POST[favourite_track]', '$_POST[least_favourite_track]','$_POST[achievements]', '$_POST[sponsors]','$_POST[email]', '$image_name','0')"); Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 28, 2010 Share Posted September 28, 2010 You really need to stop writing your queries so the query string is part of the query execution. Separate the string and store it in a variable, then use the variable in the execution. This makes debugging soooooo much easier, especially while you're still learning the basics. $query = "INSERT INTO driversnew (id, name, location, date_of_birth, car_number, favourite_track, least_favourite_track, achievements, sponsors, email, image) VALUES ('$_POST[id]', '$_POST[name]', '$_POST[location]','$_POST[date_of_birth]', '$_POST[car_number]','$_POST[favourite_track]', '$_POST[least_favourite_track]','$_POST[achievements]', '$_POST[sponsors]','$_POST[email]', '$image_name')"; mysql_query( $query ) or die( '<br>Query string: ' . $query . '<br>Created error: ' . mysql_error() ); [/email] Quote Link to comment Share on other sites More sharing options...
Lisa23 Posted September 28, 2010 Author Share Posted September 28, 2010 You really need to stop writing your queries so the query string is part of the query execution. Separate the string and store it in a variable, then use the variable in the execution. This makes debugging soooooo much easier, especially while you're still learning the basics. $query = "INSERT INTO driversnew (id, name, location, date_of_birth, car_number, favourite_track, least_favourite_track, achievements, sponsors, email, image) VALUES ('$_POST[id]', '$_POST[name]', '$_POST[location]','$_POST[date_of_birth]', '$_POST[car_number]','$_POST[favourite_track]', '$_POST[least_favourite_track]','$_POST[achievements]', '$_POST[sponsors]','$_POST[email]', '$image_name')"; mysql_query( $query ) or die( '<br>Query string: ' . $query . '<br>Created error: ' . mysql_error() ); [/email] whats the email at the end is that how i should put or u just misstyped?? and do u mean use in that way with the prvious if no image sorry sometimes u learn the easy way bt not the smartest way and strugle to keep up Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 28, 2010 Share Posted September 28, 2010 I don't know where that [/email] came from. That doesn't belong there. But for now, try what I posted and see if there are any errors. (without the [/email]) Quote Link to comment Share on other sites More sharing options...
joel24 Posted September 28, 2010 Share Posted September 28, 2010 after the query string put in $query = "INSERT INTO driversnew (id, name, location, date_of_birth, car_number, favourite_track, least_favourite_track, achievements, sponsors, email, image) echo $query; exit(); VALUES ('$_POST[id]', '$_POST[name]', '$_POST[location]','$_POST[date_of_birth]', '$_POST[car_number]','$_POST[favourite_track]', '$_POST[least_favourite_track]','$_POST[achievements]', '$_POST[sponsors]','$_POST[email]', '$image_name')"; mysql_query( $query ) or die( '<br>Query string: ' . $query . '<br>Created error: ' . mysql_error() ); so you can view the mysql query and debug Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.