Page 1 of 1

Difficult to use echo stmt to display already exis rec in db

Posted: Wed Jun 25, 2008 1:37 pm
by vishals81
$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.

Re: Difficult to use echo stmt to display already exis rec in db

Posted: Wed Jun 25, 2008 11:49 pm
by WebbieDave
Try:

Code: Select all

$result = mysql_query($sql) or die ('Unable to insert' . mysql_error());
on the INSERT statement to see if the database is returning an error. After debugging is complete, remove the call to mysql_error.

Re: Difficult to use echo stmt to display already exis rec in db

Posted: Thu Jun 26, 2008 2:40 am
by vishals81
I have added the line to the piece of code after "INSERT" stmt. If now I enter a new grid_no in the form and try to submit it to the db the following message is displayed.

"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.