search database using php

Thread Solved

Join Date: Mar 2008
Posts: 42
Reputation: aran87 is an unknown quantity at this point 
Solved Threads: 0
aran87 aran87 is offline Offline
Light Poster

search database using php

 
0
  #1
Apr 12th, 2008
hello people ive got a signup form which allows people to signup and login but to signup they enter there email address in which is then used as there login name, its all working but when the next user signs in they mite type the same email address in so i need the sql statement to search the database and check if that username already exists if it does display a message and then re enter new username, is it some sort of search statement??
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: search database using php

 
0
  #2
Apr 13th, 2008
this is really easy one.

use this:

  1. //$email is the variable with the email address
  2.  
  3. $sql = "SELECT COUNT(*) FROM `table` WHERE `email` = '" . $email . "'";
  4. $query = mysql_query($sql);
  5. $res = mysql_fetch_row($query);
  6. $num = $res[0];
  7. if ($num > 0) {
  8. //display error
  9. }
  10. else {
  11. //process
  12. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 42
Reputation: aran87 is an unknown quantity at this point 
Solved Threads: 0
aran87 aran87 is offline Offline
Light Poster

Re: search database using php

 
0
  #3
Apr 13th, 2008
hello ive put this code when the user signs up when they fill in the details then they click submit which should then display this username is already taken. The signup page is an html page so ive put the php code at the top of the page by using <?

<?
//connect to database
//select database ive got the connections by i aint put them on here
$result = mysql_query($query, $connection);

//is $email the textbox name?
$sql = "SELECT COUNT(*) FROM people WHERE `email` = '" . $email . "'";
$query = mysql_query($sql);
$res = mysql_fetch_row($query);
$num = $res[0];
if ($num > 0) {
//display error
}
else {
//process
}


// Close connection ! (please !)
mysql_close($connection);
?>
[/code]
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 42
Reputation: aran87 is an unknown quantity at this point 
Solved Threads: 0
aran87 aran87 is offline Offline
Light Poster

Re: search database using php

 
0
  #4
Apr 13th, 2008
still not working
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: search database using php

 
0
  #5
Apr 13th, 2008
post your code and i will integrate my code for you. it would take to long to explain everything and find the problem.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 42
Reputation: aran87 is an unknown quantity at this point 
Solved Threads: 0
aran87 aran87 is offline Offline
Light Poster

Re: search database using php

 
0
  #6
Apr 14th, 2008
Originally Posted by kkeith29 View Post
post your code and i will integrate my code for you. it would take to long to explain everything and find the problem.
The code works but its just inserting the same username even if it exists S_POST comes from the previous page

  1.  
  2. <?php
  3. database connection here
  4.  
  5. //$email is the variable with the email address
  6.  
  7. $sql = "SELECT COUNT(*) FROM members WHERE username = '$_POST[myusername]'";
  8.  
  9. $query = mysql_query($sql);
  10. $result = mysql_fetch_row($query);
  11. $num = $result[0];
  12. if ($num > 0) {
  13. echo "Already exists";
  14. }
  15. else {
  16. $sql = sprintf(
  17. "INSERT INTO StudentRecords (StudentName, Email) ".
  18. "VALUES ('%s','%s')"
  19. $result=mysql_query($sql) or die(mysql_error());
  20.  
  21. $sql= sprintf ("INSERT INTO members (password,username) VALUES ('%s','%s')",
  22. $result=mysql_query($sql) or die(mysql_error());
  23. }
  24.  
  25. ?>
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 42
Reputation: aran87 is an unknown quantity at this point 
Solved Threads: 0
aran87 aran87 is offline Offline
Light Poster

Re: search database using php

 
0
  #7
Apr 14th, 2008
no errors but dont check for some reason
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: search database using php

 
0
  #8
Apr 14th, 2008
you can make that column of the mysql table 'unique'. that will make sure the same username cannot be inserted twice. as for the code you posted, i need the form also so I can see the whole process so I can fix it.
Last edited by kkeith29; Apr 14th, 2008 at 7:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 63
Reputation: sukhy_1 is an unknown quantity at this point 
Solved Threads: 3
sukhy_1 sukhy_1 is offline Offline
Junior Poster in Training

Re: search database using php

 
0
  #9
Apr 14th, 2008
Hello if i use the mysql unquie column can i add a error in php saying username already taken?? im doing the same thing
Last edited by sukhy_1; Apr 14th, 2008 at 7:23 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: search database using php

 
0
  #10
Apr 14th, 2008
no, it only prevents the same username being used by throwing an error.

here is an example of validating a username.

FORM:
  1. <form action="index.php" method="post">
  2. Username:<input type="text" name="user" /><br />
  3. Password:<input type="password" name="pass" /><br />
  4. <input type="submit" name="submit" value="Submit" />
  5. </form>

PHP CODE:
  1. <?php
  2.  
  3. if (isset($_POST['submit'])) {
  4. $user = $_POST['user'];
  5. $pass = $_POST['pass'];
  6. $sql = "SELECT COUNT(*) FROM `members` WHERE `username` = '" . $user . "'";
  7. $query = mysql_query($sql);
  8. $res = mysql_fetch_row($query);
  9. $num = $res[0];
  10. if ($num > 0) {
  11. echo 'Username already exists';
  12. }
  13. else {
  14. $sql = "INSERT INTO `table` (username,password) VALUES ('" . $user . "','" . $pass . "')";
  15. $query = mysql_query($sql);
  16. }
  17. }
  18.  
  19. ?>
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC