PHP没有回应HTML表格

时间:2019-04-12 09:47:35

标签: php html

承认有点尴尬,但是这个问题我已经有一个多星期了。我正在尝试将数据从表单导入数据库中,所有代码似乎都很完美,但是我从底部提到的connect.php文件中仅收到“ Connection OK”答复。数据库中没有响应,也没有结果。

我已经仔细检查了文件的路径,并在表单中添加了“名称”和“ id”。

这是我的表格:

<form action="registration.php" method="POST">
<div class="registration_wrapper">
<button class="registration_button">Registration &#x2BC6;</button>
<input type="text" class="user_input_fields" id="first_name" name="first_name" placeholder="First Name" required>
<input type="text" class="user_input_fields" id="last_name" name="last_name" placeholder="Last Name" required>
<input type="text" class="user_input_fields" id="email" name="email" placeholder="E-mail" required>
<input type="text" class="user_input_fields" id="dob" name="dob" placeholder="Date of Birth" required>
<input type="text" class="user_input_fields" id="username" name="username" placeholder="Username" required>
<input type="text" class="user_input_fields" id="password1" name="password1" placeholder="Password" required>
<input type="text" class="user_input_fields" id="password2" name="password2" placeholder="Repeat Password" required>
<input type="checkbox" name="agreements">I agree to the <a href="Terms_and_conditions.php"><u>Terms & Conditions</u></a>
<button type="submit" class="continue_button">Continue ></button>
</div>
</form>

这是registration.php文件:

<?php
$page_title = 'Registration';

if ( $_SERVER[ 'REQUEST_METHOD' ] == 'POST' )
{
  require ('connect_db.php');
  $errors = array();

  if ( empty( $_POST[ 'first_name' ] ) )
  { $errors[] = 'Enter your first name.' ; }
  else
  { $fn = mysqli_real_escape_string( $link, trim( $_POST[ 'first_name' ] ) ) ; }

  # Check for a last name.
  if (empty( $_POST[ 'last_name' ] ) )
  { $errors[] = 'Enter your last name.' ; }
  else
  { $ln = mysqli_real_escape_string( $link, trim( $_POST[ 'last_name' ] ) ) ; }

  # Check for an email address:
  if ( empty( $_POST[ 'email' ] ) )
  { $errors[] = 'Enter your email address.'; }
  else
  { $e = mysqli_real_escape_string( $link, trim( $_POST[ 'email' ] ) ) ; }

  if ( empty( $_POST[ 'username' ] ) )
  { $errors[] = 'Enter your email address.'; }
  else
  { $un = mysqli_real_escape_string( $link, trim( $_POST[ 'username' ] ) ) ; }

  # Check for a password and matching input passwords.
  if ( !empty($_POST[ 'password1' ] ) )
  {
    if ( $_POST[ 'password1' ] != $_POST[ 'password2' ] )
    { $errors[] = 'Passwords do not match.' ; }
    else
    { $p = mysqli_real_escape_string( $link, trim( $_POST[ 'password1' ] ) ) ; }
  }
  else { $errors[] = 'Enter your password.' ; }

  # Check if email address already registered.
  if ( empty( $errors ) )
  {
    $q = "SELECT user_id FROM users WHERE email='$e'" ;
    $r = mysqli_query ( $link, $q ) ;
    if ( mysqli_num_rows( $r ) != 0 ) $errors[] = 'Email address already registered. <a href="login.php">Login</a>' ;
  }

  # On success register user inserting into 'users' database table.
  if ( empty( $errors ) ) 
  {
    $q = "INSERT INTO users (first_name, last_name, email, u_password, u_datejoined) VALUES ('$fn', '$ln', '$e','$un',SHA1('$p'), NOW() )";
    $r = mysqli_query ( $link, $q ) ;
    if ($r)
    { echo 'Registration successful'; }

    # Close database connection.
    mysqli_close($link); 


    exit();
  }
  # Or report errors.
  else 
  {
    echo '<div class="container"><h1>Error!</h1><p id="err_msg">The following error(s) occurred:<br>' ;
    foreach ( $errors as $msg )
    { echo " - $msg<br>" ; }
    echo 'Please try again.</p></div>';
    # Close database connection.
    mysqli_close( $link );
  }  
}
?>

这是connect.php文件:

<?php 
# establishing link to database using username, password and database name 
$link = mysqli_connect('localhost', 'root', 'root', 'tecbooks'); 

if (!$link) { 
    die('Could not connect to MySQL: ' . mysqli_error()); 
} 
echo '<h1>Connection OK</h1>';  
mysqli_set_charset( $link, 'utf8' ) ;
?>

2 个答案:

答案 0 :(得分:0)

以这种方式查看插入查询:

Navbar

您注意到了什么?缺少一些东西。

因此expand在这里是错误的:$q = "INSERT INTO users (first_name, last_name, email, u_password, u_datejoined) VALUES ('$fn', '$ln', '$e', '$un', SHA1('$p'), NOW() )"; 。数据库连接关闭,程序退出。

答案 1 :(得分:0)

原来是与代码无关的数据库结构问题。数据库中的用户被分配了唯一的ID。问题在于该值不会自动增加,因此无法插入记录。这是解决方案的链接:

Auto increment in phpmyadmin