| | |
search database using php
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 42
Reputation:
Solved Threads: 0
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??
this is really easy one.
use this:
use this:
PHP Syntax (Toggle Plain Text)
//$email is the variable with the email address $sql = "SELECT COUNT(*) FROM `table` WHERE `email` = '" . $email . "'"; $query = mysql_query($sql); $res = mysql_fetch_row($query); $num = $res[0]; if ($num > 0) { //display error } else { //process }
•
•
Join Date: Mar 2008
Posts: 42
Reputation:
Solved Threads: 0
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]
•
•
Join Date: Mar 2008
Posts: 42
Reputation:
Solved Threads: 0
•
•
•
•
post your code and i will integrate my code for you. it would take to long to explain everything and find the problem.
PHP Syntax (Toggle Plain Text)
<?php database connection here //$email is the variable with the email address $sql = "SELECT COUNT(*) FROM members WHERE username = '$_POST[myusername]'"; $query = mysql_query($sql); $result = mysql_fetch_row($query); $num = $result[0]; if ($num > 0) { echo "Already exists"; } else { $sql = sprintf( "INSERT INTO StudentRecords (StudentName, Email) ". "VALUES ('%s','%s')" $result=mysql_query($sql) or die(mysql_error()); $sql= sprintf ("INSERT INTO members (password,username) VALUES ('%s','%s')", $result=mysql_query($sql) or die(mysql_error()); } ?>
no, it only prevents the same username being used by throwing an error.
here is an example of validating a username.
FORM:
PHP CODE:
here is an example of validating a username.
FORM:
PHP Syntax (Toggle Plain Text)
<form action="index.php" method="post"> Username:<input type="text" name="user" /><br /> Password:<input type="password" name="pass" /><br /> <input type="submit" name="submit" value="Submit" /> </form>
PHP CODE:
PHP Syntax (Toggle Plain Text)
<?php if (isset($_POST['submit'])) { $user = $_POST['user']; $pass = $_POST['pass']; $sql = "SELECT COUNT(*) FROM `members` WHERE `username` = '" . $user . "'"; $query = mysql_query($sql); $res = mysql_fetch_row($query); $num = $res[0]; if ($num > 0) { echo 'Username already exists'; } else { $sql = "INSERT INTO `table` (username,password) VALUES ('" . $user . "','" . $pass . "')"; $query = mysql_query($sql); } } ?>
![]() |
Other Threads in the PHP Forum
- Previous Thread: need help querying two separate but related tables!!!
- Next Thread: count function
| Thread Tools | Search this Thread |
apache api array beginner beneath binary broadband broken button cakephp checkbox class cms code countingeverycharactersfromastring crack cron curl database date display dynamic echo email error fcc file files folder form forms freelancing function functions google href htaccess html image include incode insert integration ip javascript joomla limit link login mail match menu method mlm mod_rewrite multiple mysql oop pageing pagerank paypal pdf php problem query radio random recursion recursiveloop remote script search server sessions sms smtp soap source space sql strip_tags subversion support! survey syntax system table template tutorial undefined update upload url validator variable video virus web window.onbeforeunload=closeme; youtube






