$results = mysql_query("SELECT COUNT(*) FROM ip WHERE grid_no = '$grid_no'"); $grid_no = mysql_result($results,0); if ($grid_no > 0) { echo"The grid_no that you have entered already exists in the db"; exit(); } $sql= "INSERT INTO ip (name, address, city, country, grid_no) VALUES ('$name','$address','$city','$country', '$grid_no')"; $result = mysql_query($sql); //mysql_query($sql); //print_r ($_POST); echo"</br>"; echo "You have added the following details"; //$result = mysql_query("SELECT * FROM user where name='$name'"); $result = mysql_query("SELECT name, address, city, grid_no, product FROM ip order by country DESC"); if ($myrow = mysql_fetch_array($result)) { echo "<table border=1 width=1000>\n"; echo "<tr><td>name</td><td>address</td><td>city</td><td>country</td><td>grid no</tr>\n"; do { printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n", $myrow[0], $myrow[1], $myrow[2], $myrow[3], $myrow[4]); } while ($myrow = mysql_fetch_array($result)); echo "</table>\n"; echo"<br><br>Thank you for entering your details.<br>Please click on main to enter another entry.</br></br>"; } else { echo "Sorry no records were found"; } } I am finding it difficult to display an echo statement for the above that checks for already exisitng grid_no in the db. If I enter a grid_no that is already in my db then it correctly displays me the message" The grid_no that you have entered already exists in the db", however if i try to add a completely new grid number then that entry does not go into the db although the table is displayed with the existing grid_nos present in the db(New entry not added) Please could you help to resolve this issue so as to if I enter a grid_no that is not in the db it also gets added to the db. I have defined the grid_no as UNIQUE in my db structure. Would appreciate your help on this.
Hi I tried to put the following stmt after the INSERT stmt, $result = mysql_query($sql) or die ('Unable to insert' . mysql_error()); This returned me, "Unable to insertDuplicate entry '0' for key 2" For a matter of fact I am not able to figure out the issue as to why does it display this message when I am entering a new grid_no entry to the db. Also as expected if I enter an existing grid_no from the db, the message "Grid_no already exists" is correctly displayed to me. I am new to PHP and would really appreciate if you help me resolve the above issue