检查数据是否已存在于wp数据库中-自定义插件

时间:2015-05-26 作者:dea

我正在尝试向wp中的自定义表插入用户输入,我不希望有重复的条目。所以我要检查的代码是:

    global $wpdb;
    $tablename=$wpdb->prefix."students"; //geting our table name with prefix
    $data=$wpdb->get_results("SELECT students_name, students_lastname FROM $tablename");
    foreach($data as $da){
        $da->students_name;
        $da->students_lastname; 
    }
if(isset($_POST[\'submit\'])){
    $name=esc_attr($_POST[\'firstname\']);
    $surname=esc_attr($_POST[\'lastname\']);
    $email=sanitize_email($_POST[\'email\']);

    //checking if the user already exsist so that we don\'t add it twice
    if (
    (is_email($email)) && 
    (($da->students_name != $name) && ($da->students_lastname != $surname)) 
    || (($da->students_name == $name) && ($da->students_lastname != $surname))
    || (($da->students_name != $name) && ($da->students_lastname == $surname)) 
    )
    {
    //assigning the new data to the table rows
        $newdata = array(
        \'students_name\'=>$name,
        \'students_lastname\'=>$surname,
        \'students_email\'=>$email,
        \'students_date\'=>current_time( \'mysql\' ),
        );
        //inserting a record to the database
        $wpdb->insert(
        $tablename,
        $newdata
        );
    //displaying the success message when student is added
        ?>
        <div class="wrap">
            <div class="updated"><p>Student added!</p></div>
        </div>
    <?php
    }else {
        if(!is_email($email)){
            echo \'<div class="error"><p>Invalid e-mail!</p></div>\';
        }else{
        ?
    <div class="wrap">
            <div class="error"><p>Student exsist!</p></div> <!-- wp class error for error notices --->
        </div>
    <?php

        }
    }   
}
}

我的问题是,当用户添加数据时,如果随后立即再次添加相同的数据,则会显示错误消息,而不会添加用户。当用户单击以查看添加的记录(另一个功能),然后单击以添加新学生,则不会显示错误消息,并且同一用户被输入两次或更多次时,就会出现问题。有什么想法吗?

1 个回复
最合适的回答,由SO网友:Rutwick Gangurde 整理而成

您可以大大改进代码。我会将电子邮件地址视为学生的唯一标识,因为我肯定可以有两个名为“John Doe”的学生。

global $wpdb;
$tablename = $wpdb->prefix."students";

if(isset($_POST[\'submit\'])){
    $name = esc_attr($_POST[\'firstname\']);
    $surname = esc_attr($_POST[\'lastname\']);
    $email = sanitize_email($_POST[\'email\']);

    if(!is_email($email)) {
        //Display invalid email error and exit
       echo \'<div class="error"><p>Invalid e-mail!</p></div>\';
       //return or exit
    }

    //Checking to see if the user email already exists
    $datum = $wpdb->get_results("SELECT * FROM $tablename WHERE students_email = \'".$email."\'");

    //Print the $datum object to see how the count is stored in it.
    //I\'m assuming the key is \'count\'

    if($wpdb->num_rows > 0) {
        //Display duplicate entry error message and exit
        ?>
        <div class="wrap">
            <div class="error"><p>Student exsist!</p></div> <!-- wp class error for error notices --->
        </div>
        <?php
        //return or exit
    }

    //Now since the email is unique and valid, lets go ahead and enter it
    //assigning the new data to the table rows
    $newdata = array(
        \'students_name\'=>$name,
        \'students_lastname\'=>$surname,
        \'students_email\'=>$email,
        \'students_date\'=>current_time( \'mysql\' ),
    );
    //inserting a record to the database
    $wpdb->insert(
        $tablename,
        $newdata
    );
    //displaying the success message when student is added
    ?>
    <div class="wrap">
        <div class="updated"><p>Student added!</p></div>
    </div>
<?php }
我避免了许多不必要的条件和循环。试试这个。当成功输入时,使用一些JavaScript清除表单。

更新:我使用了get_results 根据您发布的查询。现在你必须使用$wpdb->num_rows 计算条件中的结果数。

资料来源:https://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Resultshttps://wordpress.org/support/topic/wpdb-mysql_num_rows

结束

相关推荐

Share WordPress Database

WordPress安装是否必须使用专用数据库?我正在构建一个带有自定义页面的网站,可以访问MySQL数据库。但该网站还将安装WordPress。我可以为WordPress和自定义页面使用相同的唯一数据库吗?