Jump to content

Check for duplicate input entries


Go to solution Solved by ploppy,

Recommended Posts

I have some code where a user can enter a file name/number separated by a comma. At the moment, I am checking for duplicate entries against a MySql database, which works well.

 

However, I also need a way to check if a user has input the same entry so upon submission, example, a1b2c3,a1b2c3. I can catch this and trigger or echo an alert and halt the execution of the script. If there are no dupe entries, proceed to check db.

 

I have been struggling with this and need to find a way to check before the check db code.I turn to the exerts for some guidance/assistance with this. I would be grateful for any help with this so I can move forward and learn something new in the process. Many thanks.

<?php
if (isset($_POST['file_add'])) {
    
    if (!empty($_POST['file_add'])) {
        $filedata       = split(',', $_POST['file_add'][0]);
        $fileadd        = array();
        $fileduplicates = array();
        foreach ($filedata as $val3) {
            
            if ($val3 != "") {
                $sql = "SELECT custref FROM files WHERE custref='$val3' AND customer = '" . $_SESSION['kt_idcode_usr'] . "'";
                $qry = mysql_query($sql) or die(mysql_error());
                if (mysql_num_rows($qry)) {
                    $fileduplicates[] = $val3;
                } else {
                    $fileadd[] = $val3;
                    $flag      = 1;
                }
                } else {
                $fileerror = '<span style="font-weight:bold;font-size:12px;color: #ff0000;">' . 'FILE ERROR! ' . '</span>' . '<span style="font-weight:normal;color: #000;background-color:#ffa;">' . 'You must enter a file for intake' . '</span>';
                echo "<script language=\"JavaScript\">\n";
                echo 'alert("FILE ERROR:\nYou must enter a file for intake.");';
                echo "
                </script>";
                echo $fileerror;
                }
                }
            if (count($fileadd)) {
                echo 'You wish to add file(s): ' . '
<div style="word-wrap:break-word;white-space: pre-wrap;overflow:auto !important;height: 100px; width: 250px; border: 1px solid #666; background-color: #fff; padding: 4px;">' . '<span style="font-weight:bold;color: #000;">' . join(', ', $fileadd) . '</span>' . '</div>
' . '
<p />
';
            //if(count($duplicates) == 0) // add to session only if we have no duplicates
            $_SESSION['file_add'] = $fileadd; /// change here, do not store $_POST since it can contain duplicates
                }
            if (count($fileduplicates)) {
            
            $filedupeerror = "ERROR:" . " " . "File" . " " . '(' . join(', ', $fileduplicates) . ')' . " " . "This file is already in the database";
            
            echo "<script language=\"JavaScript\">\n";
            echo 'alert("FILE ERROR:\nThis file is already in the database");';
            echo "
            </script>";
            echo $filedupeerror;
        }
    }
}
?>
Edited by ploppy
Link to comment
https://forums.phpfreaks.com/topic/298715-check-for-duplicate-input-entries/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.