Hi there guys, I seem to be having a silly issue and I cant figure out why, my code is working fine for uploading and storing the image name in the datebase, but my issue come about when the form gets sumitted with no file selected I do want this to happen! but I would like a hidden field to be used as the file name that is update the database with "spacer.png"

if $photoname = empty then use this string "spacer.jpg"

Carry on and insert into datebase

Any help would be appreciated?

thanks in advance

<?php


//This is the directory where images will be saved
$target = "memberFiles/$userid/blessings/";
$target = $target . basename( $_FILES['photo']['name']);
$spacer = "spacer.png";
//This gets all the other information from the form
$photoname=($_FILES['photo']['name']);

//Connect to the database through our include 
include_once "connect_to_mysql.php";



// Place Session variable 'id' into local variable
$id = $_SESSION['id'];
// Process the form if it is submitted
if ($_POST['blessing1']) {


    $blessing1 = mysql_real_escape_string(     $_POST['blessing1']);
    $userid = $_POST['userid'];

    $sql1 = mysql_query("INSERT INTO blessings (userid, blessing1, photo, datepublished) 
        VALUES('$userid','$blessing1','$photoname', now())") or die (mysql_error());



//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {

//Gives and error if its not
header('Location: view_blessings.php');
}

        header('Location: view_blessings.php');

exit();
} // close if post
?>

Recommended Answers

All 2 Replies

i didn't use php for a long time but i guess if you put this code in line 20 would work it out if you mean with your question to change the empty name with 'spacer.png'

if ($_FILES['photo']['name']!=""){$photoname=$_FILES['photo']['name'];}
else {$photoname=$spacer;}

Thank you, that worked perfectly. I really appreciate you help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.