我如何使用PDO制作一个表单,用php中的foreach自动生成数据库的行


How can i make a form that generates the rows of a database automatically with a foreach in php using PDO

你好,我想为我在数据库中制作的测验制作一个编辑和删除表格。但我不知道如何用PHP中的foreach显示数据库中的行,所以当我添加一个问题时,它会自动显示在我的表单中

我试过这个:

<?php 
            error_reporting(E_ALL);
            ini_set("display_errors", 1);
            $user = '*******';
            $pass = '*******';
            $dbQuiz    = new PDO('mysql:host=localhost;dbname=lab_blendi', $user, $pass);
            $questions = $dbQuiz->prepare('SELECT * FROM Vragen');
            $questions->execute();
            $questionResult = $questions->fetchAll(PDO::FETCH_ASSOC);
            $output = array();
            $output['title'] = $questionResult[0]["Title"];
            foreach ($questionResult as $key => $aantal) {
                if ($aantal['QuestionId'] > 0) {
                    //do something here???
                }
            }
        ?>

如果我没有告诉你一些你需要知道的帮助,请问我。

fetchAll将把所有结果返回到一个数组中。

这只是一种以标准方式循环该数组以获取返回的所有列数据的情况。

您实际上不需要在$aantal['QuestionId']上进行IF测试,因为如果foreach继续循环,就必须有数据。

<?php 
    error_reporting(E_ALL);
    ini_set("display_errors", 1);
    $user = '*******';
    $pass = '*******';
    $dbQuiz    = new PDO('mysql:host=localhost;dbname=lab_blendi', $user, $pass);
    $questions = $dbQuiz->prepare('SELECT * FROM Vragen');
    $questions->execute();
    $questionResult = $questions->fetchAll(PDO::FETCH_ASSOC);
    $output = array();
    $output['title'] = $questionResult[0]["Title"];
    foreach ($questionResult as $key => $aantal) {
        echo $aantal['QuestionId'];
        echo $aantal['Title'];
        echo $aantal['Text'];
    }
?>

如果我理解正确,您希望在数据库中添加一些符号时自动更新数据库视图
这在PHP中是无法实现的,因为PHP是服务器端的,您需要用javascript实现Ajax。