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')"); } Link to comment https://forums.phpfreaks.com/topic/214579-if-no-image-insert-image-with-value/ 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'; } Link to comment https://forums.phpfreaks.com/topic/214579-if-no-image-insert-image-with-value/#findComment-1116566 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')"); Link to comment https://forums.phpfreaks.com/topic/214579-if-no-image-insert-image-with-value/#findComment-1116571 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')"); Link to comment https://forums.phpfreaks.com/topic/214579-if-no-image-insert-image-with-value/#findComment-1116595 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')"); Link to comment https://forums.phpfreaks.com/topic/214579-if-no-image-insert-image-with-value/#findComment-1116598 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] Link to comment https://forums.phpfreaks.com/topic/214579-if-no-image-insert-image-with-value/#findComment-1116599 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 Link to comment https://forums.phpfreaks.com/topic/214579-if-no-image-insert-image-with-value/#findComment-1116607 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]) Link to comment https://forums.phpfreaks.com/topic/214579-if-no-image-insert-image-with-value/#findComment-1116616 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 Link to comment https://forums.phpfreaks.com/topic/214579-if-no-image-insert-image-with-value/#findComment-1116629 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.