douhuilin1152 2015-01-01 18:18
浏览 13
已采纳

PHP注册脚本没有显示错误,但没有更新数据库

I am learning php (please be gentle with me - I am a front-end dev trying to learn the dark art of PHP) I know this script fails to have the correct measures in for security etc etc but I am using as a base to get me going. I fail to spot any errors in my script but for some reason when I hit register the page refreshes blank with no errors and show nothing in the DB to suggest it has been inserted successfully?

Real wanting somebody to cast an eye over this and see whats going wrong.

<?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>

My connect file is sat in the root and I know it has a valid connection. (I hope that isn't the issue anyway)

  • 写回答

3条回答 默认 最新

  • duanchi1230 2015-01-01 18:54
    关注

    if($empty( $errors )) at lines 40 and 49 of your example : remove the $ before empty, it is a function name, not a variable name.

    Also add error_reporting(E_ALL); at the beginning of your script to try to display PHP error/warning messages. They are sometimes disabled by web hosting providers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题