我的这个PHP代码片段有什么问题

时间:2015-07-07 11:40:13

标签: php mysql

<?php
$connection = mysql_connect("localhost","*******","******");
if (!$connection) {
die('PHP Mysql database connection could not connect : ' . mysql_error());
}
else{ 
$db_name = "*****";
mysql_select_db($db_name, $connection);


$result = mysql_query("SELECT email FROM subscribers WHERE email = '".$_POST['email']."'";);
$num_rows = mysql_num_rows($result);
if(isset($_POST['submit'])){
if($_POST['email']!=""){
if ($num_rows > 0) {
echo "email already unsubscribed";
}
else {
$update_sql = "UPDATE subscribers SET unsubscribed = '1' WHERE email = '".$_POST['email']."'";
mysql_query($update_sql, $connection);
echo "<div id='notify' style ='margin-left: auto; margin-right: auto; width: 330px; height: 40px; text-align: center;  background-color: #9BFFCD; font-weight: bold; font-family: Verdana, Geneva, sans-serif; padding-top:18px; border: solid #060 thin;'> YOU'RE UNSUBSCRIBED</div>";
}

}
}
}
?> 
<!DOCTYPE html>
<html lang="en">
<head>
<title>Unsubscribe Page</title>
<script>
  setTimeout(function(){
    document.getElementById('notify').style.display = 'none';
    /* or
    var item = document.getElementById('notify')
    item.parentNode.removeChild(item); 
    */
  }, 3000);
</script>
</head>
<body>
<div style="margin-left: auto; margin-right: auto; width: 330px; height: 80px; text-align: center; margin-top:100px;">
<div style="height: 30px; margin-bottom: 12px; padding-top: 9px; font-family: Verdana, Geneva, sans-serif; font-size: 11px; text-align: center; background-color:#FBDACE"><strong>CONFIRM UR EMAIL-ID BELOW TO UNSUBSCRIBE</strong><span style="text-align: center"></span></div>
<div style="height: 30px; margin-bottom: 12px; padding-top: 9px; font-family: Verdana, Geneva, sans-serif; font-size: 11px; text-align: center; background-color:#FBDACE">
<form method="post" name="update" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td><input name="email" type="text" size="33" /></td>
      <td><input name="submit" type="submit" value="Unsubscribe" /></td>
    </tr>
  </table>
</form>
</div>
</div>
</html>

我想要的是当用户输入他的电子邮件ID时,如果它已经存在于表记录中,那么它会显示已经取消订阅的回送消息,否则它会取消订阅取消订阅按钮上的用户点击

1 个答案:

答案 0 :(得分:1)

尝试改变:

$result = mysql_query("SELECT email FROM subscribers WHERE email = '".$_POST['email']."'";);
$num_rows = mysql_num_rows($result);
if(isset($_POST['submit'])){
if($_POST['email']!=""){
if ($num_rows > 0) {
echo "email already unsubscribed";
}
else {
$update_sql = "UPDATE subscribers SET unsubscribed = '1' WHERE email = '".$_POST['email']."'";
mysql_query($update_sql, $connection);
echo "<div id='notify' style ='margin-left: auto; margin-right: auto; width: 330px; height: 40px; text-align: center;  background-color: #9BFFCD; font-weight: bold; font-family: Verdana, Geneva, sans-serif; padding-top:18px; border: solid #060 thin;'> YOU'RE UNSUBSCRIBED</div>";
}

if(isset($_POST['submit'])){
    if($_POST['email']!=""){
       $result = mysql_query("SELECT email FROM subscribers WHERE 
      email = '".$_POST['email']."' AND unsubscribed ='1'";);
       $num_rows = mysql_num_rows($result);
       if ($num_rows > 0) {
         echo "email already unsubscribed";
       }
      else {
  $update_sql = "UPDATE subscribers SET unsubscribed = '1' WHERE 
  email = '".$_POST['email']."'";
  mysql_query($update_sql, $connection);
  echo "<div id='notify' style ='margin-left: auto; margin-right: auto; width: 330px; height: 40px; text-align: center;  background-color: #9BFFCD; font-weight: bold; font-family: Verdana, Geneva, sans-serif; padding-top:18px; border: solid #060 thin;'> YOU'RE UNSUBSCRIBED</div>";
}

变更:

  1. 在检查FORM是否已被POST并且电子邮件字段不为空之后执行SELECT查询。
  2. 在SELECT查询中添加了unsubscribed ='1'子句,否则将获取尚未取消订阅的记录。