ploppy Posted October 20, 2015 Share Posted October 20, 2015 (edited) 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 October 20, 2015 by ploppy Quote Link to comment https://forums.phpfreaks.com/topic/298715-check-for-duplicate-input-entries/ Share on other sites More sharing options...
Solution ploppy Posted October 20, 2015 Author Solution Share Posted October 20, 2015 Never mind. sorted it. Quote Link to comment https://forums.phpfreaks.com/topic/298715-check-for-duplicate-input-entries/#findComment-1523790 Share on other sites More sharing options...
benanamen Posted October 20, 2015 Share Posted October 20, 2015 You are using obsolete MySQL code that will not work at all in the latest version of PHP. You need to use PDO with prepared statements. Quote Link to comment https://forums.phpfreaks.com/topic/298715-check-for-duplicate-input-entries/#findComment-1523799 Share on other sites More sharing options...
Barand Posted October 20, 2015 Share Posted October 20, 2015 ... and obsolete PHP functions. split is deprecated too. Quote Link to comment https://forums.phpfreaks.com/topic/298715-check-for-duplicate-input-entries/#findComment-1523804 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.