PHP表单输入值不起作用

时间:2017-01-23 09:58:18

标签: php html forms

我喜欢在输入字段中填写标准值 我有这个代码:

$stma = $conn->prepare("SELECT * FROM `users` WHERE ID = '".$_GET['gebruiker']."' ");
$stma->execute();
$row_count = $stma->rowCount(); // returns 1
foreach ($conn->query($stma) as $rows) {
    $Username = $rows['Username'];
}

/// my form
echo '<form method="POST" >
    <table>
    <th colspan="3"><h1>Gebruiker bewerken</h1></th>
        <tr>
            <th>
                <h3>Gebruikersnaam: </h3>
            </th>
            <td>
                <input style="width: 70%;" type="text" READONLY value="'.$Username.'" > 
                // the value must be filled in this input field
            </td>
        </tr>
        <tr>
            <th>
                <h3>Wachtwoord: </h3>
            </th>
            <td>
                <input style="width: 70%;" type="password"  name="wachtwoord" REQUIRED>
            </td>
        </tr>
        <tr>
            <th>
            </th>
            <td colspan="2">
                <input type="submit" name="bewerken" class="button" style="vertical-align:middle" value="Opslaan">
            </td>
        </tr>
        '.$error.'
    </table>
</form>';

代码没有填写我从数据库中获得的值 我仍然得到一个空的表格字段 我的查询返回1个结果行(我已选中)
有人看到我的错误吗? 我没有看到我犯的错误(这是我的错误,它也适用于我的其他形式)

3 个答案:

答案 0 :(得分:1)

要确保它输出所有错误和警告(用于调试),这可能会有所帮助:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

将上述代码放在文件顶部。 您可能还希望阻止任何SQL注入:

$stma = $conn->prepare("SELECT * FROM `users` WHERE ID = ? ");
$stma->bindParam(1, $_GET['gebruiker'], PDO::PARAM_INT);
$stma->execute();
$stma->debugDumpParams(); // you could use this to check whether or not all parameters are set correctly
$row_count = $stma->rowCount(); // returns 1
foreach ($conn->query($stma) as $rows) {
    $Username = $rows['Username'];
}

答案 1 :(得分:1)

以下是一个工作示例。

<强> PHP

try {
        $conn = new PDO('mysql:host=localhost;dbname=YourDBname', 'root', '');
    } catch (PDOException $e) {
        echo $e->getMessage();
    }

    $id = $_GET['gebruiker'];

    $sql = "SELECT * FROM `users` WHERE id = :id";
    $stm = $conn->prepare($sql);

    $stm->execute(['id'=>$id]);

    $user = $stm->fetchObject();

    $username = $user->username;

<强> HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test</title>
</head>
<body>
    <form action="POST">
        <input type="text" value="<?php echo (isset($username)) ? $username : 'No value' ; ?>">
    </form>
</body>
</html>

如果您的网址设置了gebruiker,那么您只需执行以下操作:script.php?gebruiker = 1您可以将1替换为表格中存在的任何ID值。

答案 2 :(得分:-1)

please try this code
$stma = $conn->prepare("SELECT * FROM `users` WHERE ID = '".$_GET['gebruiker']."' ");

$stma->execute();

$row_count = $stma->rowCount(); // returns 1

foreach ($conn->query($stma) as $rows) {
  $Username = $rows['Username'];
}

**please replace this code**
$res = $conn->query("SELECT * FROM users WHERE ID = '".$_GET['gebruiker']."' ");

$allRows = $res->fetch_assoc(); 

$Username = $allRows['UserName'];