PHP不会插入MySQL

时间:2021-08-30 16:08:42

I am having problems inserting information from a form into a MySQL database.

我在将表单中的信息插入MySQL数据库时遇到问题。

When I click the "submit" button, the URL changes from domain.com/register-form.php to domain.com/register-script.php and it doesn't add anything to my database table.

当我单击“提交”按钮时,URL会从domain.com/register-form.php更改为domain.com/register-script.php,并且它不会向我的数据库表添加任何内容。

After I found out my original piece of code wasn't working, I decided to simplify it to the bare minimum without escape_strings and password hashing, and the basic version still didn't insert into my database.

在我发现我的原始代码无效后,我决定将其简化为最小化而没有escape_strings和密码哈希,并且基本版本仍未插入到我的数据库中。

I'm certain that I'm just overlooking something very basic, but I can't figure it out for the life of me. All of the database information is correct, and the user has full permissions.

我确信我只是忽略了一些非常基本的东西,但我无法理解我的生活。所有数据库信息都是正确的,并且用户具有完全权限。

NOTE: Please ignore the lack of security in the script and user rights. This is a very simplified test that didn't work.

注意:请忽略脚本和用户权限缺乏安全性。这是一个非常简化的测试,不起作用。

My file structure is a single directory consisting of the following three files: database.php, register-form.php, register-script.php.

我的文件结构是一个目录,由以下三个文件组成:database.php,register-form.php,register-script.php。

My database table Users is structured as:

我的数据库表用户的结构如下:

user_username varchar(255) COLLATE utf8_unicode_ci NOT NULL,
user_password varchar(255) COLLATE utf8_unicode_ci NOT NULL,
user_email varchar(255) COLLATE utf8_unicode_ci NOT NULL

user_username varchar(255)COLLATE utf8_unicode_ci NOT NULL,user_password varchar(255)COLLATE utf8_unicode_ci NOT NULL,user_email varchar(255)COLLATE utf8_unicode_ci NOT NULL

database.php:

<?php
    $hostname = "localhost";
    $user = "databaseuser";
    $password = "databasepassword";
    $database = "databasename";
    $prefix = "";
    $database = mysqli_connect($hostname,$user,$password,$database);
?>

register-form.php

<?php require 'database.php' ?>

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>

<form name="register" action="register-script.php" method="post">
<label>Desired Username: </label>
<input type="text" name="newuser_username" placeholder="Desired Username">
<label>Password: </label>
<input type="password" name="newuser_password" placeholder="Password">
<label>Email: </label>
<input type="email" name="newuser_email" placeholder="Email Address">
<input type="submit" name="register_button" value="Sign Up!">
</form>

</body>
</html>

register-script.php

<?php
    require 'database.php';

    if(isset($_REQUEST['register_button'])){
        $username = $_REQUEST['newuser_username'];
        $password = $_REQUEST['newuser_password'];
        $email = $_REQUEST['newuser_email'];

        $sql = "INSERT INTO users (user_username,user_password,user_email) VALUES ('$username','$password','$email')";

        mysqli_query($database,$sql);

    }

?>

1 个解决方案

#1


0  

First of all make sure that your phpmyadmin user name and password are correct in default case it is root and blank.

首先确保您的phpmyadmin用户名和密码在默认情况下是正确的,它是root和空白。

Check Your database connection with mysqli_connect_errno() that actually you are connected to database or not.

检查您的数据库连接与mysqli_connect_errno()实际上您是否连接到数据库。

Check with mysqli_num_rows() that your query returning some results or not.

用mysqli_num_rows()检查你的查询是否返回了一些结果。

and at the end close mysqli connection.

最后关闭mysqli连接。

#1


0  

First of all make sure that your phpmyadmin user name and password are correct in default case it is root and blank.

首先确保您的phpmyadmin用户名和密码在默认情况下是正确的,它是root和空白。

Check Your database connection with mysqli_connect_errno() that actually you are connected to database or not.

检查您的数据库连接与mysqli_connect_errno()实际上您是否连接到数据库。

Check with mysqli_num_rows() that your query returning some results or not.

用mysqli_num_rows()检查你的查询是否返回了一些结果。

and at the end close mysqli connection.

最后关闭mysqli连接。