php - PHP MYSQL 中的错误跟踪 - 是否有更多信息?
问题描述
我正在尝试为网站实施用户注册。一切都很好,直到我在服务器上的错误日志中得到这个:
堆栈跟踪:
#0 /home/yclyxfu6ktu0/public_html/mydomain.com/newapp/register.php(50): PDOStatement->fetchAll()
#1 {main} 抛出 /home/yclyxfu6ktu0/public_html/mydomain.com/newapp /register.php 第 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
}
推荐阅读
- python - 如何根据以下问题更改下面的 python 代码以计算并输出输入数字的所有数字的总和?
- angular - 用于存储值的 Angular 服务
- angular - Ionic 5 和 Angular 出现更详细的控制台错误
- ios - 是否可以从我的 iPhone 恢复我的应用程序的数据库文件?
- java - 通信两个独立的 API
- javascript - 如何定位另一个 iFrame 中的 iFrame 中的元素?
- django - Apache Proxy 和 Daphne:连接缓慢
- sql - 查询帮助忽略重复项
- flutter - Flutter:如何在容器上显示手机图库?
- php - Laravel 前端脚手架不起作用