简体   繁体   中英

Table will not update, however showing successful

I am new to MYSQL

When i try to update a database, the result is showing successful, but the item is not showing. Please can someone help. I have tried carrying it over, by turning the post into a session variable, but made no difference.

update_ac.php

                $name = $_POST['name'];
            $lastname = $_POST['review'];

            // Connect to server and select database.
            mysql_connect("$host", "$username", "$password")or die("cannot connect");
            mysql_select_db("$db_name")or die("cannot select DB");

            // update data in mysql database
            $sql="UPDATE reviews SET name='$name', review='$lastname' WHERE id='$id'";
            $result=mysql_query($sql);

            // if successfully updated.
            if($result){
            echo "Successful";
            echo "<BR>";
            echo "<a href='list_reviews.php'>View result</a>";
            }

            else {
            echo "ERROR";
            echo "Welcome ". $_POST['name']. "<br />";
            echo $lastname; 
            }

            ?> 

update.php

                // Connect to server and select database.
            mysql_connect("$host", "$username", "$password")or die("cannot connect");
            mysql_select_db("$db_name")or die("cannot select DB");

            // get value of id that sent from address bar
            $id=$_GET['id'];

            // Retrieve data from database
            $sql="SELECT * FROM $tbl_name WHERE id='$id'";
            $result=mysql_query($sql);

            $rows=mysql_fetch_array($result);
        ?>

        <?php include "includes/header.php"; ?>
                <div id="bodywrap">
                    <div id="leftcol">
                    <h1>Update the form</h1>

        <form name="form1" method="post" action="update_ac.php">
        <label>Name</label>
        <input name="name" type="text" id="name" value="<? echo $rows['name']; ?>">
        <br />
        <label>Review</label>
        <input name="lastname" type="text" id="lastname" value="<? echo $rows['review']; ?>" size="15">
        <br />
        <input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>">
        <br />
        <input type="submit" name="Submit" value="Submit">
        </form>

        <?php
        // close connection
        mysql_close();
        ?>
$name = $_POST['name'];
$lastname = $_POST['review'];

Did you notice you used $id and not $_POST['id'] or $_REQUEST['id'] or $_GET['id']? Missing this:

$id = $_POST['id'];

The SQL worked because it DID update on every id='' it found, so it was not an error.

Also, as others pointed out, use mysqli, mysql extension is dangerous and probably will be deprecated soon

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