dqkelut8423 2015-07-07 11:40
浏览 12
已采纳

什么错误我的这个PHP代码片段[关闭]

<?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>

What I want is when user enters his email id then if it is already exists in table record then it shows an echo message already unsubscribed, otherwise it unsubscribe the user on unsubscribe button click

  • 写回答

1条回答 默认 最新

  • dsgdhtr_43654 2015-07-07 12:08
    关注

    Try changing from this :

    $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>";
    }
    

    to

    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>";
    }
    

    Changes :

    1. SELECT query is executed after checking whether the FORM has been POSTed and email field is not empty.
    2. Added unsubscribed = '1' clause in SELECT query otherwise it will fetch records which are not yet unsubscribed.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了