- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用以下准备好的语句更新表:
$query = " UPDATE signup SET name=?,password=?,verify_key=? WHERE email=? ";
$stmt=$conn->prepare($query);
$stmt->bind_param("ssss",$dname,$password,$verify_key,$email);
$stmt->execute();
$stmt->close();
但它给出了我长期坚持的错误:
Fatal error: Call to a member function bind_param() on boolean in somefile.php on line 75
数据库表结构如下:
mysql> desc signup;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| sid | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| email | varchar(100) | NO | UNI | NULL | |
| password | varchar(255) | NO | | NULL | |
| verify_key | varchar(255) | NO | | NULL | |
| active | bit(1) | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
注意: 我打印了 $dname、$password、$verify_key、$email。所有打印正确的字符串值。
完整的 PHP 代码:
<?php
session_start();
ob_start();
require_once("connection.php");
$dname=$_POST['dname'];
$email=$_POST['email'];
$password=$_POST['password'];
if(empty($dname) || empty($email) || empty($password))
die("Enter correct details.");
$password = password_hash($password,PASSWORD_DEFAULT); // generate hash for password
$verify_key = password_hash(RandomString(),PASSWORD_DEFAULT); // generate hash for verification key
/* RandomString() generates random string */
function RandomString($length = 32) {
$randstr;
//our array add all letters and numbers if you wish
$chars = array(
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
for ($rand = 0; $rand <= $length; $rand++) {
$random = rand(0, count($chars) - 1);
$randstr .= $chars[$random];
}
return $randstr;
} // function RandomString() over
/* Mail Sending Code */
function sendMail($email,$verify_key){
$to = $email;
$subject = 'Something';
$mail_link='https://127.0.0.1/rs/signupCheck.php?email='.$email.'&key='.$verify_key;
$message = "
<html>
<head>
<title></title>
</head>
<body>
<p>
<a href='> Click this link to verify your account </a>
</p>
</body>
</html>
";
// To send HTML mail, the Content-type header must be set
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
$headers[] = 'From: Something <Something>';
$res = mail($to, $subject, $message, implode("\r\n", $headers));
return $res; //check officially mail is sent or not
} // function sendMain() ends
// check if email already exists in database
$query = " SELECT active FROM signup WHERE email=?";
$stmt = $conn->prepare($query);
$stmt->bind_param("s",$email);
$stmt->execute();
$stmt->bind_result($dbactive);
if($stmt->fetch()){ // email is present in DB
if($dbactive==1){ // status=1
$stmt->close();
die("Account already created. Try forget passwod if you can't access it.");
}
else{
/* Record is already present with status=0 , override all the details */
if(sendMail($email,$verify_key)) { // send mail first then update DB
$query = " UPDATE `signup` SET `name`=?,`password`=?,`verify_key`=? WHERE `email`=? ";
$stmt=$conn->prepare($query);
$stmt->bind_param("ssss",$dname,$password,$verify_key,$email);
$stmt->execute();
$stmt->close();
die("We sent you verification link on mail. Click it to verify !!!");
}
else {
die("Mail sending failed. Try again !!!");
}
}
} // email already present with either status=1 or status=0
else { // Insert account details in DB , nothing is present in DB for user
if(sendMail($email,$verify_key)) { // send mail first then update DB
$query = "INSERT INTO signup (sid,name,email,password,verify_key,active)VALUES(?,?,?,?,?,?)";
$stmt = $conn->prepare($query);
$stmt->bind_param("issssi",$fixed_sid='',$dname,$email,$password,$verify_key,$fixed_active=0);
/* Notice $fixed_sid and $fixed_active , it is needed
you can't have fixed values in bind_param
*/
$stmt->execute();
$stmt->close();
die("We sent you verification link on mail. Click it to verify !!!");
}
else {
die("Mail sending failed. Try again !!!");
}
} // first time data entry in DB
?>
最佳答案
在您的下一个 mysqli
交互之前添加 $stmt->close();
。
if(sendMail($email,$verify_key)) { // send mail first then update DB
$stmt->close();
$query = " UPDATE `signup` SET `name`=?,`password`=?,`verify_key`=? WHERE `email`=? ";
或者您可能总是希望在完成之前的 mysql
交互后执行 close
。
例如:
$query = " SELECT active FROM signup WHERE email=?";
$stmt = $conn->prepare($query);
$stmt->bind_param("s",$email);
$stmt->execute();
$stmt->bind_result($dbactive);
$stmt->close();
并从 if
中删除 $stmt->close();
。我从未见过关于此的官方文档,但我看到其他线程认为这是一个潜在问题。手册页本身实际上几乎是相反的:
So, while explicitly closing open connections and freeing result sets is optional, doing so is recommended.
是可选的
是错误的(...根据我的经验,我也主要使用 PDO)。
关于使用准备好的语句的 php 更新查询不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43309353/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!