PHP 注册脚本没有显示错误,但没有更新数据库
No errors showing on PHP registration script but not updating database
我正在学习 php(请对我温柔一些 - 我是一名试图学习 PHP 黑暗艺术的前端开发人员)我知道这个脚本没有正确的安全措施等等等,但我正在使用作为让我前进的基础。我没有在我的脚本中发现任何错误,但出于某种原因,当我点击注册时,页面刷新为空白且没有错误,并且在数据库中没有显示任何内容表明它已成功插入?
真的希望有人关注一下,看看出了什么问题。
<?php
$page_title = 'Register';
include ('includes/header.html');
if ($_SERVER ['REQUEST_METHOD'] == 'POST')
{
require ('../connect_db.php');
$errors = array();
// Takes value provided in first name input field
if(empty($_POST['first_name']))
{ $errors[] = 'Enter your first name here'; }
else
{ $fn = mysqli_real_escape_string($dbc, trim($_POST['first_name'])); }
// Takes value provided in last name input field
if(empty($_POST['last_name']))
{ $errors[] = 'Enter your last name here'; }
else
{ $fn = mysqli_real_escape_string($dbc, trim($_POST['last_name'])); }
// Takes value provided in email input field
if(empty($_POST['email']))
{ $errors[] = 'Enter your email address'; }
else
{ $ln = mysqli_real_escape_string($dbc, trim($_POST['email'])); }
// Takes value provided in passwor input field & then checks if they match
if(empty($_POST['pass1']))
{
if($_POST[ 'pass1' ] != $_POST[ 'pass2' ])
{ $errors[] = 'Passwords do not match.'; }
else
{ $p = mysqli_real_escape_string($dbc, trim( $_POST['pass1'])); }
}
else{ $errors[] = 'Enter your password.'; }
//This then checks to see if the email is already registered
if($empty( $errors ))
{
$q = "SELECT user_id FROM users WHERE email='$e'";
$r = mysqli_query ($dbc, $q);
if(mysqli_num_rows($r) !=0 )
{ $errors[] = 'Email address already registered. <a href="login.php">Login</a>'; }
}
//The information will then be stored into the database when successful
if($empty( $errors ))
{
$q = "INSERT INTO users (first_name, last_name, email, pass, reg_date) VALUES ('$fn,'$in','$e', SHA1('$p'), NOW() )";
$r = mysqli_query ($dbc, $q);
if( $r )
{
echo '<h1>Registered!</h1>
<p>You are now registered.</hp
<p><a href="login.php">Login</a></p>';
}
mysqli_close( $dbc );
include ('includes/footer.html');
exit();
}
//Appended statement to show any errors that may occur
else
{
echo '<h1>Error!</h1>
<p id="err_msg">The following error(s) occured:<br>';
foreach ( $errors as $msg )
{
echo "- $msg<br>";
}
echo 'Please try again</p>';
mysqli_close( $dbc );
}
}
?>
<!-- Form begins here -->
<h1>Register</h1>
<form action="register.php" method="POST">
<p>
First name: <input type="text" name="first_name" value="<?php echo $_POST[ 'first_name' ]; ?>"><br><br>
Last name: <input type="text" name="last_name" value="<?php echo $_POST[ 'last_name' ]; ?>"><br><br>
Email Address: <input type="text" name="email" value="<?php echo $_POST[ 'email' ]; ?>"><br><br>
Password: <input type="text" name="pass1" value="<?php echo $_POST[ 'pass1' ]; ?>"><br><br>
Confirm Password: <input type="pass2" name="password" value="<?php echo $_POST[ 'pass2' ]; ?>"><br><br>
</p>
<input type="submit" value="Register"></p>
</form>
我的连接文件位于根目录中,我知道它有一个有效的连接。 (我希望这不是问题所在)
$r = mysqli_query ($dbc, $q) or die(mysqli_error($db));
"or die(mysqli_error($db))."
是你的朋友(更重要的是 "or" 不要让 'die' 部分吓到你。这只是意味着现在停止处理 - 吐出错误消息。如果你正在学习 php,你应该熟悉这个何时以及如何使用它。当你 dev/deb 吐出丑陋的消息时,好的,在生产中使用 'or die'.
以外的东西
How to display errors for my mysqli query
if($empty( $errors ))
在您的示例的第 40 和 49 行:删除空前的 $,它是函数名,而不是变量名。
还在脚本的开头添加 error_reporting(E_ALL);
以尝试显示 PHP error/warning 消息。它们有时会被网络托管服务提供商禁用。
仔细检查调用页面的表单 HTML。该方法是否设置为 Post 以外的其他内容?如果您没有看到任何东西,检查第一个 if 语句是一个好的开始。你可以做一个
die(print_r($_POST, true));
快速检查一下。如果那里有任何东西,你没关系,如果它是空的,检查 "method" 属性的 <form>
标签中的 HTML。
我正在学习 php(请对我温柔一些 - 我是一名试图学习 PHP 黑暗艺术的前端开发人员)我知道这个脚本没有正确的安全措施等等等,但我正在使用作为让我前进的基础。我没有在我的脚本中发现任何错误,但出于某种原因,当我点击注册时,页面刷新为空白且没有错误,并且在数据库中没有显示任何内容表明它已成功插入?
真的希望有人关注一下,看看出了什么问题。
<?php
$page_title = 'Register';
include ('includes/header.html');
if ($_SERVER ['REQUEST_METHOD'] == 'POST')
{
require ('../connect_db.php');
$errors = array();
// Takes value provided in first name input field
if(empty($_POST['first_name']))
{ $errors[] = 'Enter your first name here'; }
else
{ $fn = mysqli_real_escape_string($dbc, trim($_POST['first_name'])); }
// Takes value provided in last name input field
if(empty($_POST['last_name']))
{ $errors[] = 'Enter your last name here'; }
else
{ $fn = mysqli_real_escape_string($dbc, trim($_POST['last_name'])); }
// Takes value provided in email input field
if(empty($_POST['email']))
{ $errors[] = 'Enter your email address'; }
else
{ $ln = mysqli_real_escape_string($dbc, trim($_POST['email'])); }
// Takes value provided in passwor input field & then checks if they match
if(empty($_POST['pass1']))
{
if($_POST[ 'pass1' ] != $_POST[ 'pass2' ])
{ $errors[] = 'Passwords do not match.'; }
else
{ $p = mysqli_real_escape_string($dbc, trim( $_POST['pass1'])); }
}
else{ $errors[] = 'Enter your password.'; }
//This then checks to see if the email is already registered
if($empty( $errors ))
{
$q = "SELECT user_id FROM users WHERE email='$e'";
$r = mysqli_query ($dbc, $q);
if(mysqli_num_rows($r) !=0 )
{ $errors[] = 'Email address already registered. <a href="login.php">Login</a>'; }
}
//The information will then be stored into the database when successful
if($empty( $errors ))
{
$q = "INSERT INTO users (first_name, last_name, email, pass, reg_date) VALUES ('$fn,'$in','$e', SHA1('$p'), NOW() )";
$r = mysqli_query ($dbc, $q);
if( $r )
{
echo '<h1>Registered!</h1>
<p>You are now registered.</hp
<p><a href="login.php">Login</a></p>';
}
mysqli_close( $dbc );
include ('includes/footer.html');
exit();
}
//Appended statement to show any errors that may occur
else
{
echo '<h1>Error!</h1>
<p id="err_msg">The following error(s) occured:<br>';
foreach ( $errors as $msg )
{
echo "- $msg<br>";
}
echo 'Please try again</p>';
mysqli_close( $dbc );
}
}
?>
<!-- Form begins here -->
<h1>Register</h1>
<form action="register.php" method="POST">
<p>
First name: <input type="text" name="first_name" value="<?php echo $_POST[ 'first_name' ]; ?>"><br><br>
Last name: <input type="text" name="last_name" value="<?php echo $_POST[ 'last_name' ]; ?>"><br><br>
Email Address: <input type="text" name="email" value="<?php echo $_POST[ 'email' ]; ?>"><br><br>
Password: <input type="text" name="pass1" value="<?php echo $_POST[ 'pass1' ]; ?>"><br><br>
Confirm Password: <input type="pass2" name="password" value="<?php echo $_POST[ 'pass2' ]; ?>"><br><br>
</p>
<input type="submit" value="Register"></p>
</form>
我的连接文件位于根目录中,我知道它有一个有效的连接。 (我希望这不是问题所在)
$r = mysqli_query ($dbc, $q) or die(mysqli_error($db));
"or die(mysqli_error($db))."
是你的朋友(更重要的是 "or" 不要让 'die' 部分吓到你。这只是意味着现在停止处理 - 吐出错误消息。如果你正在学习 php,你应该熟悉这个何时以及如何使用它。当你 dev/deb 吐出丑陋的消息时,好的,在生产中使用 'or die'.
以外的东西How to display errors for my mysqli query
if($empty( $errors ))
在您的示例的第 40 和 49 行:删除空前的 $,它是函数名,而不是变量名。
还在脚本的开头添加 error_reporting(E_ALL);
以尝试显示 PHP error/warning 消息。它们有时会被网络托管服务提供商禁用。
仔细检查调用页面的表单 HTML。该方法是否设置为 Post 以外的其他内容?如果您没有看到任何东西,检查第一个 if 语句是一个好的开始。你可以做一个
die(print_r($_POST, true));
快速检查一下。如果那里有任何东西,你没关系,如果它是空的,检查 "method" 属性的 <form>
标签中的 HTML。