从上一页php,sql获取值

时间:2017-02-26 15:59:05

标签: php html sql

我无法从update.php获取id的值,并将另一个页面提供给update2.php
这是我的update.php的代码

<form method = "post" action = "update2.php ?id=".$row['0'].">
        <p class = "head">Update Account Basic Info</p>
        <p class = "form">
        <input type = "text" class = "name" name = "f_name" placeholder = "First Name">
        <input type = "text" class = "name" name = "l_name" placeholder = "Last Name">
        <br>
        <input type = "text" class = "other" name = "email" placeholder = "Email Address">
        <br>
        <input type = "numeric" class = "other" name = "mob_no" placeholder = "Mobile Number">
        <br></br>
        <input type = "submit" name = "save" value = "Update Account">
        </p>
</form>
    <?php
        $con = mysql_connect("localhost","root","");
        mysql_select_db("dbfacebook", $con);

        $query = "SELECT id_no, f_name, l_name, email, mob_no FROM tblaccount WHERE id_no = '$_GET[id]'";
        $result = mysql_query($query);

        while($row = mysql_fetch_array($result)){
            echo "<p class = 'sub'>Current Account Basic Info</p>";
            echo "<p class = 'form'>ID Number: ".$row['0']."<br>";
            echo "First Name: ".$row['1']."<br>";
            echo "Last Name: ".$row['2']."<br>";
            echo "Email: ".$row['3']."<br>";
            echo "Mobile Number: ".$row['4']."</p>";
        }   
        mysql_close();
    ?>

这是我的update2.php代码

<?php
        $con = mysql_connect("localhost","root","");
        mysql_select_db("dbfacebook", $con);
        echo $_GET['id'];
        mysql_query("UPDATE tblaccount SET f_name = '$_POST[f_name]', l_name = '$_POST[l_name]', email = '$_POST[email]', mob_no = '$_POST[mob_no]' WHERE id_no = '$_GET[id]'");

        echo "<h1>Account Updated</h1>";
        mysql_close($con);
?>

我的问题是我在update2.php中的$ _GET [id]函数无法在update.php中获取id_no的值

1 个答案:

答案 0 :(得分:-1)

使用

时,您无法获得正确的结果
action = "update2.php ?id=".$row['0']."

因为,.$row['0'].不是php ..它只是HTML ..

相反,您必须使用

action = "update2.php?id=<?php echo $row['0']; ?>"

<强>更新

由于您收到Undefined variable: row错误,这表示$row为空。

因此,移动

$con = mysql_connect("localhost","root","");
        mysql_select_db("dbfacebook", $con);

        $query = "SELECT id_no, f_name, l_name, email, mob_no FROM tblaccount WHERE id_no = '$_GET[id]'";
        $result = mysql_query($query);

打开<form>之前的行。

即,现在,代码应该像

<?php
 $con = mysql_connect("localhost","root","");
        mysql_select_db("dbfacebook", $con);

        $query = "SELECT id_no, f_name, l_name, email, mob_no FROM tblaccount WHERE id_no = '$_GET[id]'";
        $result = mysql_query($query);
        while($row = mysql_fetch_array($result)){

    <form method = "post" action = "update2.php?id=<?php echo $row['0']; ?>">
}

?>