Verify record's existence in MySQL database?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sunsolaris2000
    New Member
    • Jan 2012
    • 20

    Verify record's existence in MySQL database?

    I made a small database(1 table) in phpMyAdmin. One of the fields I want to check is "Name".
    I want to verify through PHP if a name that user types in a form exists in the database. In the html a list of names from DB appears, but the user might type wrong the name in the form.
    The problem is the answer whether it exists or not varies.

    I have 2 PHP files:

    Connection.php and welcome.php

    Connection

    Code:
    <html>
    	<head>
    		<title>Project</title>
    	</head>
    	<body>
    
    <?php
        mysql_connect("localhost","root","");
    	mysql_select_db("e-card");
    	
    	$sql=mysql_query("SELECT * FROM person");
    	$dbname="name";
    	#$formula_adr="formula_adr";
    	#$adress="adr";
    	$line=mysql_fetch_assoc($sql);
    	
    	echo'Choose a name to whom you send e-card:<br/><br/>';
    	echo $line[$dbname].'<br/>';
    	while($line=mysql_fetch_assoc($sql))
    		echo $line[$dbname].'<br/>';
    ?>
    	<form action="welcome.php" method="POST">
         Nume: <input type="text" name="fname" />
         <input type="submit" value="Verify existance in DB"/>
    </form>	
    </body>
    </html>
    And welcome:
    Code:
    <?php
       mysql_connect("localhost","root","");
    	mysql_select_db("e-card");
    	
    	$sql=mysql_query("SELECT * FROM persoana");
    	$dbname="name";
    	$formula_adr="formula_adr";
    	$adresa="adr";
    	$linie=mysql_fetch_assoc($sql);
    	
    	if ($_SERVER['REQUEST_METHOD']=='POST' and isset($_POST['fname']))
          {
            $name = $_POST['fname'];
    
    while($line=mysql_fetch_assoc($sql))
       {
     	   if(strcmp($name,$line[$dbname]))
             {
               echo 'Found';
             }
    	   else
            {
    	      echo 'This name doesn't exist in DB';
    	    }
        }
    }
    
    ?>
    THANK YOU IN ADVANCE ^_-
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    There's no need to return all the data and check each name. Just use the WHERE clause in the SQL to filter for just the name the user typed. Then you can check to see if there's at least one record and that will tell you if the name is in the database.

    Comment

    Working...