简体   繁体   中英

Update table in database using mysql/php when profile is edited by user

I am using local phpmyadmin as my database(MYSQL), I am doing something like this and i have no idea why this code is not updating the table in database. I am able to fetch data from database and show it on required page and place.

<?php
    session_start();
    require('db.php');
    $id = session_id();
    $query = "SELECT * from new_record where id='" . $id . "'";
    $result = mysqli_query($con, $query) or die (mysqli_error());
    $row = mysqli_fetch_assoc($result);
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Update Record</title>
        <link rel="stylesheet" href="css/style.css"/>
    </head>
    <body>
        <div class="form">
            <h1>Update Record</h1>
            <?php
                $status = "";
                if (isset($_POST['new']) && $_POST['new'] == 1)
                {
                    $id = session_id();
                    $name = $_POST['name'];
                    $age = $_POST['age'];
                    $submittedby = $_SESSION['username'];
                    $update = "UPDATE new_record SET    name='" . $name . "', age='" . $age . "', submittedby='" . $submittedby . "' WHERE id='" . $id . "'";
                    mysqli_query($con, $update) or die(mysqli_error());
                    $status = "Record Updated Successfully. </br></br><a href='personal-profile.php'>View Updated Record</a>";
                    echo '<p style="color:#FF0000;">' . $status . '</p>';
                }else {
            ?>
            <div>
                <form name="form" method="post" action="">
                    <input type="hidden" name="new" value="1"/>
                    <input name="id" type="hidden" value="<?php echo $row['id']; ?>"/>
                    <p><input type="text" name="name" placeholder="Enter Name" required value="<?php echo $row['name']; ?>"/></p>
                    <p><input type="text" name="age" placeholder="Enter Age" required value="<?php echo $row['age']; ?>"/></p>
                    <p><input name="submit" type="submit" value="Update"/></p>
                </form>
                <?php } ?>
            </div>
        </div>
    </body>
</html>
  1. As mentioned in comments - take care of Injection issues.

  2. session_id() taken into your $id does not have rows in your table. There are no rows in your table matching the MySQL Column id to the current session_id(). You will still have your form displayed with no values for name and age. When you fill and submit, it need to update an existing row which is not available in your table.

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