colleyboy Posted September 20, 2010 Share Posted September 20, 2010 Hi guys and gals , got a minor problem. I have a table in which i want the "photo" column to have a default value of "noimage.jpg". I set the default value to "noimage.jpg" and put "as defined" too. but when i fill the form in and leave the upload field blank it doesnt show the noimage.jpg as it should and in the mysql table it leaves it blank and not with default value. Here is the inserts.php which adds the data to the mysql table. Can you help please. <CENTER><B>Vehicle Added</B></CENTER> <BR> <?php mysql_connect("localhost", "wormste1_barry", "barry") or die(mysql_error()); mysql_select_db("wormste1_barry") or die(mysql_error()); $CarName = mysql_real_escape_string(trim($_POST['CarName'])); $CarTitle = mysql_real_escape_string(trim($_POST['CarTitle'])); $CarPrice = mysql_real_escape_string(trim($_POST['CarPrice'])); $CarMiles = mysql_real_escape_string(trim($_POST['CarMiles'])); $CarDescription = mysql_real_escape_string(trim($_POST['CarDescription'])); $pic = mysql_real_escape_string(trim($_FILES['uploadedfile']['name'])); $target_path = "images/"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; echo "<br />"; } else{ echo "There was an error uploading the file, please try again!"; } mysql_query("INSERT INTO cars (CarName, CarTitle, CarPrice, CarMiles, CarDescription, photo) VALUES('$CarName', '$CarTitle', '$CarPrice', '$CarMiles', '$CarDescription', '$pic' ) ") or die(mysql_error()); echo "The vehicle data has been added!"; ?> Quote Link to comment Share on other sites More sharing options...
the182guy Posted September 20, 2010 Share Posted September 20, 2010 The mysql default value will only take effect if you don't include that field in the query. In your code, you include the field regardless, so if there was no file uploaded then your just inserting an empty string and so the default value is ignored. I would put the default value code in the PHP script rather than mysql for this. Use the file upload error codes to check if a file was uploded, and also to check if there were any problems. See http://php.net/manual/en/features.file-upload.errors.php 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.