2017-02-28 120 views
0

嗨,大家好,我有一个代码,我必须选择一个图像,但是如果我不想更新数据库中现有的图像呢? 这是对我的系统中的配置文件的更新。如何从数据库更新数据,但不更新php中的图像

我正在使用mysql数据库和mysqli准备语句 你能帮我吗?

这是我的工作代码。

<?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(); 
    } 
} 
?> 

这里是我的html文件

<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> 
+0

你想在这里? –

+0

@OmarElDon mmm。如果我不选择图像,则数据库中的图像仍然是现有图像。 – sauce

+0

只有在选择图像文件时才更新数据if(isset($ _ FILES ['image'] ['name'])){'。那么为什么你不想更新图像 –

回答

-1

试试这个:

<?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(); 
} 
?> 

要解释一下我加入简单的话:那最初设置为空$ sql_img。如果发布图像,则会更改为图像位置字符串。并被添加到最终的SQL脚本

+0

警告:mysqli_stmt :: bind_param():变量的数量不匹配准备语句中的参数数 – sauce

+0

@sauce再试一次吗?我编辑的代码有一个错误 – JapanGuy

+0

:(它仍然是相同的 – sauce