duanchigeng4313 2018-03-27 04:25
浏览 70

无法更新表单数据

I am trying to edit form data by displaying the previous saved data on the form and then update it. It shows the data on the form which is saved in database but when i enter the new data it does not get the id of the row. I echo the update query, it shows the changed values but it shows id equals to empty. Here is my code for edit record and update; Edit record is working but update isn't:

    <?php

        include('connection.php');

        $id = ''; 
        if( isset( $_GET['id'])) {
            $id = $_GET['id']; 
        }

        $udfname = mysql_real_escape_string($_POST["udfname"]);
        $udlname = mysql_real_escape_string($_POST["udlname"]);
        $udpwd = mysql_real_escape_string($_POST["udpwd"]);
        $udeml = mysql_real_escape_string($_POST["udeml"]);
        $udnum = mysql_real_escape_string($_POST["udnum"]);

        $query="UPDATE form
                SET fname = '$udfname', lname = '$udlname', pwd = '$udpwd', eml = '$udeml', num = '$udnum' 
                WHERE id='$id'";

        $res= mysql_query($query);
        if($res){
            echo "<p> Record Updated<p>";        
        }else{
            echo "Problem updating record. MY SQL Error: " . mysql_error();
        }
    ?>

Form for editing record:

    <?php
        include('connection.php');
        $id = (int)$_GET['id'];
        $query = mysql_query("SELECT * FROM form WHERE id = '$id'") or die(mysql_error());

        while($row = mysql_fetch_array($query)) {
            echo "";
            $fname = $row['fname'];
            $lname = $row['lname'];
            $pwd = $row['pwd'];
            $eml = $row['eml'];
            $num = $row['num'];        
        }
    ?>

<html>
    <head>
        <title>Edit</title>
        <script>
        '
        '
        Jquery code here
        '
        '
        </script>
    </head>
    <body>
        <form action="update.php" method="post">
            <input type="hidden" name="ID" value="<?=$id;?>">
            First Name: <input type="text" name="udfname" value="<?=$fname;?>"><br>
            Last Name: <input type="text" name="udlname" value="<?=$lname?>"><br>
            Password: <input type="text" name="udpwd" value="<?=$pwd?>"><br>
            Email: <input type="text" name="udeml" value="<?=$eml?>"><br>
            Contact Number: <input type="text" name="udnum" value="<?=$num?>"><br>
            <input type="Submit">
        </form>
    </body>
</html> 
  • 写回答

3条回答 默认 最新

  • doujia1988 2018-03-27 04:28
    关注

    At update time your form is submitted using POST request. So you need to get ID using POST method. So to get ID of hidden field change your code as below:

    $id = ''; 
    if( isset( $_POST['ID'])) {
        $id = $_POST['ID']; 
    }
    
    评论

报告相同问题?