大家好,我有一个代码,我必须选择一个图像,但如果我不想更新数据库中的现有图像怎么办? 它是我系统中个人资料的更新。
我正在使用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 :(得分:-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脚本