PHP - how to compare user entered roll no to database value in php?

Asked By rana pranav on 05-Apr-13 12:02 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(i.e 1) by someone is already there in the sql  database(i.e 1) 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 focus of EXPLAINATION and PROBLEM so that I can understand