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

时间:2015-12-03 05:14:11

标签: php html database forms

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

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

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

这是我的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">

  

这是我的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 个答案:

答案 0 :(得分:0)

Column count does not match value count。什么被插入为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');

应该是

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');

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

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

答案 1 :(得分:0)

从插入查询中删除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')";