简体   繁体   中英

How to display back the correct message upon inserting sql query in php?

I'm currently stuck because I can successfully update my queries when I enter a value and then submit, yet the end result is the output "The message "Record updated successfully" even before I click the submit button. To add on, I tried adding in a counter to only echo out the success message once it has reached a certain quota, but it doesn't help, please help! Thank you so much.

What I'm trying to do is ONLY output the "Record updated successfully" after I click the submit button, not having the message appearing straight even before I click the submit button.

HTML CODE

<div class = "form-group">
    Book Title 
    <input type="text" class="form-control" id="bookTitle" name="bookTitle" 
        value= "<?php if (isset($_POST['bookTitle'])) echo($_POST['bookTitle']); ?>" 
        placeholder="" required>

PHP Code

<?php
    $sql = "SELECT * FROM `allbooks` WHERE `allbooks`.`id` = $id ";
    if ($result = mysqli_query($connection, $sql)) {
        while ($row = mysqli_fetch_assoc($result)) {
            if ($connection) {
                if (isset($_POST['bookTitle'])) {
                    $bookTitle = mysql_real_escape_string($_POST['bookTitle']);
                    $_SESSION['bookTitle'] = $bookTitle;
                }
            }

            $sql = "UPDATE `allbooks` SET `allbooks`.bookTitle = '$bookTitle', WHERE `allbooks`.id = $id";
            if (mysqli_query($connection, $sql)) {
                echo "Record updated successfully";
            } else {
                echo "Please enter the correct values for the boxes!";
            }
        }
    }
?>

我通过在If(mysqli_query($ connection,$ sql)):)之后添加另一个计数器和一条if语句来解决问题,无论如何,谢谢!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM