douyanguo7964 2018-06-02 08:49
浏览 64
已采纳

PHP准备好的陈述; 我们的结果

I try to implement a newsletter function in PHP. When a user subscribes on our website, a verification e-mail gets send to them. If they have already subscribed, they get a message displayed like: "you have already subscribed" or "E-Mail already registered, but not verified. The e-mail was resent.

function new_member($email,$list_id)
{
  global $db;
  $san_email = filter_var($email, FILTER_SANITIZE_EMAIL);
  if (filter_var($san_email, FILTER_VALIDATE_EMAIL))
  {
    $hash = openssl_random_pseudo_bytes(10, $cstrong);
    $qry = "SELECT COUNT(*),active,access_hash FROM ".MYSQL_PREFIX."mailing_list_member WHERE address = '?' AND list_id = ?";
    var_dump($qry);
    if ($stmt = $db->prepare($qry)) {
      $stmt->bind_param("si", $san_email, $list_id);
      $stmt->bind_result($count,$active,$db_hash);
      $stmt->fetch();
      $stmt->close();
    } else {
      echo "sendet query: <pre>".$qry."</pre>
"."Antwort: ".$db->error;
    }

    if ($count==1) {
      if ($active==1){
        return "E-Mail-Addresse bereits angemeldet.";
      } else {
        send_verification_mail($san_email,$list_id,$db_hash);
        return "E-Mail-Addresse bereits angemeldet, allerdings noch nicht bestätigt. Bestätigungsmail wurde erneut gesendet.";
      }
    } else {
      $qry = "INSERT INTO ".MYSQL_PREFIX."mailing_list_member (address,list_id,access_hash) VALUES ('?',?,'?')";
      if ($stmt = $db->prepare($qry)) {
        var_dump($san_email);
        $stmt->bind_param("sis", $san_email, $list_id, $hash);
        $stmt->fetch();
        $stmt->close();
        send_verification_mail($san_email,$list_id,$hash);
        return "Bestätigungsemail wurde erfolgreich an ".$san_email." gesendet.";
      } else {
        echo "sendet query: <pre>".$qry."</pre>
"."Antwort: ".$db->error;
      }
    }
  } else {
    return "Keine gültige E-Mail-Addresse angegeben!";
  }
}

For testing purpose this function got called like this (with a ream mail in there):

echo new_member('mail@example.com',1);

If I do this, the mail gets sent to the right e-mail-address, but not inserted into the database. Even if the user is already in the database, the e-mail is sent.

  • 写回答

1条回答 默认 最新

  • douchuang4402 2018-06-03 18:47
    关注

    The ?-Placeholder needs not to be in quotes and both prepared statements miss an execute command. The first one needs to be:

    $qry = "SELECT COUNT(*),active,access_hash FROM ".MYSQL_PREFIX."mailing_list_member WHERE address = ? AND list_id = ?";
     if ($stmt = $db->prepare($qry)) {
      $stmt->bind_param("si", $san_email,$list_id);
      $stmt->execute();
      $stmt->bind_result($count,$active,$db_hash);
      $stmt->fetch();
      $stmt->close();
    }
    

    and the second one:

    $qry = "INSERT INTO ".MYSQL_PREFIX."mailing_list_member (address,list_id,access_hash) VALUES (?,?,?)";
     if ($stmt = $db->prepare($qry)) {
        $stmt->bind_param("sis", $san_email, $list_id, $hash);
        $stmt->execute();       
        send_verification_mail($san_email,$list_id,$hash);
        $stmt->close();
        return "Bestätigungsemail wurde erfolgreich an ".$san_email." gesendet.";
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 爬虫爬取网站的一些信息
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错