php - PHP没有来自HTML表单的响应
问题描述
承认有点尴尬,但这个问题我已经有一个多星期了。我正在尝试将表单中的数据获取到我的数据库中,代码方面的一切似乎都很完美,但我从底部提到的 connect.php 文件中只得到“连接正常”的回复。没有响应,数据库中也没有结果。
我已经仔细检查了文件的路径,并在表单中添加了“name”和“id”。
这是我的表格:
<form action="registration.php" method="POST">
<div class="registration_wrapper">
<button class="registration_button">Registration ⯆</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' ) ;
?>
解决方案
原来是与代码无关的数据库结构问题。数据库中的用户被分配了唯一的 ID。问题是该值没有自动增加,因此无法插入记录。这是解决方案的链接:
推荐阅读
- python - 在训练 Keras 模型时,有没有办法让 GPU 在设定的时期之间进行小休息(睡眠)?
- python - Python的新手,我试图创建一个计算器只是为了好玩,但出现了这个错误
- javascript - 如何使用javascript通过鼠标单击使单词随机更改其位置?
- flutter - Flutter Hello world 应用无法在 Android Emulator x86_64 上启动
- scala - Akka 持久查询阅读器作为竞争消费者
- php - xdebug 给出致命错误:找不到类
- typescript - 在 Nuxt 项目中使用 TypeScript 和 Prettier 冲突 ESLint 配置
- c# - 我在 docker 容器上的源代码在哪里
- ios - Xcode 在 Interface Builder 中使用不同的 Text
- android - 实现从 Android App 直接调用