我正在为更新功能开发 php 页面



当页面加载时,它显示错误,idnameaddress未定义。这些变量处于编辑条件,但我不明白如何将这些值放在文本归档的"值"中进行编辑,因为这不起作用。主代码是这样的。 更新代码也没有更新值,但它显示您的记录已更新

    <?php
    include 'server.php';
    if (isset($_GET['edit'])) {
        $id = $_GET['edit'];
        $update = true;
        $record = mysqli_query($con, "SELECT * FROM info WHERE id=$id");
        if (count($record) == 1) {
            $n = mysqli_fetch_array($record);
            $name = $n['name'];
            $address = $n['address'];
        }
    }
    ?>
    <!-- Form -->
    <!DOCTYPE html>
    <html>
        <head>
            <title>CReate, Update</title>
            <link rel="stylesheet" type="text/css" href="style.css">
        </head>
        <body>
            <form method="post" action="php_code.php" >
                <input type="hidden" name="id" value="<?php echo $id; ?>">
                <input type="text" name="name" value="<?php echo $name; ?>">
                <input type="text" name="address" value="<?php echo $address; ?>">
                <div class="input-group">
                    <?php if ($update == true): ?>
                        <button class="btn" type="submit" name="update" >Update</button>
                    <?php else: ?>
                        <button class="btn" type="submit" name="submit" >save</button>
                    <?php endif ?>
                </div>
            </form>
            <!-- Display -->
            <?php
            $i = 1;
            $q = mysqli_query($con, "select*from info");
            while ($f = mysqli_fetch_array($q)) {
                ?>
                <table>
                    <th> sr N0</th>
                    <th> Name</th>
                    <th> address</th>
                    <th> Action</th>
                    <tr>
                        <td>   <?php echo $i; ?>  </td>
                        <td>   <?php echo $f['name']; ?>  </td>
                        <td>   <?php echo $f['address']; ?>  </td>
                        <td><a href="index.php?edit=<?php echo $f['id']; ?>" class="edit_btn" >Edit</a></td>
                    </tr>
                    <?php $i ++;
                }
                ?>
            </table>
        </body>
    </html>

<?php
include 'server.php';
if (isset($_POST['save']))
{
    $name= $_POST['name'];
    $add= $_POST['address'];
    $q = "INSERT into info (name,address) VALUES ('$name', '$add')";
    mysqli_query($con, $q);
    echo "inserted";
}
 if (isset($_POST['update']))
 {
    $uname= $_POST['name'];
    $uaddress= $_POST['address'];
    $q = "UPDATE info set name= '$uname', adress= '$uaddress' WHERE id = $id";
    mysqli_query($con, $q);
    echo "updated";
 }

?>

Make

if (count($record) == 1 ) {

if (mysqli_num_rows($record) == 1 ) {

最新更新