Jump to content

Default Value - MYSQL


colleyboy

Recommended Posts

Hi guys and gals :D,

 

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!";

?>

Link to comment
https://forums.phpfreaks.com/topic/213864-default-value-mysql/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/213864-default-value-mysql/#findComment-1113160
Share on other sites

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.