早安。我的问题是在点击更新按钮后,编辑元素就像标题,日期,内容和图像一样,只有回显输出显示为“已成功更新”。我想要做的是点击更新按钮后,元素将保持在那里,它将回应那里。
单击更新按钮后,只显示输出“Successfully update”,编辑元素消失
这是edit2.php的php代码
<?php
include_once('connection.php');
$newsid = $_GET['news_id'];
if(isset($_POST['esubmit'])){
/* create a prepared statement */
if ($stmt = mysqli_prepare($con, "SELECT * FROM news WHERE news_id = ? LIMIT 1")) {
/* bind parameters */
mysqli_stmt_bind_param($stmt, "s", $newsid);
/* execute query */
mysqli_stmt_execute($stmt);
/* get the result set */
$result = mysqli_stmt_get_result($stmt);
/* fetch row from the result set */
$row = mysqli_fetch_array($result);
}
}
if(isset($_POST['update'])){
if($_FILES['image']['error'] == 0) {
$image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
move_uploaded_file($_FILES["image"]["tmp_name"],"img/" . $_FILES["image"]["name"]);
$newsimage="img/" . $_FILES["image"]["name"];
$title = $_POST['titles'];
$date = $_POST['dates'];
$content = $_POST['contents'];
$sql ="UPDATE news SET news_title ='$title', news_date ='$date', news_content = '$content', news_image ='$newsimage' WHERE news_id = '$newsid'";
mysqli_query($con, $sql);
echo "successfully updated";
}
else{
$title = $_POST['titles'];
$date = $_POST['dates'];
$content = $_POST['contents'];
$sql ="UPDATE news SET news_title ='$title', news_date ='$date', news_content = '$content' WHERE news_id = '$newsid'";
mysqli_query($con, $sql);
echo "successfully updated";
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php
if(isset($_POST['esubmit'])){
?>
<form method="post" enctype="multipart/form-data" action ="edit2.php?news_id=<?php echo $row['news_id']; ?>" >
Title<input type ="text" name ="titles" value="<?php echo $row['news_title']; ?>"/><br>
Date<input type ="text" name="dates" value="<?php echo $row['news_date']; ?>" /><br>
Content<textarea name="contents"><?php echo $row['news_content']; ?></textarea>
<input class="form-control" id="image" name="image" type="file" accept="image/*" onchange='AlertFilesize();'/>
<img id="blah" src="<?php echo $row['news_image']; ?>" alt="your image" style="width:200px; height:140px;"/>
<input type="submit" name="update" value="Update" />
</form>
<?php
}
?>
<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript">
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#image").change(function(){
readURL(this);
});
</script>
</body>
</html>
答案 0 :(得分:1)
问题在于这个if
块,
if(isset($_POST['esubmit'])){ ...
当您提交表单时,$_POST['esubmit']
将不会被设置,因此表单不会再次显示。所以你的if
块应该是这样的:
if(isset($_POST['esubmit']) || isset($_POST['update'])){ ...
总的来说,您需要按以下方式更改第一个和第三个if
块,
if(isset($_POST['esubmit'])){
/* create a prepared statement */
if ($stmt = mysqli_prepare($con, "SELECT * FROM news WHERE news_id = ? LIMIT 1")) {
/* bind parameters */
mysqli_stmt_bind_param($stmt, "s", $newsid);
/* execute query */
mysqli_stmt_execute($stmt);
/* get the result set */
$result = mysqli_stmt_get_result($stmt);
/* fetch row from the result set */
$row = mysqli_fetch_array($result);
/* get all values */
$title = $row['news_title'];
$date = $row['news_date'];;
$content = $row['news_content'];
$newsimage = $row['news_image'];
}
}
并且
if(isset($_POST['esubmit']) || isset($_POST['update'])){
?>
<form method="post" enctype="multipart/form-data" action ="edit2.php?news_id=<?php echo $newsid; ?>" >
Title<input type ="text" name ="titles" value="<?php if(isset($title)){ echo $title; } ?>"/><br>
Date<input type ="text" name="dates" value="<?php if(isset($date)){ echo $date; } ?>" /><br>
Content<textarea name="contents"><?php if(isset($content)){ echo $content; } ?></textarea>
<input class="form-control" id="image" name="image" type="file" accept="image/*" onchange='AlertFilesize();'/>
<img id="blah" src="<?php if(isset($newsimage)){ echo $newsimage; } ?>" alt="your image" style="width:200px; height:140px;"/>
<input type="submit" name="update" value="Update" />
</form>
<?php
}
从扩展讨论中
由于您可以将图片上传设为可选内容,因此您需要在处理表单的else
块中获取图片详细信息,以防用户未上传图片,如下所示:
if(isset($_POST['update'])){
if($_FILES['image']['error'] == 0) {
// your code
}else{
// your code
/* get the image details*/
if ($stmt = mysqli_prepare($con, "SELECT news_image FROM news WHERE news_id = ? LIMIT 1")) {
mysqli_stmt_bind_param($stmt, "s", $newsid);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_array($result);
$newsimage = $row['news_image'];
}
}
}