简体   繁体   English

使用mysqli的if条件插入值

[英]Insert values with if condition with mysqli

I am trying to make register member page. 我正在尝试注册会员页面。 If a new member insert an email which has been already exist, then there will be a notification saying that the email is exist. 如果新成员插入了已经存在的电子邮件,则将出现一条通知,指出该电子邮件已存在。 But if the email has not been exist, the values they insert in the form will be send to database. 但是,如果电子邮件不存在,则它们在表单中插入的值将被发送到数据库。

I don't know what is wrong with my code bellow. 我不知道我的代码波纹管有什么问题。 It just blank and doesn't send anything to databse. 它只是空白,不发送任何数据到数据库。 I need a help. 我需要帮助。

<?php
//conection:
 $link = mysqli_connect(".com","klaudia","intheclaud","elektro") or die("Error " . mysqli_error($link));

//consultation:

 $member_id=$_GET['member_id'];
 $member_name=ucwords(htmlspecialchars($_POST['member_name']));
 $member_email=$_POST['member_email'];
 $member_password=htmlspecialchars($_POST['member_password']);
 $member_phone=$_POST['member_phone'];
 $member_address_satu=ucwords(htmlspecialchars($_POST['member_address_satu']));
 $member_address_dua=ucwords(htmlspecialchars($_POST['member_address_dua']));
 $member_reference=$_POST['member_reference'];



  $query = "SELECT * FROM member_registry WHERE member_email='$member_email '" or die("Error in  the consult.." . mysqli_error($link));

 //execute the query.

 $result = $link->query($query);

 if (mysqli_num_rows($result) > 0) {
    echo "This email you are using has been registered before";
  }
 else {

 mysqli_query($link, "INSERT INTO member_registry (
                                                  'member_id', 
                                                  'member_name', 
                                                  'member_email', 
                                                  'member_password', 
                                                  'member_phone', 
                                                  'member_address_satu', 
                                                  'member_address_dua', 
                                                  'member_reference') 
                                    VALUES (0,1,2,3,4,5,6,7,8)";

?> ?>

I have tried to check the connection and the database. 我试图检查连接和数据库。 Everything works fine here. 这里一切都很好。 When I insert someone name which has been in the table of the database, it will echo that the email already exist. 当我插入数据库表中的某人名称时,它将回显该电子邮件已经存在。 and vice versa. 反之亦然。

    $query = "SELECT * FROM member_registry WHERE member_name='Klaudia '" or die("Error in the consult.." . mysqli_error($link));

    //execute the query.

    $result = $link->query($query);

    if (mysqli_num_rows($result) > 0) {
    echo "This email you are using has been registered before";
    }
    else {
    echo "This email you are using has NOT been registered before";   
     }

[UPDATE] [UPDATE]

      mysqli_query($link, "INSERT INTO member_registry (
                                                  'member_id', 
                                                  'member_name', 
                                                  'member_email', 
                                                  'member_password', 
                                                  'member_phone', 
                                                  'member_address_satu', 
                                                  'member_address_dua', 
                                                  'member_reference') 
                                    VALUES (0,1,2,3,4,5,6,7,8)");
      }
     ?> 

You shouldn't handle this by two separate queries (at least without a transaction). 您不应该通过两个单独的查询来处理这个问题(至少没有事务)。
Instead create a unique index that doesn't allow the same email address twice in the table and check for the specific ER_DUP_ENTRY error code to detect doublets. 而是在表中创建一个不允许两次相同电子邮件地址的唯一索引 ,并检查特定的ER_DUP_ENTRY错误代码以检测重复字符

sscce : sscce

<?php
define('MYSQL_ER_DUP_ENTRY', 1062);

$mysqli = new mysqli('localhost', 'localonly', 'localonly', 'test');
if ($mysqli->connect_errno) {
    trigger_error('connection failed', E_USER_ERROR);
}


$result = $mysqli->query('
    CREATE TEMPORARY TABLE soFoo (
        id int auto_increment,
        email varchar(128),
        primary key(id),
        unique key(email)
    )'
);
if ( !$result) {
    trigger_error('create table failed', E_USER_ERROR);
}


$stmt = $mysqli->prepare('INSERT INTO soFoo (email) VALUES (?)');
if (!$stmt) {
    trigger_error('prepare failed', E_USER_ERROR);
}

$result = $stmt->bind_param("s", $email);
if ( !$result) {
    trigger_error('bind_param failed', E_USER_ERROR);
}

foreach( array('email1', 'email2', 'email1') as $n=>$email ) {
    echo $n, ' ', $email;
    $result = $stmt->execute();
    if ( $result ) {
        echo " ok\r\n";
    }
    else {
        if ( MYSQL_ER_DUP_ENTRY==$stmt->errno ) { // <-- here's the test for the duplicate entry
            echo " duplicate\r\n";
        }
        else {
            var_dump($stmt->errno, $stmt->error);
        }
    }
}

prints 版画

0 email1 ok
1 email2 ok
2 email1 duplicate

You have an error in your syntax. 您的语法有误。 You don't close your function. 您不会关闭功能。 Also you shouldn't use single quotes around your coumn names. 同样,您不应该在列名周围使用单引号。

 mysqli_query($link, "INSERT INTO member_registry ( `member_id`, `member_name`, `member_email`, `member_password`, `member_phone`, `member_address_satu`, `member_address_dua`, `member_reference`) VALUES (0,1,2,3,4,5,6,7,8)"); 

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

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