donglijuan8227 2018-09-24 16:16
浏览 32
已采纳

在echo上查找打印变量$ email的不成功

HTML

<form name="display" action="update.php" method="POST" >
    <li>Account Email:<input type="text" name="find_email" /></li>
    <li><input type="submit" name="submit" /></li>
</form>

PHP

<?php

$db     = pg_connect("host=localhost port=5432 dbname=CS2102 user=xxx password=xxx");
$email = $_POST['find_email'];
$query = "SELECT * FROM user";
$result = pg_query($db, $query) or die("Cannot execute query: $query
");       // Query template
$row    = pg_fetch_row($result);        // To store the result row
$count = pg_query($db, "SELECT count(*) FROM user");
if($db) echo "db connected" . "num of data " . pg_num_rows($count) ." . $email;

if (isset($_POST['submit'])) {
            echo "<ul><form name='update' action='update.php' method='POST' >
            <li>New Password:</li> 
            <li><input type='text' name='new_password' value='$row[password]' /></li>
        <li><input type='submit' name='new' /></li>
        </form>
    </ul>";
}

$password = $_POST['new_password'];
$email = $_POST['find_email'];
echo $email;
if (isset($_POST['new'])) { // Submit the update SQL command
    echo $password; 
    $sql = "'UPDATE user SET password = '$password'' WHERE email = '$email'";
    echo $sql . ":)";

}

It prints:

'UPDATE user SET password = '555'' WHERE email := '':)

But the prior $email that I echo out went missing when I fill up the second form and click submit.

  • 写回答

1条回答 默认 最新

  • dongyanpai2701 2018-09-24 16:51
    关注

    Try adding the email in the update password form too. In PhP, the variable wont be saved if the page refreshes, so you have to save it and again send it to process further. Please have a look at the following update.

    P.S. Added hidden field in the second form to save the posted email from first form

    if (isset($_POST['submit'])) {
                echo "<ul><form name='update' action='update.php' method='POST' >
                <li>New Password:</li> 
                <input type='hidden' name='find_email' value='$_POST['find_email']' />
                <li><input type='text' name='new_password' value='$row[password]' /></li>
            <li><input type='submit' name='new' /></li>
            </form>
        </ul>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?