Reputation: 1543
My database have a profile table with image column. I want to set the default image using default value if the user are not inserting his image URL in the web form.
The column definition:
profile_image varchar(255) NOT NULL DEFAULT 'http://i.imgur.com/U.png'
I already inserted the default image URL in phpmyadmin but everytime I click submit at web form, every key in data at webform is stored in the profile table but the image column is empty.
I know I can do this with form by adding the image URL in web form but I want to do this via mysql.
This problem is solved because I insert the image at php insert but how do I use the null value to let mysql know I'm sending NULL? Is this the correct way of using Null?
$nl = NULL;
$query = "INSERT INTO `student` (
`name` ,
`emel` ,
`address` ,
`profile_image`
)
VALUES (
'$name', '$emel', '$add', '$nl');";
The above code won't work. The profile_image column is still empty.
Upvotes: 1
Views: 18533
Reputation: 13600
You need to do 2 things:
1.) specify a default value of the column for the image, maybe something like default.jpg
(I believe you've already done this)
2.) ensure your php script provides a null value
(not empty) or don't specify the value at all
during insertion/modification, so your mysql knows it needs to use the default value. Or you can handle the logic in your php script to provide the url for the default image. I'd avoid some complicated functions in SQL, because your logic may change over time and SQL server really shouldn't be responsible for handling application logic.
Upvotes: 4