[英]Image reselect php
我有一张桌子,每一行都有一个图像和一些文本。 当前,当我在不选择图像的情况下更新内容时,图像的数据库字段将被清除。 但是,如果没有选择图像,我想保留旧图像。
我该怎么做?
需要注意的是,我知道不推荐使用mysql_ *函数。
<?php
include("db/db.php");
$select_db = "select * from aboutus WHERE id=1";
$run_news = mysql_query($select_db);
while ($row = mysql_fetch_array($run_news)) {
$id = $row['id'];
$image = $row['image'];
$content = $row['content'];
}
?>
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Update About Content</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form role="form" method="post" action="aboutcontent.php?id=1" enctype="multipart/form-data">
<div class="box-body">
<div class="form-group">
<label for="exampleInputFile">Reselect Image *(H=530px, W=800px)</label>
<input type="file" name="user_image" id="exampleInputFile">
</div>
<div class="form-group">
<label >Content</label><br>
<textarea name="content" class="tinymce" class="form-control" rows="15"><?php echo $content; ?></textarea>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" name="update" class="btn btn-primary">Update</button>
</div>
</form>
</div>
<?php
include("db/db.php");
// Code for UPDATE button
if (isset($_POST['update'])) {
$content = $_POST['content'];
$imgFile = $_FILES['user_image']['name'];
$tmp_dir = $_FILES['user_image']['tmp_name'];
$imgSize = $_FILES['user_image']['size'];
if ($imgFile) {
$upload_dir = 'images/about/'; // upload directory
$imgExt = strtolower(pathinfo($imgFile, PATHINFO_EXTENSION)); // get image extension
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
$userpic = rand(1000, 1000000) . "." . $imgExt;
if (in_array($imgExt, $valid_extensions)) {
if ($imgSize < 5000000) {
unlink($upload_dir . $row['image']);
move_uploaded_file($tmp_dir, $upload_dir . $userpic);
}
else {
$errMSG = "Sorry, your file is too large it should be less then 5MB";
}
}
else {
$errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
}
else {
// if no image selected the old image remain as it is.
$userpic = $row['image']; // old image from database
}
// if no error occured, continue ....
$sql = "UPDATE aboutus SET image='$userpic', content='$content' WHERE id=1";
$query = mysql_query($sql);
if (!$query) {
die('Invalid query: ' . mysql_error());
}
else {
echo "<script>alert('Successfully Updated!!!'); window.location='index.php?aboutcontent'</script>";
}
}
?>
既然我还不能发表评论,那么当您提交的图像中没有图像时,您将进入if或else语句(在每个部分中放置一个die(某些内容))以弄清楚这一点。 如果您没有做到这一点,请尝试:
//initialize error message
$errMSG = '';
//this error means 'There is no error, the file uploaded with success'
if($_FILES['user_image']['error'] == 0){
//rest of logic to get the filename and move files stuff
}
else{
$userpic = $row['image'];
}
//you dont want to update database if there are errors
if(errMSG = ''{
$sql = "UPDATE aboutus SET image='$userpic', content='$content' WHERE id=1";
}
或仅进行两个不同的数据库调用,一个用于拥有图像时,而另一个用于不具有图像时
if($_FILES['user_image']['error'] == 0){
//rest of logic to get the filename and move files stuff
//if everything else is true (has filename and correct file size)
//you dont want to update database if there are errors
if(errMSG = ''{
$sql = "UPDATE aboutus SET image='$userpic', content='$content' WHERE id=1";
}
$sql = "UPDATE aboutus SET image='$userpic', content='$content' WHERE id=1";
}
else{
$userpic = $row['image'];
$sql = "UPDATE aboutus SET content='$content' WHERE id=1";
}
通过检查文件是否已提交,可以轻松解决该问题:
if(!empty($userpic)){
// SQL update here
} else {
// No file submitted so don't update
}
之所以获得空的mysql字段,是因为您正在使用一个空的变量更新该字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.