dq8081 2015-08-27 18:22
浏览 18

尝试检查是否已存在具有相同值的电子邮件

Im trying to make so it checks if the email already exists in the database before inserting the data in it.

Here is my code:

<?php
    $servername = "servername";
    $username = "username";
    $password = "password";
    $dbname = "dbname";

    $Mail = $_POST['email'];

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }

    //Inputed in E-Mail Field
    $Mail = $_POST['email'];

    $SearchEmail = $conn->query("SELECT (Mail) FROM betakey WHERE Mail = '$Mail'");

    if ($SearchEmail->num_rows > 0) {
        print "That Email is already registered for the closed alpha";
    }
    else {
        $conn->query("INSERT INTO betakey VALUES ('$Mail')");
    }

    $conn->close();
    ?> 

It doesn't give me any error when i access the page but it also doesn't echo that it exists or inserts the data when it doesn't.

  • 写回答

1条回答 默认 最新

  • dongyin5516 2015-08-27 18:40
    关注

    The problem is in your insert query. An insert query is always structured like shown below.

    INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...) See: http://www.w3schools.com/php/php_mysql_insert.asp

    Applying this to your code, results in the following.

    <?php
    $servername = "servername";
    $username = "username";
    $password = "password";
    $dbname = "dbname";
    
    $Mail = $_POST['email'];
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    
    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }
    
    //Inputed in E-Mail Field
    $Mail = $_POST['email'];
    
    $SearchEmail = $conn->query("SELECT count(Mail) FROM betakey WHERE Mail = '$Mail'");
    
    if ($SearchEmail->num_rows > 0) {
        print "That Email is already registered for the closed alpha";
    }
    else {
        $conn->query("INSERT INTO betakey (Mail) VALUES ('$Mail')");
    }
    
    $conn->close();
    ?> 
    

    And I suggest you take away one of the two '$Mail = $_POST['email'];', because it is useless to declare a variable twice in the same way.

    评论

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序