nethken nethken - 5 months ago 6
jQuery Question

PHP - Edit elements is gone after updating

Goodmorning. My problem is after clicking the update button the edit elements are gone like the title, date, content and the image only the echo output is shown "successfully updated". What i want to do is after clicking the update button the elements will stay there and it will echo there.

Here is my edit2.php
enter image description here

after i click the update button only the output "Successfully update" is shown the edit elements is gone
enter image description here

here is the php code for edit2.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>

Answer

The problem is because of this if block,

if(isset($_POST['esubmit'])){ ...

When you submit the form, $_POST['esubmit'] will be not get set and hence, the form won't get displayed again. So your if block should be like this:

if(isset($_POST['esubmit']) || isset($_POST['update'])){ ...

Overall, you need to change your first and third if blocks in the following way,

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'];
    }

}

And

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
}
Comments