为什么之前输入的值没有显示在我的EDIT表单中?

时间:2014-03-12 10:56:36

标签: php forms netbeans crud

我正在研究PHP CRUD操作,我在PHP中创建了一个基本的编辑表单。我没有使用任何字段验证,我只想编辑信息。

我正在关注此tutorial

在编辑链接上单击用户后,他将被定向到以下用户应编辑其数据的表单。

这是代码

<?php
include_once './functions.php';
include_once './database.php';


function renderForm($firstName,$lastName,$age){
?>
<!DOCTYPE html>

<html>
    <head>
        <title>Edit</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">
    </head>
    <body>
        <form action="edit.php" method="post">
            First Name<input type="text" name="firstname" value="<?php $firstName ;?>"><br/>
            Last Name<input type="text" name="lastname" value="<?php $lastName ;?>"><br/>
            Age<input type="text" name="age" value="<?php $age ;?>"><br/>
            <input type="submit" name="submit" value="Edit">
            <a href="main_menu.php">Cancel</a>


        </form>
   <?php
}
   ?>

        <?php
        if (isset($_POST['submit'])) {
            $firstName = cleanData($_POST['firstname']);
            $lastName = cleanData($_POST['lastname']);
            $age = (int) $_POST['age'];
            $id = $_GET['id'];
            $query = "UPDATE basic ";
            $query.="SET first_name='$firstName',last_name='$lastName',age=$age ";
            $query.="WHERE id=$id";

            confirmQuery($query);
            closeDatabase();

        }else{
            $id=cleanData($_GET['id']);

            $query="SELECT * FROM basic WHERE id= {$id} ";
            $result=confirmQuery($query);

            $rows=  mysqli_fetch_assoc($result);
            $firstName=$rows['first_name'];
            $lastName=$rows['last_name'];
            $age=$rows['age'];

            renderForm($firstName, $lastName, $age);

        }
        ?>


    </body>
</html>

 //Additional information
//functions included in other files
function cleanData($input){
    global $connection;
    return mysqli_real_escape_string($connection,$input);
}

function confirmQuery($query){
    global $connection;
    $result=mysqli_query($connection, $query);
    if(!$result){
        return "Query failed : ".mysqli_error($connection);
    }
    else{
        return $result;
    }
}
function closeDatabase(){
    global $connection;
    mysqli_close($connection);
}
       //I have not included the file which I am using to 
//connect to the DB. I am sure there is no error with that file since it works 
//properly with other php files

我的编辑表单存在的问题是它没有显示以前输入的数据,只显示一个空白表单(类似于创建表单)。 (当我在上面提到的教程中运行演示时不会发生这种情况)

Netbenas IDE说,HTML输入标签内的变量似乎在其范围内未被使用。我搜索了这个问题,发现可以简单地忽略警告。

但我哪里出错了? 我很感激任何能够通过我的代码并向我显示错误的人。

谢谢你:)

1 个答案:

答案 0 :(得分:0)

我已在PHP code中将您的edit.php更改为以下代码使用。如果您有任何问题发表评论。

    <?php
include_once './functions.php';
include_once './database.php';

if (isset($_POST['submit'])) {
            $firstName = cleanData($_POST['firstname']);
            $lastName = cleanData($_POST['lastname']);
            $age = (int) $_POST['age'];
            $id = $_GET['id'];
            $query = "UPDATE basic ";
            $query.="SET first_name='$firstName',last_name='$lastName',age=$age ";
            $query.="WHERE id=$id";

            $r=mysql_query($query);
            if($r)
            {
                echo "Record updated";
            }

        }




            $id=$_GET['id'];

            $query="SELECT * FROM basic WHERE id='$id' ";
            $result=confirmQuery($query);

            $rows=  mysqli_fetch_assoc($result);
            $firstName=$rows['first_name'];
            $lastName=$rows['last_name'];
            $age=$rows['age'];          


?>
<!DOCTYPE html>

<html>
    <head>
        <title>Edit</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">
    </head>
    <body>
        <form action="edit.php" method="post">
            First Name<input type="text" name="firstname" value="<?php echo $firstName ;?>"><br/>
            Last Name<input type="text" name="lastname" value="<?php echo $lastName ;?>"><br/>
            Age<input type="text" name="age" value="<?php echo $age ;?>"><br/>
            <input type="submit" name="submit" value="Edit">
            <a href="main_menu.php">Cancel</a>


        </form>

    </body>
</html>