首页 > 解决方案 > 带有while循环的PHP表单

问题描述

我在生成可以基于 SQL 查询打印表单的页面时遇到了一些麻烦。在这种情况下,我需要执行查询以列出名称为 Porcac1x 的表的所有行。内容需要是显示当前变量值并且可以更新的输入字段。这正是我卡住的地方。如何创建一个基于 php while 循环的可变表单?使用附加的代码,我可以列出内容并显示所有变量,但是在创建表单操作以更新值时遇到了麻烦。我想明确一点,我不关心安全性,因为代码将在本地环境中运行,我是唯一可以访问的人。

这是当前输出在此处输入图像描述,但是保存按钮当然不起作用

<html>
 <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="description" content="$1">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" type="text/css" href="style.css">

    <title>test page</title>
</head>
    
    <body><form action="" method="post">
        <?php
        
        
                                $servername = "localhost";
                                $username = "root";
                                $password = "root";
                                $dbname = "root";

                                // Create connection
                                $conn = new mysqli($servername, $username, $password, $dbname);
                                // Check connection
                                if ($conn->connect_error) {
                                  die("Connection failed: " . $conn->connect_error);
                                }

                                $sql = "SELECT `id`, `title`, `amount` FROM `expenses` WHERE name='Porcac1x';";
                                $result = $conn->query($sql);

                                if ($result->num_rows > 0) {
                                  // output data of each row
                                  while($row = $result->fetch_assoc()) {
                                    echo "ID: <input type='text' name='".$row["id"]."' value='".$row["id"]."'> Title: <input type='text' name='".$row["title"]."' value='".$row["title"]."'> Amount: <input type='text' name='".$row["amount"]."' value='".$row["amount"]."'> <button type='submit' name='save'>Save</button><br>";
                                  }
                                } else {
                                  echo "0 results";
                                }                               
                                

                              
                                   if(isset($_POST['save'])){
                                       
                                    $myID = $_POST["id"];//??? < Issue
                                    $myTitle = $_POST["title"];//??? < Issue
                                    $myAmount = $_POST["amount"]; //??? < Issue
                                    
                                    echo $myID;
                                    echo $myTitle;
                                    echo $myAmount;
                                       
                                       $sqlUpdate = "UPDATE expenses SET title='$myTitle', amount ='$myAmount' WHERE id='$myID';";
                                       
                                       echo $sqlUpdate;
                                       
                                        if ($conn->multi_query($sqlUpdate) === TRUE) {
                                            echo "Record updated successfully";
                                          $risposta= "Record updated successfully";
                                        } else {
                                          echo "Error updating record: " . $conn->error;
                                           $risposta= "Error updating record: " . $conn->error;
                                        }

                                        $conn->close(); 
                                   }                                        
                                   
                                ?>
                            </form>
                                
                                </body>
                                </html>

标签: phpmysqlforms

解决方案


就像我在评论中所说的那样,你需要数组表示法。(或每行 1 个表格)

这是每行 1 个表单元素的解决方案:

<html>
 <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="description" content="$1">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" type="text/css" href="style.css">

    <title>test page</title>
</head>

 <body>                 
     <?php

        $servername = "localhost";
        $username = "root";
        $password = "root";
        $dbname = "root";

        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        $sql = "SELECT `id`, `title`, `amount` FROM `expenses` WHERE name='Porcac1x';";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            // output data of each row

            while($row = $result->fetch_assoc()) {
                echo '<form method="post">';
                echo "ID: <input type='text' name='id' value='".$row["id"]."'> Title: <input type='text' name='title' value='".$row["title"]."'> Amount: <input type='text' name='amount' value='".$row["amount"]."'> <button type='submit' name='save'>Save</button><br>";
                echo '</form>';
            }
        } else {
            echo "0 results";
        }

        if(isset($_POST['save'])){

            $myID = $_POST["id"];//??? < Issue
            $myTitle = $_POST["title"];//??? < Issue
            $myAmount = $_POST["amount"]; //??? < Issue

            echo $myID;
            echo $myTitle;
            echo $myAmount;

            $sqlUpdate = "UPDATE expenses SET title='$myTitle', amount ='$myAmount' WHERE id='$myID';";

            echo $sqlUpdate;

            if ($conn->multi_query($sqlUpdate) === TRUE) {
                echo "Record updated successfully";
                $risposta= "Record updated successfully";
            } else {
                echo "Error updating record: " . $conn->error;
                $risposta= "Error updating record: " . $conn->error;
            }

            $conn->close();
        }



        ?>
    </body>
</html>

推荐阅读