473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Verify record's existence in MySQL database?

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

1 1374
Rabbit
12,516 Expert Mod 8TB
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

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

Similar topics

3
by: Eric Linders | last post by:
Hi, Pretty new to PHP/MySQL, but I have some programming experience in a past life. Hopefully you can help me out as I learn PHP/MySQL! :-) I have a MySQL database that includes customer...
6
by: Eric W. Holzapfel | last post by:
Hello PHP group, I have mysql running on a linux box. I have apache (2.0.4) running on a diff linux box. I have php 4.X. I can connect to the mysql db using odbc from a windows box, but...
6
by: TheDude5B | last post by:
Hi, i dont know if this is the best place to post this, if there is a better forum could you let me know. My problem: i am new to asp.net and am using a MySQL database to hold my information. I...
6
by: Bob Sanderson | last post by:
I have created a MySQL database for my company which is accessed by PHP pages. I would like to permit some users to edit the records but allow others read-only access. However, I don't want to have...
0
by: nassim.bouayad.agha | last post by:
Unique table version record Hello, I am using a table record to store informations about database dump,I use this table : CREATE TABLE dump_version( count BIGINT unsigned NOT NULL DEFAULT 0,...
0
by: Hans | last post by:
Hi, That's my first time to send mail to this address for asking help. Sorry for my poor english firstly. My case is like this: Many guys are using a mysql database, each guy has a database...
1
by: JustMe | last post by:
Hi all, Although I've got quite a lot of experience with php and (M/My)SQL, I only used both within an online php script. These last few months I bought an option with my webhost to access...
4
by: dirk | last post by:
Hey, I'm new to php and I'm trying to write some php code so that I can insert data into a mysql database using html forms. I've got two text forms and a submit button. When entering data and...
4
by: Gary Baardman | last post by:
I am having a few problems returning records from a mySQL database using PHP. Firstly I must say I am trying to create a video album using MySQL, PHP and using pagination. My first problem is...
4
by: Francesca | last post by:
Hi everybody, I am a real newbie in both perl and relational databases like mysql, and I have been banging my head on the wall trying to understand how to populate a mysql database using an Excel...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.