0

I'm trying to insert a user input to a custom table in wp and I don't want to have a duplicate entries. So my code to check that is:

    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

        }
    }   
}

}

My problem is, when the user adds the data and if then adds the same data again immediately, the error message is shown and the user isn't added. Problem happens when user clicks to view the added records (another function) and then clicks to add the new student then the error message is not shown and the same user is entered twice or more. Any ideas?

5
  • You can get the result as an array and count it. If not 0 meaning the user already exists. May 26 '15 at 8:12
  • But in the database there can be the same name if it does not have the same last name and it can be the same last name if it does not have the same name. I'm not just checking if the name and last name mach. Can that be also done with counting?
    – dea
    May 26 '15 at 8:15
  • The comment is confusing! Can you illustrate? Another thing, you also need to use some JavaScript to clear your forms. Use a unique identifier for a student, like a roll number or an email address, because names+surnames can be duplicate. May 26 '15 at 8:16
  • This form is in a custom plugin which uses custom table data and custom post type and it's shown in admin menu. Admin adds the students name and last name and email, and if he/she forgets that one student was already added or something like that I don't want to allow duplicate entries. So student with the same name and different email can be added, but if they have the same name and surname it's a duplicate entry. Maybe it would be easier just to check if the emails are different.
    – dea
    May 26 '15 at 8:39
  • There you have it, emails are unique. It is perfectly valid for 2 people with the same names to exist but impossible with the same emails. Read my answer code, it uses the email as the unique ID to solve the duplicate entry issue. May 26 '15 at 8:41
0

You can improve your code a lot. I'll treat the email address as the student's unique identification, since I can definitely have 2 students named '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 }

I have avoided a lot of unnecessary conditions and loops. Try this. Use some JavaScript to clear your form when a successful entry is made.

Update: I have used get_results in accordance with the query you had posted. Now you have to use $wpdb->num_rows to count the number of results in the condition.

Source: https://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results https://wordpress.org/support/topic/wpdb-mysql_num_rows

22
  • I think I need some conditions, because now it still adds the student if it has the same email.
    – dea
    May 26 '15 at 8:57
  • Print the $datum object to see if it returns the proper data. Add an echo statement inside the count if to check if the condition is met. May 26 '15 at 9:03
  • It doesn't show anything, like it doesn't even check the if statement. I tried with var_dump and echo and nothing. Just adds the student.
    – dea
    May 26 '15 at 9:09
  • Then you need to check the SQL query by running it directly on the DB. May 26 '15 at 9:46
  • I fixed it with different SQL query. I did it with $datum = $wpdb->get_results("SELECT COUNT(*) FROM $tablename WHERE students_email = $email", ARRAY_N); and then the if($datum > 0 ) and it works now. Thanks for your help!
    – dea
    May 26 '15 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.