ploppy Posted May 8, 2008 Share Posted May 8, 2008 hi. i would like some help with checking for duplicate record before it is inserted into db and print some kind of mssage. here is the code i have so far and would appreciate your comments. if (isset($_POST['file_ret'])) { // extract the array of list choices from the post data $file_ret = $_POST['file_ret']; $numChoices = count($file_ret); $insertVal = ""; for($i = 0; $i < $numChoices; $i++) { // concatenate each array value to one string variable $insertVal .= $file_ret[$i]; // put a comma after all but the last value if($i != $numChoices - 1) { $insertVal .= ","; } } echo 'Fil(es) Selected: ' . '<span style="font-weight:bold;color: #000;">' . $insertVal . '</span>' . '<p />'; $_SESSION['file_ret'] = $_POST['file_ret'];} elseif ($insertVal <= 0) { echo '<span class="stop_error">' . 'You haven\'t chosen a file. you must select a file' . '</span>' . '<p />';} this is the page that the client can print for there records. and here is the page that inserts into db //File retrival $activity = $_POST['file']; if ($activity == 'File Retrival') { foreach ($_SESSION['file_ret'] as $key=>$value) { $query = "UPDATE boxes SET status = '$status' WHERE customer = '$customer' AND department = '$dept' AND custref = '$value'"; mysql_query($query) or die('Error, query failed'); } } many thanks Link to comment https://forums.phpfreaks.com/topic/104762-check-for-duplicate-record/ Share on other sites More sharing options...
ploppy Posted May 9, 2008 Author Share Posted May 9, 2008 no replies :-( have i posted question wrong? Link to comment https://forums.phpfreaks.com/topic/104762-check-for-duplicate-record/#findComment-536563 Share on other sites More sharing options...
moon 111 Posted May 9, 2008 Share Posted May 9, 2008 No, you just haven't waited long enough. Or was that a 'bump'? Link to comment https://forums.phpfreaks.com/topic/104762-check-for-duplicate-record/#findComment-536567 Share on other sites More sharing options...
xenophobia Posted May 9, 2008 Share Posted May 9, 2008 I don't see any insert statement in your SQL. Anyway, if you wish to check for duplicated records, simply put a select statement with a WHERE clause to specify the condition on the searching. Then use mysql_num_rows($query) to get the return rows. If the rows > 0, which mean duplicated. Thats all. Link to comment https://forums.phpfreaks.com/topic/104762-check-for-duplicate-record/#findComment-536570 Share on other sites More sharing options...
ploppy Posted May 9, 2008 Author Share Posted May 9, 2008 @xeno this is what i have at the moment. <?php $insertVal = ""; if (isset($_POST['box_add'])) { // extract the array of list choices from the post data $box_add = $_POST['box_add']; $numChoices = count($box_add); for($i = 0; $i < $numChoices; $i++) { // concatenate each array value to one string variable $insertVal .= "'" . $box_add[$i] . "',"; } // for($i = 0; $i < $numChoices; $i++) } // if (isset($_POST['box_add'])) $sql = " select ref from box where ref IN( $insertVal '0' )" ; $res = mysql_query($sql) or die("error on $sql : " . mysql_error()); if (mysql_num_rows($res) != 0) { print "record already in database"; } else { if ($insertVal == '') { echo '<span class="stop_error">' . 'You haven\'t added a box. you must add a box' . '</span>' . '<p />'; } else { echo 'Fil(es) Added: ' . '<span style="font-weight:bold;color: #000;">' . $insertVal . '</span>' . '<p />'; $_SESSION['box_add'] = $_POST['box_add'];} } ?> what is supposed to happen, is that if the client inputs a value the print "record already in database" is printed. which it does ok. if the value is left blank, what should then happen is the message echo 'Fil(es) Added: ' . '<span style="font-weight:bold;color: #000;">' . $insertVal . '</span>' . '<p />'; should be printed. but instead it prints '',. how can i get this to work. thanks @moon I didn't know there was a time limit. how long am i supposed to wait? 24hrs not long enough? cheers Link to comment https://forums.phpfreaks.com/topic/104762-check-for-duplicate-record/#findComment-536630 Share on other sites More sharing options...
ploppy Posted May 9, 2008 Author Share Posted May 9, 2008 {bump} Link to comment https://forums.phpfreaks.com/topic/104762-check-for-duplicate-record/#findComment-536717 Share on other sites More sharing options...
xenophobia Posted May 10, 2008 Share Posted May 10, 2008 Alright let's clarify your problems. You probably had something like this in your HTML: <input type="i don't know what is your type" name="box_add[]" /> Am I right? So your purpose was wanted to get these list of "box_add" to check whether is already existed in the database or not. Right? If it is existed, you wish to print : "record already in database!" Else, you will want all these data stored into the SESSION named 'box_add' Ah, also you want to put a validation whereby if the user did't "add the box", you will want to warn them, right? So here is my suggestion code, hope you can catch some idea: <?php if(isset($_POST['box_add']) && sizeof($_POST['box_add']) > 0) { // To make sure the user did include the 'box' // We will need this variable later on! $shall_we_proceed = true; // Here, we will loop each box's value and query it to check whether exist or not. // Ofcourse, it might be slower than yours previous posting, whereby using only 1 statement. // Is up to you to use which algorithm you wish to validate it. ^^ foreach($_POST['box_add'] as $val) { $sql = "SELECT * FROM box WHERE ref='$val';"; $qry = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($qry)) { echo "Records Existed!!!"; $shall_we_proceed = false; break; // Quit the loop } } if($shall_we_proceed) { // Okay! We can proceed! $_SESSION['box_add'] = $_POST['box_add']; } } else { // Error! echo "hey! please add box!"; } ?> Hope it work, i did't test out . Link to comment https://forums.phpfreaks.com/topic/104762-check-for-duplicate-record/#findComment-537376 Share on other sites More sharing options...
ploppy Posted May 10, 2008 Author Share Posted May 10, 2008 xeno. thanks for code. however, it displays ok if a record exists, but it doesn't add box message. also, i need to echo to the page what the client has input. i tried echo $val; but it didn't work. can you revamp? cheers Link to comment https://forums.phpfreaks.com/topic/104762-check-for-duplicate-record/#findComment-537477 Share on other sites More sharing options...
ploppy Posted May 10, 2008 Author Share Posted May 10, 2008 {bump} Link to comment https://forums.phpfreaks.com/topic/104762-check-for-duplicate-record/#findComment-537847 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.