使用mysqli在if条件中插入值

时间:2014-11-06 13:38:06

标签: php mysql mysqli

我正在尝试制作注册会员页面。如果新成员插入已存在的电子邮件,则会出现通知,表明该电子邮件已存在。但如果电子邮件尚未存在,则表单中插入的值将发送到数据库。

我不知道我的代码有什么问题。它只是空白,不会向数据库发送任何内容。我需要帮助。

<?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)";

&GT?;

我试过检查连接和数据库。这里一切都很好。当我插入已经在数据库表中的某个名称时,它将回显该电子邮件已经存在。反之亦然。

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

[更新]

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

2 个答案:

答案 0 :(得分:1)

您不应该通过两个单独的查询处理此问题(至少没有交易) 相反,create a unique index不允许在表格中使用相同的电子邮件地址两次,并检查特定的ER_DUP_ENTRY错误代码以检测双联。

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

打印

0 email1 ok
1 email2 ok
2 email1 duplicate

答案 1 :(得分:0)

语法错误。你没有关闭你的功能。 此外,你不应该在你的coumn名字周围使用单引号。

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