PHP - How to Compare value from HTML form with sql database?

Asked By rana pranav on 09-Apr-13 11:29 PM

I am writing PHP script, I have two textbox containing simple HTML form, two textboxes named t1 and t2.

First textbox is used to insert Student name another one is used to insert roll no of students…

Simple thing I want to do is-

I want to check that if the roll no entered (from HTML form) by someone is already there in the sql database then it should display echo message that roll no already exist else welcome. I have also written the code but when I submit HTML form textboxes becomes blank and does not show any message written in echo…

The HTML code I have written is

<form>

<table>

<tr>

<td>

Enter Name :

<input type="text" name="t1" maxlength="10" size="21"/><br />

</td>

</tr>

<tr>

<td>

Enter RollNo :

<input type="text" maxlength="8" name="t2" /><br /><br />

</td>

</tr>

<tr>

<td align="center">

<input type="submit" name="Click" value="Click" maxlength="8" />

</td>

</tr>

</table>

</form>

PHP Code is

<body>

<?php

$con= mysql_connect("localhost","root","");

mysql_select_db("D",$con); // database name is D

$no = $_POST['t2'];

$sql = "SELECT *FROM Student WHERE Roll_No = '".$id."'"; // table name is Student

$sql1 = mysql_query($sql);

$sql2 = mysql_fetch_array($sql1);

if('$no'==$sql2['Roll_No'])

{

echo" Roll no already exist ";

}

else

{

echo"Welcome";

}

?>

</body>

Please rewrite above code to make it perfect with the explaination and problem so that I can understand...