简体   繁体   中英

Cannot Update Image In PHP&MYSQL

hi guys i am working on a project where i have a student_edit.php file which updates the students details all my data is updated successfully but there is one problem when i update lets say only two fields and not all then image is blanked and my fields updated successfully.

what i am doing is that i am showing student picture and besides it i have another input fields to browse image to update image. What i want is simple that if i am not updating image instead i update other fields image should be still there and not become blank.

Necessary code snippets is like that:

<?php

    $file_name = $_FILES['picture']['name'];
    $tmp_name  = $_FILES['picture']['tmp_name'];

    if (copy($tmp_name, "images/" . $file_name)) {
        $picture = "images/" . $file_name;
    }

    if (!isset($_POST['picture'])) {
        $res = mysql_query("UPDATE student SET `id`='$id',`branch_id`='$branch_id',`class_id`='$class_id',`section_id`='$section_id',`roll_number`='$roll_number',`student_name`='$student_name',`father_name`='$father_name',`dob`='$dob',`student_address`='$student_address',`gender`='$gender',`status`='$status',updated=now() WHERE id='$id'") or die(mysql_error());
    }
    else {
        $res = mysql_query("UPDATE student SET `id`='$id',`branch_id`='$branch_id',`class_id`='$class_id',`section_id`='$section_id',`roll_number`='$roll_number',`student_name`='$student_name',`father_name`='$father_name',`dob`='$dob',`student_address`='$student_address',`gender`='$gender',`status`='$status',`picture`='$picture',updated=now() WHERE id='$id'") or die(mysql_error());
    }

?>

and their is picture area where i call pics

<p>
    <label for="picture"><strong>Picture:</strong> </label>
    <a href="#"><img src="<?php echo $rec['picture'];?>" width="100" height="100"/></a>
    <input name="picture" type="file" value="">

</p>

and their is picture area where i call pics

<p>
    <label for="picture"><strong>Picture:</strong> </label>
    <a href="#"><img src="<?php echo $rec['picture'];?>" width="100" height="100"/></a>
    <input name="picture" type="file" value="">

</p>

Just a quick guess by looking at your code, you are not escaping the value of $picture when you place it in the database, which could be why the image value in the database is not updating.

Also, don't use if(!isset($_POST['picture'])) , but try if (!isset($_FILES['picture']) || (isset($_FILES['picture']) && $_FILES['picture']['name'] == "") . The $_POST super global won't work, because files are sent via the $_FILES super global.

Putting it all together:

$file_name = $_FILES['picture']['name'];
$tmp_name = $_FILES['picture']['tmp_name'];

if (!isset($_FILES['picture']) || (isset($_FILES['picture']) && $_FILES['picture']['name'] == "")) {
  $res = mysql_query("UPDATE student SET `id`='$id',`branch_id`='$branch_id',`class_id`='$class_id',`section_id`='$section_id',`roll_number`='$roll_number',`student_name`='$student_name',`father_name`='$father_name',`dob`='$dob',`student_address`='$student_address',`gender`='$gender',`status`='$status',`picture`='$picture',updated=now() WHERE id='$id'") or die(mysql_error());
} else {
  copy($tmp_name, "images/" . $file_name)

  $picture = mysql_real_escape_string("images/".$file_name);
  $res = mysql_query("UPDATE student SET `id`='$id',`branch_id`='$branch_id',`class_id`='$class_id',`section_id`='$section_id',`roll_number`='$roll_number',`student_name`='$student_name',`father_name`='$father_name',`dob`='$dob',`student_address`='$student_address',`gender`='$gender',`status`='$status',updated=now() WHERE id='$id'") or die(mysql_error());
}

Also, I REALLY recommend that you use the MySQLi extension: http://php.net/manual/en/book.mysqli.php

The above code is totally untested, but should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM