【问题标题】:Printing text to textbox and input from sql database in php在php中将文本打印到文本框并从sql数据库输入
【发布时间】:2020-03-29 12:35:00
【问题描述】:

我想问是否有人可以帮助我解决这个问题。

我想从 SQL 数据库中预填充输入和文本框。我试了很多次都没有成功。

这是代码:

<?php
    $servername = "localhost";
    $username = "********";
    $password = "********";
    $dbname = "kucharka";

    $id = $_POST['id_recept'];

    $conn = mysqli_connect($servername, $username, $password, $dbname);
    $query = 'SELECT nazev, popis FROM recepty WHERE id = '.$id.'';
    $result = mysqli_query($conn, $query);

    if ($result) {
        while( $row = mysqli_fetch_array($result) ){
            echo "<li><label>Název</label></li>";
                echo "<input class='blue' type='text' name='nazev' placeholder='Název'>".$row['nazev']."</input>";
            echo "<li><label>Fotografie</label></li>";
                echo "<div class='foto'>";
                    echo "<input type='file' id='real-file' name='foto[]' hidden='hidden' multiple='multiple'>";
                    echo "<button type='button' id='custom-button' class='blue_foto'>Stiskněte</button>";
                    echo "<span id='custom-text'>Žádná fotografie.</span>";
                echo "</div>";
            echo "<li><label>Druh</label></li>";
                echo "<div class='select_custom'>";
                    echo "<select name='druh'>";
                            $conn = mysqli_connect($servername, $username, $password, $dbname);
                            $query = 'SELECT id, druh FROM druh';
                            $result = mysqli_query($conn, $query);

                            if ($result) {
                                while ($row = mysqli_fetch_array($result)) {
                                    echo "<option value=".$row['id'].">".$row['druh']."</option>";
                                }
                            }
                    echo "</select>";
            echo "</div>";
            echo "<li><label>Popis</label></li>";
                echo "<textarea name='text' placeholder='Popis'>".$row['popis']."</textarea>";
            echo "<li><input type='submit' name='submit' class='orange_input' value='Potvrďte'></li>";
        }
    }       
?>

提前谢谢你。

【问题讨论】:

  • 什么...到底是行不通。您将不得不帮助我们为您提供帮助
  • 你需要一个表单标签来表明你要提交任何东西。您不需要每次查询都连接到数据库。您的 sql 对 injection 开放。
  • 对于预填充输入使用 echo “”;

标签: php html sql arrays while-loop


【解决方案1】:

啊!您在 While 循环内有一个 While 循环!!

两者都在处理mysqli_fetch_array($result),因此内循环破坏了外循环中使用的$result

<?php
    $servername = "localhost";
    $username = "********";
    $password = "********";
    $dbname = "kucharka";

    $id = $_POST['id_recept'];

    $conn = mysqli_connect($servername, $username, $password, $dbname);
    $query = 'SELECT nazev, popis FROM recepty WHERE id = '.$id.'';
    $result = mysqli_query($conn, $query);

    if ($result) {
        while( $row = mysqli_fetch_array($result) ){
            echo "<li><label>Název</label></li>";
                echo "<input class='blue' type='text' name='nazev' placeholder='Název'>".$row['nazev']."</input>";
            echo "<li><label>Fotografie</label></li>";
                echo "<div class='foto'>";
                    echo "<input type='file' id='real-file' name='foto[]' hidden='hidden' multiple='multiple'>";
                    echo "<button type='button' id='custom-button' class='blue_foto'>Stiskněte</button>";
                    echo "<span id='custom-text'>Žádná fotografie.</span>";
                echo "</div>";
            echo "<li><label>Druh</label></li>";
                echo "<div class='select_custom'>";
                    echo "<select name='druh'>";

// changed code here
                            // 1 you dont need to connect twice
                            //$conn = mysqli_connect($servername, $username, $password, $dbname);
                            $query = 'SELECT id, druh FROM druh';

                            // use different var here
                            // and then use it in the related function calls
                            $result1 = mysqli_query($conn, $query);

                            if ($result1) {
                                // and of course use a different var to hold the row data
                                // so you dont overwrite that also
                                while ($row1 = mysqli_fetch_array($result1)) {
                                    echo "<option value=".$row1['id'].">".$row1['druh']."</option>";
                                }
                            }
                    echo "</select>";
            echo "</div>";
            echo "<li><label>Popis</label></li>";
                echo "<textarea name='text' placeholder='Popis'>".$row['popis']."</textarea>";
            echo "<li><input type='submit' name='submit' class='orange_input' value='Potvrďte'></li>";
        }
    }   

单独的问题,您在此代码中似乎没有 &lt;form&gt;&lt;/form&gt; 标记。否则,放置在输入字段中的数据将永远不会作为表单传输到 PHP 脚本进行处理

重要提示

您的脚本对SQL Injection Attack 开放。 甚至if you are escaping inputs, its not safe! 您应该考虑在 MYSQLI_PDO API 中使用 prepared parameterized statements 而不是串联值

所以要准备相关的危险查询

$query = 'SELECT nazev, popis FROM recepty WHERE id = ?';
$stmt = $conn->prepare($conn, $query);
$stmt->bind_param('i', $_POST['id_recept']);

$stmt->execute();
$result = $stmt->get_result();

. . .

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-01
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多