使用PHP将HTML表单连接到数据库

时间:2022-10-16 13:13:14

I have an HTML form (code below), and my goal is to submit user-generated data to the database so that they can later login using the stored information. I'm using MAMP.

我有一个HTML表单(下面的代码),我的目标是将用户生成的数据提交给数据库,以便以后可以使用存储的信息进行登录。我正在使用MAMP。

The problem: I created a PHP file to process this data and send it to the database, but when I press "submit" the only thing I get is a page that repeats my PHP code. I tried saving the files into my htdocs folder and got a 404 error.

问题:我创建了一个PHP文件来处理这些数据并将其发送到数据库,但是当我按“提交”时,我得到的唯一一个是重复我的PHP代码的页面。我尝试将文件保存到我的htdocs文件夹中并出现404错误。

I know that variations of this question have been asked before, but still haven't found a solution that works. I did a "Hello World" test using PHP and it worked fine, so I think everything is connected/installed correctly. Would really appreciate any advice!

我知道之前已经问过这个问题的变体,但仍然没有找到有效的解决方案。我使用PHP进行了“Hello World”测试并且工作正常,所以我认为所有内容都已正确连接/安装。非常感谢任何建议!

Here's my HTML:

这是我的HTML:

<label>Username</label>
<input type="text" id="name" name="name" placeholder="Choose a Username" required>
<span class="error"><?php echo $nameErr;?></span>
<br>

<label>Email address:</label>
<input type="text" id="email_addr" name="email_addr" placeholder="Enter your Email" required>
<span class="error"><?php echo $email_addrErr;?></span>
<br>

<label>Confirm email address:</label>
<input type="text" id="email_addr_repeat" name="email_addr_repeat" placeholder="Confirm email address" required oninput="check(this)">
<span class="error"><?php echo $email_addr_repeatErr;?></span>
<br>

<label>Create a password:</label>
<input type="password" id="password" name="password" placeholder="Create a Password" required>
<span class="error"><?php echo $passwordErr;?></span>
<br>

<label>Confirm password:</label>
<input type="password" id="password_repeat" name="password_repeat" placeholder="Confirm Password" required oninput="check(this)">
<span class="error"><?php echo $password_repeatErr;?></span>
<br>

<input type="submit" id="button" name="submit" value="Submit">

Here's my PHP:

这是我的PHP:

    if( $_POST )
        {
      $con = mysql_connect("localhost");

      if (!$con)
      {
        die('Could not connect: ' . mysql_error());
      }

      mysql_select_db("mydatabase", $con);

      $users_name = $_POST['name'];
      $users_email = $_POST['email_addr'];
      $users_email_two = $_POST['email_addr_repeat'];
      $users_password = $_POST['password'];
      $users_password_two = $_POST['password_repeat'];

      $query = " INSERT INTO `mydatabase`.`registration` (`name`,`email_addr`,`email_addr_repeat`, `password`,`password_repeat`) VALUES (NULL, '$users_name','$users_email','$users_email_two','$users_password',   '$users_password_two')";
      mysql_query($query);

      echo "<h2>Thank you for registering!</h2>";

      mysql_close($con);
    }

2 个解决方案

#1


0  

Column count does not match value count. What is being inserted as NULL?

列数与值计数不匹配。什么被插入为NULL?

INSERT INTO `mydatabase`.`registration` (`name`, `email_addr`,   
`email_addr_repeat`, `password`,
    `password_repeat`) VALUES (NULL, '$users_name',
    '$users_email', '$users_email_two','$users_password',       
'$users_password_two');

should be

应该

INSERT INTO `mydatabase`.`registration` (`NULL_COLUMN`, `name`, `email_addr`,   
`email_addr_repeat`, `password`,
    `password_repeat`) VALUES (NULL, '$users_name',
    '$users_email', '$users_email_two','$users_password',       
'$users_password_two');

where NULL COLUMN is the column that is being inserted asNULL.

其中NULL COLUMN是作为NULL插入的列。

Also, mysql_* functions are deprecated and removed in PHP 7. Use PDO or MySQLi.

此外,在PHP 7中不推荐使用mysql_ *函数并使用它们。使用PDO或MySQLi。

#2


0  

Remove NULL value from insert query :

从插入查询中删除NULL值:

$query = " INSERT INTO mydatabase.registration (name, email_addr, email_addr_repeat, password, password_repeat) VALUES ('$users_name', '$users_email', '$users_email_two','$users_password', '$users_password_two')"; 

#1


0  

Column count does not match value count. What is being inserted as NULL?

列数与值计数不匹配。什么被插入为NULL?

INSERT INTO `mydatabase`.`registration` (`name`, `email_addr`,   
`email_addr_repeat`, `password`,
    `password_repeat`) VALUES (NULL, '$users_name',
    '$users_email', '$users_email_two','$users_password',       
'$users_password_two');

should be

应该

INSERT INTO `mydatabase`.`registration` (`NULL_COLUMN`, `name`, `email_addr`,   
`email_addr_repeat`, `password`,
    `password_repeat`) VALUES (NULL, '$users_name',
    '$users_email', '$users_email_two','$users_password',       
'$users_password_two');

where NULL COLUMN is the column that is being inserted asNULL.

其中NULL COLUMN是作为NULL插入的列。

Also, mysql_* functions are deprecated and removed in PHP 7. Use PDO or MySQLi.

此外,在PHP 7中不推荐使用mysql_ *函数并使用它们。使用PDO或MySQLi。

#2


0  

Remove NULL value from insert query :

从插入查询中删除NULL值:

$query = " INSERT INTO mydatabase.registration (name, email_addr, email_addr_repeat, password, password_repeat) VALUES ('$users_name', '$users_email', '$users_email_two','$users_password', '$users_password_two')";