sauce sauce -4 years ago 69
MySQL Question

How to update data from database but without updating image in php

Hi guys I have a code here wherein I have to select an image but what if I don't want to update the existing image in my database?
It is an update to a profile in my system.

I am using mysql database and mysqli prepared statement
Could you help me?

Here is my working code.

<?php
$id_alum = $_GET['id'];
include('db/database_configuration.php');
if(ISSET($_POST['save'])){

if(isset($_FILES['image']['name'])){
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
move_uploaded_file($_FILES["image"]["tmp_name"], "featured_image/". $_FILES["image"]["name"]);
$location = $_FILES["image"]["name"];
//edit.....get input values
if (empty($_POST['fullname'])){$fullname = 'NULL'; } else{ $fullname ="". mysqli_real_escape_string($conn, $_POST['fullname']) . "";}
if (empty($_POST['job'])){$job_title = 'NULL'; } else{ $job_title ="". mysqli_real_escape_string($conn, $_POST['job']) . "";}
if (empty($_POST['desc'])){$description = 'NULL'; } else{ $description ="". mysqli_real_escape_string($conn, $_POST['desc']) . "";}
if (empty($_POST['desc2'])){$description2 = 'NULL'; } else{ $description2 ="". mysqli_real_escape_string($conn, $_POST['desc2']) . "";}

$sql = "UPDATE `tblfeatured` SET image1=?, fullname=?, job_title=?, description=?, description2=? WHERE id_alum= $id_alum";

$stmt = $conn->prepare($sql);

// This assumes the date and account_id parameters are integers `d` and the rest are strings `s`
// So that's 5 consecutive string params and then 4 integer params

$stmt->bind_param('sssss', $location, $fullname, $job_title, $description, $description2);
$stmt->execute();

if ($stmt->errno) {
echo "FAILURE!!! " . $stmt->error;
}
else {
echo "<script>alert('Updated Successfully')</script>";
echo '<script>window.location = "featured_result.php"</script>';
}

$stmt->close();
}
}
?>


Here is my html file

<form method = "POST" action = "update_featured_alumni.php?id=<?php echo $id_alum; ?>" enctype = "multipart/form-data">
<label style="color: white; font-size: 12pt;">Change Image? Drag or click for an image</label>

<div id="uploader" onclick="$('#photo').click()">
<img src="" width="" />
<div class="pull-right">Existing Image<img src="featured_image/<?php echo $image;?>" width="150" height="150"></div>
</div>


<input type="file" name="image" id="photo"/>
<div id = "file_name" style="color: white;"></div>
<button class = "w3-btn w3-green w3-card-4" name = "save" title="Save"><font size="2"><span class = "fa fa-arrow-circle-o-down"></span> Save</font></button>

<br>
<font color="black">
<input class = "form-control" type = "text" name= "fullname" placeholder = "Fullname" style="margin-bottom: 15px;" value="<?php echo $fname;?>">



<input class = "form-control" type = "text" name= "job" placeholder = "Job Title & Workplace" style="margin-bottom: 15px;" value="<?php echo $job;?>">
<textarea id="txtArea" name="desc" onkeyup="resizeTextarea('txtArea')" data-resizable="true" placeholder="Alumni Description"><?php echo str_replace('\r\n', "\r\n", $desc); ?></textarea>
<textarea id="txtArea2" name="desc2" onkeyup="resizeTextarea('txtArea2')" data-resizable="true" placeholder="Alumni Description"><?php echo str_replace('\r\n', "\r\n", $desc2); ?></textarea>
</font>
<button class = "w3-btn w3-green w3-card-4" name = "save" title="Save"><font size="6"><span class = "fa fa-arrow-circle-o-down"></span> Save</font></button>
</form>

Answer Source

Try this:

<?php
$id_alum = $_GET['id'];
include('db/database_configuration.php');
if(ISSET($_POST['save'])){
    $sql_img = "";

    if($_FILES['image']['size'] > 0){
    $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $image_name = addslashes($_FILES['image']['name']);
    $image_size = getimagesize($_FILES['image']['tmp_name']);
    move_uploaded_file($_FILES["image"]["tmp_name"], "featured_image/". $_FILES["image"]["name"]);
    $location = $_FILES["image"]["name"];
    $location_db = addslashes($_FILES["image"]["name"]);
    $sql_img = "image1 = '".$location."', ";
    }
    //edit.....get input values
    if (empty($_POST['fullname'])){$fullname = 'NULL'; } else{ $fullname ="". mysqli_real_escape_string($conn, $_POST['fullname']) . "";}
    if (empty($_POST['job'])){$job_title = 'NULL'; } else{ $job_title ="". mysqli_real_escape_string($conn, $_POST['job']) . "";}
    if (empty($_POST['desc'])){$description = 'NULL'; } else{ $description ="". mysqli_real_escape_string($conn, $_POST['desc']) . "";}
    if (empty($_POST['desc2'])){$description2 = 'NULL'; } else{ $description2 ="". mysqli_real_escape_string($conn, $_POST['desc2']) . "";}
    $sql = "UPDATE `tblfeatured` SET ".$sql_img."fullname=?, job_title=?, description=?, description2=? WHERE id_alum= $id_alum";

    $stmt = $conn->prepare($sql);

    // This assumes the date and account_id parameters are integers `d` and the rest are strings `s`
    // So that's 5 consecutive string params and then 4 integer params

    $stmt->bind_param('ssss', $fullname, $job_title, $description, $description2);
    $stmt->execute();

    if ($stmt->errno) {
      echo "FAILURE!!! " . $stmt->error;
    }
    else {
        echo "<script>alert('Updated Successfully')</script>";
        echo '<script>window.location = "featured_result.php"</script>';
    }

    $stmt->close();
}
?>

To explain what i added in simple words: $sql_img that is initially set to empty. If an image is posted, it changes to the image location string. And gets added to the final sql script

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download