简体   繁体   English

将电子邮件地址插入数据库并检查它是否已经存在

[英]insert email adress into database and check if it already exists

I got a form input field where the users writes his email to subscribe to a newsletter. 我有一个表单输入字段,用户可以在其中输入他的电子邮件以订阅新闻通讯。 I have to validate the email and also check if it's already in my database. 我必须验证电子邮件,并检查它是否已经存在于数据库中。 Now all checks are correct, but when it actually needs to insert the email into my database it gives me an SYNTAX error on my sql statement. 现在所有检查都是正确的,但是当它实际上需要将电子邮件插入我的数据库时,它在我的sql语句上给我一个SYNTAX错误。 checked it multiple times, tweaked it but I couldn't find the problem. 对其进行了多次检查,调整,但我找不到问题。 i even just added a row to my PhPMyAdmin and copy pasted that code which executed. 我什至只是在我的PhPMyAdmin中添加一行,然后复制粘贴执行的代码。 Still nothing. 依然没有。 So if i test this, it validates the email, then i tried an email that's already in my databank, and it correctly gave the output that it's already in the db, but when it has to go to the laste else, it gives me and error: 因此,如果我对此进行测试,它将验证电子邮件,然后我尝试了数据库中已经存在的电子邮件,并且正确地给出了输出已经存在于数据库中的输出,但是当必须进行最后处理时,它会给我和错误:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near ''email@email.com'' at line 1 在第1行的“ email@email.com”附近,查看与您的MySQL服务器版本相对应的手册以使用正确的语法。

My code below: 我的代码如下:

<form  method="post"  action="#anker" class="footerform" id="anker">

              <input type="email" value="" name="fldEmail" class="brief" id="nieuwsbrief" placeholder="Nieuwsbrief Email" >
                <input type="submit" value="Schrijf me in!" name="subscribe" id="nieuwsbriefsubmit" class="btn"></div>
                <?php
                require('db.php');
                if (isset($_POST["subscribe"])) {
                  if ($_POST["fldEmail"]=="") {
                    echo "<p>error: Vul het veld in aub</p>"; 
                  }else {
                    $fldEmail=$_POST['fldEmail'];
                    $fldEmail =filter_var($fldEmail, FILTER_SANITIZE_EMAIL);
                    $fldEmail= filter_var($fldEmail, FILTER_VALIDATE_EMAIL);
                  if (!$fldEmail) {
                    echo "<p>Je email-adres is niet correct</p>";
                  } else {
                    $fldEmail = stripslashes($fldEmail);
                    $fldEmail = mysqli_real_escape_string($connection, $fldEmail);
                    $testquery = "SELECT fldEmail FROM nieuwsbrief WHERE fldEmail = '$fldEmail'";
                    $result = mysqli_query($connection, $testquery) or die(mysqli_error($connection));
                    $rows = mysqli_num_rows($result);
                    if ($rows > 0) {
                      echo "emailadres zit al in onze databank";
                    } else {
                        $query = "INSERT INTO nieuwsbrief (fldEmail) VALUES '$fldEmail'";
                        $result2 = mysqli_query($connection, $query) or die(mysqli_error($connection));
                        echo "<p>Bedankt om je op de nieuwsbrief in te schrijven</p>";
                    }
                  }
                }
              }
                ?>

            </form>

SQL Sintax is not correct SQL Sintax不正确

Use 采用

$query = "INSERT INTO nieuwsbrief (fldEmail) VALUES ('$fldEmail') ";

Check This 检查这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM