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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.