<?php
require_once'config.php';
if(isset($_POST['subscribe'])){
$email=$_POST['email'];
//check the email if it is already subscribed or not
if(strlen($email)>3){
$stmt=$mysql->prepare("SELECT * FROM subscribers WHERE email=?");
$stmt->bind_param('s', $email);
$stmt->execute();
if($stmt->field_count > 0){ // if not then add
unset($mysql); unset($stmt);
$mysql=new mysqli(HOST,USER,PASS,DB);
$stmt=$mysql->prepare('INSERT INTO subscribers(email) VALUES (?)');
$stmt->bind_param('s', $email);
$stmt->execute();
if($stmt->affected_rows>0){
echo "subscribed";
}
} else { //else is there
echo "Already there";
}
} else echo "empty string";
}
?>
如果我删除了行unset($mysql); unset($stmt);
,则会导致致命错误:Bind_param Non-Object Error
。但如果我取消设置然后重新定义对象然后工作正常。任何人都可以解释原因吗?
答案 0 :(得分:0)
警告:这是未经测试的。但是,我认为如何以更清洁的方式编写这一点非常重要。特别是,将你的意图分解为特定的函数,每个函数都做一件事并返回结果。这将有助于防止代码混乱,并且可以更轻松地自行测试每个块。
require_once'config.php';
//It is always good to declare stuff like this in a central place
//That way they're easy to change, and you change it everywhere you have that express intent
$SQL_FIND_SUBSCRIPTION = "SELECT * FROM subscribers WHERE email=?";
$SQL_NEW_SUBSCRIPTION = "INSERT INTO subscribers(email) VALUES (?)";
//Functions make testing easy!
static function attemptSubscription($postValues, &$returnMsg) {
if ( isset($postValues['subscribe'])
&& isset($postValues['email']) //be sure to validate both!
{
if (isValidEmail($postValues['email'])) {
subscribe($email);//this syntax may be off.
} else {
$returnMsg = "A valid email address must be provided.";
return false;
}
} else {
$returnMsg = "No subscription was attempted.";
return false;
}
}
//Returns true if the passed parameter is a valid email
static function isValidEmail($email) {
//left as an exercise for the reader.
}
//Assumes a valid email address is being passed
static function subscribe($email, &$returnMsg) {
global $mysql; //sadly, this is the cleanest way possible without using a class.
$stmt=$mysql->prepare($SQL_FIND_SUBSCRIPTION);
$stmt->bind_param('s', $email);
$stmt->execute();
$stmt->store_result(); // This lets you reuse stmt
if($stmt->field_count > 0){
//Subscription already exists
$returnMsg = "This subscription already exists.";
return true;
} else {
//Subscription must be added
return addNewSubscription($email, $returnMsg);
}
}
static function addNewSubscription($email, &$returnMsg) {
global $mysql; // :(
$stmt=$mysql->prepare($SQL_NEW_SUBSCRIPTION);
$stmt->bind_param('s', $email);
$stmt->execute();
$stmt->store_result();
if($stmt->affected_rows>0){
$returnMsg = "New subscription successful!";
return true;
} else {
$returnMsg = "New subscription failed.";//you can add more info here if you want
return false;
}
$stmt->close();
}
//now actually execute
$resultMsg = "";
if (attemptSubscription($_POST, $resultMsg)) {
echo "Success! ".$resultMsg;
} else {
echo "Oh no! Failure! ".$resultMsg;
}
// ?> Note that eliding this will help remove nasty hidden characters from the rendered html
查看store_result了解更多信息。
使用这种配置,您可以验证各个命令是否正常工作,而不依赖于首先执行它们嵌套的结构。特别是,验证第二个SQL查询至关重要:非绑定错误通常是因为无法找到列或表。但是,也就是说,意识到prepare()
语句所做的是向服务器发送查询以检查语法;该查询永远不会被重新发送,而是在服务器中保持“加载”,直到您告诉它(使用另一个语句)去掉它。随后绑定参数时,该参数将发送到服务器并放入可用插槽(?
)。然后,由于此模型,您可以多次有效地执行此查询。
请注意,如果服务器上的查询没有新参数的未绑定插槽,则尝试绑定它将失败。如果服务器上的查询试图引用不可用的变量(例如列名),则此步骤也将失败。
从您提供的详细信息中不清楚确切的问题是什么,但是如果您以更干净的方式编写代码,调试这些问题将变得更加容易:您的第二个SQL语句是不是很糟糕?是不是你没有从第一个语句中正确释放服务器资源?