Jump to content

if no image insert image with value


Lisa23

Recommended Posts

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

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')");

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')");

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')");

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]

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

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

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.