By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,871 Members | 2,444 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,871 IT Pros & Developers. It's quick & easy.

Verify record's existence in MySQL database?

sunsolaris2000
P: 20
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

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.     <head>
  3.         <title>Project</title>
  4.     </head>
  5.     <body>
  6.  
  7. <?php
  8.     mysql_connect("localhost","root","");
  9.     mysql_select_db("e-card");
  10.  
  11.     $sql=mysql_query("SELECT * FROM person");
  12.     $dbname="name";
  13.     #$formula_adr="formula_adr";
  14.     #$adress="adr";
  15.     $line=mysql_fetch_assoc($sql);
  16.  
  17.     echo'Choose a name to whom you send e-card:<br/><br/>';
  18.     echo $line[$dbname].'<br/>';
  19.     while($line=mysql_fetch_assoc($sql))
  20.         echo $line[$dbname].'<br/>';
  21. ?>
  22.     <form action="welcome.php" method="POST">
  23.      Nume: <input type="text" name="fname" />
  24.      <input type="submit" value="Verify existance in DB"/>
  25. </form>    
  26. </body>
  27. </html>
  28.  
And welcome:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.    mysql_connect("localhost","root","");
  3.     mysql_select_db("e-card");
  4.  
  5.     $sql=mysql_query("SELECT * FROM persoana");
  6.     $dbname="name";
  7.     $formula_adr="formula_adr";
  8.     $adresa="adr";
  9.     $linie=mysql_fetch_assoc($sql);
  10.  
  11.     if ($_SERVER['REQUEST_METHOD']=='POST' and isset($_POST['fname']))
  12.       {
  13.         $name = $_POST['fname'];
  14.  
  15. while($line=mysql_fetch_assoc($sql))
  16.    {
  17.         if(strcmp($name,$line[$dbname]))
  18.          {
  19.            echo 'Found';
  20.          }
  21.        else
  22.         {
  23.           echo 'This name doesn't exist in DB';
  24.         }
  25.     }
  26. }
  27.  
  28. ?>
THANK YOU IN ADVANCE ^_-
Jan 5 '12 #1

✓ answered by Rabbit

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.

Share this Question
Share on Google+
1 Reply


Rabbit
Expert Mod 10K+
P: 12,366
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.
Jan 5 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.