首页 > 解决方案 > 使用 php 在 mysql 数据库中添加用户名、电子邮件和密码时出错

问题描述

我正在尝试使用 php 注册用户。不知何故,唯一被放入的是ID。介意给我小费吗?

 <?php
error_reporting(0);

require_once "php/connect.php";
$username = $_POST ['username'];
$useremail = $_POST['useremail'];
$userpwd = $_POST ['userpwd'];
$userpwd2 = $_POST['userpwd2'];



    try {
        $statement = $dbconnection->prepare("INSERT INTO `tbl_Nutzerdaten` (userid, username, useremail, userpwd) VALUES (null , '$username', '$useremail', '$userpwd')");
        $result = $statement->execute();
        $fetch = $statement->fetch();

    } catch (PDOException $e) {
        echo "Fehler:" . $e->getMessage();

    }

?>

所以这就是我的 insert.php 的样子。我一直在尝试改变我的表格,但仍然没有成功。数据库具有表:“tbl_Nutzerdaten”,其中包含用户 ID、用户名、useremail 和 userpwd 列。除了 ID,我仍然无法添加任何内容。

<!DOCTYPE html>
<html lang="de">
<head>
    <title>Registrierung</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="css/bootstrap.min.css" rel="stylesheet" >
    <script src="js/bootstrap.bundle.min.js" ></script>
    <?php
    error_reporting(0);
    require_once "insert.php";
    ?>
</head>
<body>
<div class="container bg-transparent border-0">
    <div class="row">
        <div class="col-sm-9 col-md-7 col-lg-5 mx-auto border-0">
            <div class="img-thumbnail my-5 border-0">
                <img src="bilder/htl logo.png"></img>
                <div class="card card-signin my-5 border-0">
                    <div class="card-body border-0">

                        <form class="form-signin form-control border-0" action="#" method="post">
                          <div class="form-label-group">
                              <h1 class="text-center">Registrierung</h1>
                               <label for="username">Benutzername</label>
                               <input type="text" id="username" class="form-control text-center" placeholder="Benutzername" required autofocus>
                              <div class="form-label-group">
                              <label for="useremail">Email</label>
                              <input type="email" id="useremail" class="form-control text-center" placeholder="Email" required autofocus>
                              </div>
                         </div>

                            <div class="form-label-group">
                                <label for="Passwort">Passwort</label>
                                <input type="password" id="userpwd" class="form-control text-center" placeholder="Passwort" required>

                            </div>
                            <div class="form-label-group">
                                <label for="Passwort2">Passwort bestätigen</label>
                                <input type="password" id="userpwd2" class="form-control text-center" placeholder="Passwort bestätigen" required>

                            </div>


                            <div class="custom-control custom-checkbox mb-3">
                             <input type="checkbox" class="custom-control-input" id="pwcheck">
                             <label class="custom-control-label" for="pwcheck">Password merken?</label>
                            </div>
                          <button class="btn btn-lg btn-primary btn-block text-uppercase " type="submit">Registrieren</button>


                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
</div>

这就是我正在使用的表格。

每次填写表格时,我在数据库中看到的唯一内容是 ID 和空值。我非常感谢您的帮助。

标签: phpmysqlbootstrap-4

解决方案


要将数据发送到特定的 PHP 文件,您必须定义 where..

在 HTML 页面中的 form 标记中,action 属性定义了您的 file.php 的路径,将信息发送到哪里。

例如: <form class="form-signin form-control border-0" action="path/to/your/file.php" method="post">

在您的输入标签中,您必须添加“名称”属性,因此您可以使用此名称在 PHP 文件中引用此数据...只是为了通过示例说明...

<input type="text" name="username" id="username" class="form-control text-center" placeholder="Benutzername" required autofocus>

然后在 PHP 文件中用类似这样的东西获取这些数据

$username = $_POST ['username'];

然后,您必须将数据发送到特定的数据库,毕竟您对这些数据进行了所有您想要的控制..

要发送数据,您必须了解如何使用准备好的语句避免 SQL 注入、绑定参数并最终执行语句。

我认为你必须看看所有这些东西,我建议你看看这个链接:

https://www.php.net/manual/en/book.pdo.php

https://www.w3schools.com/php/php_forms.asp

https://www.w3schools.com/html/html_forms_attributes.asp


推荐阅读