Hi, I am getting a value submitted from a form. $val= $_POST['field']; Then putting it in an array. $arr= explode("/", $val); Now I have to open a db and check if this value in that field already exists or not. if not, insert it. This is what I am not getting. mysql_connect(localhost,username,password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM $tablename WHERE field='$cat'"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); I know that I need to run a foreach loop to automatically check for each item in $arr but I am completely confused by the number of records in $num now. My question: What code needs to be put in FOREACH loop now. It has to check all records selected for the previous query with the current value in $arr. If a record with $arr value is found, do nothing or else insert a record. foreach ($arr as $value) { } Thank you for any help here.
foreach ($arr as $value) { $query="SELECT * FROM $tablename WHERE field='" . $value . "'"; $result=mysql_query($query); $num=mysql_numrows($result); if ($num == 0) { //Insert } } PHP:
Hi, Thanks so much. It was easy just had to let go the pressure. Thanks again. Here's what I came up with. ( a little longer than what you gave but doesn't involve querying the db over and over) ---------------- Code in first post here. foreach ($arr as $value) { $i=0; $c= "1"; while ($i < $num) { $f = mysql_result($result,$i,"field"); if ($f == $value) $c= "2"; ++$i; } if ($c== "1") { INSERT here } } ----------------- Bye