php - PHP MYSQL中的错误跟踪-是否有更多信息?

标签 php mysql error-handling

我正在尝试为网站实现用户注册。
一切都很好,直到我在服务器上的错误日志中得到了:

Stack trace:
#0 /home/yclyxfu6ktu0/public_html/mydomain.com/newapp/register.php(50): PDOStatement->fetchAll()
#1 {main} thrown in /home/yclyxfu6ktu0/public_html/mydomain.com/newapp/register.php on line 50


因此,我进入register.php的第50行。
00000001 <?php
00000002 //register.php
00000003 
00000004 include('database_connection.php');
00000005 
00000006 if(isset($_SESSION['user_id']))
00000007 {
00000008  header("location:index.php");
00000009 }
00000010 
00000011 $message = '';
00000012 
00000013 if(isset($_POST["register"]))
00000014 {
00000015  $query = "
00000016  SELECT * FROM register_user 
00000017  WHERE user_email = :user_email
00000018  ";
00000019  $statement = $connect->prepare($query);
00000020  $statement->execute(
00000021   array(
00000022    ':user_email' => $_POST['user_email']
00000023   )
00000024  );
00000025  $no_of_row = $statement->rowCount();
00000026  if($no_of_row > 0)
00000027  {
00000028   $message = '<label class="text-danger">Email Already Exits</label>';
00000029  }
00000030  else
00000031  {
00000032   $user_password = rand(100000,999999);
00000033   $user_encrypted_password = password_hash($user_password, PASSWORD_DEFAULT);
00000034   $user_activation_code = md5(rand());
00000035   $insert_query = "
00000036   INSERT INTO register_user 
00000037   (user_name, user_email, user_password, user_activation_code, user_email_status) 
00000038   VALUES (:user_name, :user_email, :user_password, :user_activation_code, :user_email_status)
00000039   ";
00000040   $statement = $connect->prepare($insert_query);
00000041   $statement->execute(
00000042    array(
00000043     ':user_name'   => $_POST['user_name'],
00000044     ':user_email'   => $_POST['user_email'],
00000045     ':user_password'  => $user_encrypted_password,
00000046     ':user_activation_code' => $user_activation_code,
00000047     ':user_email_status' => 'not verified'
00000048    )
00000049   );
00000050   $result = $statement->fetch();
00000051   if(isset($result))
00000052   {
00000053    $base_url = "http://everyonehr/newapp/";
00000054    $mail_body = "
00000055    <p>Hi ".$_POST['user_name'].",</p>
00000056    <p>Thanks for Registration. Your password is ".$user_password.", This password will work only after your email verification.</p>
00000057    <p>Please Open this link to verified your email address - ".$base_url."email_verification.php?activation_code=".$user_activation_code."
00000058    <p>Best Regards,<br />Webslesson</p>
00000059    ";
00000060    require 'class/class.phpmailer.php';
00000061    $mail = new PHPMailer;
00000062    $mail->IsSMTP();        //Sets Mailer to send message using SMTP
00000063    $mail->Host = 'smtpout.secureserver.net';  //Sets the SMTP hosts of your Email hosting, this for Godaddy
00000064    $mail->Port = '80';        //Sets the default SMTP server port
00000065    $mail->SMTPAuth = true;       //Sets SMTP authentication. Utilizes the Username and Password variables
00000066    $mail->Username = 'xxxxxxxx';     //Sets SMTP username
00000067    $mail->Password = 'xxxxxxxx';     //Sets SMTP password
00000068    $mail->SMTPSecure = '';       //Sets connection prefix. Options are "", "ssl" or "tls"
00000069    $mail->From = 'info@webslesson.info';   //Sets the From email address for the message
00000070    $mail->FromName = 'Webslesson';     //Sets the From name of the message
00000071    $mail->AddAddress($_POST['user_email'], $_POST['user_name']);  //Adds a "To" address   
00000072    $mail->WordWrap = 50;       //Sets word wrapping on the body of the message to a given number of characters
00000073    $mail->IsHTML(true);       //Sets message type to HTML    
00000074    $mail->Subject = 'Email Verification';   //Sets the Subject of the message
00000075    $mail->Body = $mail_body;       //An HTML or plain text message body
00000076    if($mail->Send())        //Send an Email. Return true on success or false on error
00000077    {
00000078     $message = '<label class="text-success">Register Done, Please check your mail.</label>';
00000079    }
00000080   }
00000081  }
00000082 }
00000083 
00000084 ?>
00000085 
00000086 <!DOCTYPE html>
00000087 <html>
00000088  <head>
00000089   <title>PHP Register Login Script with Email Verification</title>  
00000090   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
00000091   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
00000092   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
00000093  </head>
00000094  <body>
00000095   <br />
00000096   <div class="container" style="width:100%; max-width:600px">
00000097    <h2 align="center">PHP Register Login Script with Email Verification</h2>
00000098    <br />
00000099    <div class="panel panel-default">
00000100     <div class="panel-heading"><h4>Register</h4></div>
00000101     <div class="panel-body">
00000102      <form method="post" id="register_form">
00000103       <?php echo $message; ?>
00000104       <div class="form-group">
00000105        <label>User Name</label>
00000106        <input type="text" name="user_name" class="form-control" pattern="[a-zA-Z ]+" required />
00000107       </div>
00000108       <div class="form-group">
00000109        <label>User Email</label>
00000110        <input type="email" name="user_email" class="form-control" required />
00000111       </div>
00000112       <div class="form-group">
00000113        <input type="submit" name="register" id="register" value="Register" class="btn btn-info" />
00000114       </div>
00000115      </form>
00000116      <p align="right"><a href="login.php">Login</a></p>
00000117     </div>
00000118    </div>
00000119   </div>
00000120  </body>
00000121 </html>
新的用户信息被插入到数据库中,因此我知道数据库连接有效。
谁能告诉我为什么脚本在这里失败:
00000050   $result = $statement->fetch();
先感谢您!
我根本无法弄清楚该语句出了什么问题。

最佳答案

INSERT查询不返回任何结果,因此您无法获取它们。
如果您想知道INSERT是否成功,请检查execute()的返回值。

if ($statement->execute(...)) {
    // code that sends mail
} else {
    // report failure
}

关于php - PHP MYSQL中的错误跟踪-是否有更多信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65443208/

相关文章:

php - PHP高级搜索(具有多个选项)

javascript - 如何用 "ReferenceError"捕获 "onerror"

php - 在PHP中输出详细的错误

Php:任何用户输入字段和提交按钮仅更新最后一个字段

php - 两个表连接两个结果

php - 如何更改MySql中的 'Timestamp'列语言?

php - 尝试在数据库中插入记录时 SQL 查询错误

mysql - 无法使用 foreman-rake 重置 foreman 管理员密码

PHP改进数据检索

php - MYSQL删除字段不属于数组的地方