简体   繁体   中英

Problem: Part of the PHP exercise does not run correctly

I created a section for editing. When I edit the information and click the save button, the information is not saved and the header section does not display completely.

<?php
if (isset($_POST['submit_btn']))
{
    $id = $_POST['id'];
    $fn = trim($_POST['name']);
    $ln = trim($_POST['lastname']);
    $age = trim($_POST['age']);
    $q = "UPDATE `users` SET `fn` = '$fn',
             `ln` = '$ln', 
             `age` = '$age'
            WHERE id = '$id'";
    mysqli_query($dbconnect,$q);

    if (mysqli_affected_rows($dbconnect) > 0)
        redirect("?msg=ok&id=**$id**");
    else
        redirect("?msg=error&id=**$id**");
}
else
    echo ("Not In If(isset)");
?>

<form action="" method="post">
    <label for="name">FirstName:</label>
    <input type="text" name="name" id="name" value="<?php echo $row['fn']?>">
    <br>
    <label for="lastname">LastName:</label>
    <input type="text" name="lastname" id="lastname" value="<?php echo $row['ln']?>">
    <br>
    <label for="age">Age:</label>
    <input type="text" name="age" id="age" value="<?php echo $row['age']?>">
    <br>
    <input type="submit" name="submit_btn" value="Save">
    <a href="index2.php">
        Back
    </a>
</form>
</body>

Bold sections do not work here. Below is a picture of the result:

在此处输入图像描述

In the link that I specified, after clicking on save the ID will not be displayed and all the information filled in the forms will be lost.

Sorry if the result is styleless and boring and I just created this page to practice php Thank you for being responsive

You are mistaking a POST request with a GET request. Part, which appears in the URL is sent to the webserver in GET request.

Your form is submitting POST request to the webserver, logic in the code does the same, but you are trying to display information from url (GET).

Please check the examples in php.net:

You can take an example with GET request variable below, however, be careful with trusting the "end client" and always prepare your statements, which you send to your database to execute queries.


if (isset($_GET['submit']))
{
    $number = $_GET['number'];
    
    echo $number
        ? "Number which was submitted: $number <br>"
        : 'Number is not set';
} else {
    echo 'Form has not been yet submitted';
}
?>

<form action="" method="get">
    <input type="number" name="number" placeholder="Number">
    <input type="submit" name="submit" value="Save">
</form>

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