我无法在php页面上查看数据库内容

时间:2019-11-29 14:41:38

标签: php mysql

我无法在php页面上查看数据库内容 我想使用ID提取数据 那就是代码:

<?php
//connect with database
$servername = "localhost";
$userdbname = "root";
$dbpassword = "";
$dbname = "users";
$usid = 0;
$docname = '';


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

$sql = "SELECT * FROM users WHERE id=".$usid;
if ($conn->query($sql) === TRUE) {
    $conn->close();
    $URLS = array();
    while ($row = $result->fetch_array()) {
        $docname= $row['docname'];
    }
    header("Location:Editeform.php");
}

?>

这是我要查看的表格:

<div class="form-group">
  <input type="text" class="form-input" name="docname" id="name" placeholder="Your Name" value="<?php echo $users['docname']; ?>" />
                    </div>

1 个答案:

答案 0 :(得分:0)

我已经举例说明了PDO的外观(我发现您才刚刚开始,我建议学习PDO并不困难)
这样,您也可以避免SQL注入。

$host = '127.0.0.1';
$db   = 'test';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';

$usid = 0;
$docname = '';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];
try {
     $pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
     throw new \PDOException($e->getMessage(), (int)$e->getCode());
}

$stmt = $pdo->query("SELECT * FROM users WHERE id=?");
$stmt->execute([$usid]); 

$row = $stmt->fetch();
$docname = $row['docname'];

echo '
<div class="form-group">
  <input type="text" class="form-input" name="docname" id="name" placeholder="Your Name" value="'.$docname.'" />
</div>';

P.S。我尚未对其进行测试,因此请填写您的详细信息并尝试一下!

这是一个非常不错的网站,可以全面了解PDO:https://phpdelusions.net/pdo