| | |
registration form - check if username is taken
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
Hi guys,
I have a simple registration form and have had trouble getting it to check if the username is already taken in the database. This is what I have at the moment:
(the code i tried to use to make the check is about half way down)
Thanks all.
Max
I have a simple registration form and have had trouble getting it to check if the username is already taken in the database. This is what I have at the moment:
(the code i tried to use to make the check is about half way down)
PHP Syntax (Toggle Plain Text)
<?php mysql_connect("", "", "") or die(mysql_error()); mysql_select_db("") or die(mysql_error());; $fname = $_POST["fname"]; $lname = $_POST["lname"]; $uname = $_POST["uname"]; $pword = $_POST["pword"]; $email = $_POST["email"]; //above values are taken from form on previous page... //first name, last name, username, password and email $subject = "Thankyou for registering with Grafax!"; $from = "**myemail**"; $message = 'You have just registered the following details at Grafax.co.uk:<hr/> <table bgcolor="#E0E0E0"> <strong>First Name: </strong>'. $fname .'<br/> <strong>Last Name: </strong>'. $lname .'<br/> <strong>Username: </strong>'. $uname .'<br/> <strong>Password: </strong>'. $pword .'</table> <hr/> <h3><a href="#">Click here to login</a></h3>'; //this is where i tried to check the username against whats in the database. //If $uname entered by user is different to the result from the mysql query //then insert information. it didnt work.... :P $dbunames = mysql_query("SELECT * FROM logins WHERE uname='$uname'"); if ($uname != $dbunames) { mysql_query("INSERT INTO ``.`` (`fname`, `lname`, `uname`, `pword`, `email`) VALUES ('$fname', '$lname', '$uname', '$pword', '$email')"); mail($email,$subject,$message,"From:$from\r\nReply-to: $from\r\nContent-type: text/html; charset=us-ascii"); echo "<h1>Successfully added: </h1><br><h3>fname:</h3>"; echo $fname; echo "<br /><h3>lname:</h3>"; echo $lname; echo "<br /><h3>uname:</h3>"; echo $uname; echo "<br /><h3>pword:</h3>"; echo $pword; echo "<hr>"; echo "<h1>Email sent to ". $email ."</h1>"; } else { echo "Username taken."; } ?> <head> <title>Insert Outcome</title> </head>
Thanks all.
Max
Ill solve somebody's thread someday! xD
Instead of
You can do,
•
•
•
•
if ($uname != $dbunames)
php Syntax (Toggle Plain Text)
if(mysql_num_rows($dbunames) > 0 ) { //check if there is already an entry for that username echo "Already taken"; } else { //insert to table }
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: Jun 2009
Posts: 1
Reputation:
Solved Threads: 0
Please Help! I am having the same problem, but my form is a little more complicated, and instead of username, i do not want duplicate gallery names. i tried the msql num rows and maybe i just don't know where to put it in the code. Also my form does not go to a different page. Also, is there a better way to validate if the fields are empty? Also, i cannot get my form to go away after the user submits a gallery- the form is still there. Thanks in advance.
PHP Syntax (Toggle Plain Text)
<?php require_once('includes/dbc.php');?> <?php // define a constant for the maximum upload size define ('MAX_FILE_SIZE', 51200); if (array_key_exists('upload', $_POST)) { $gallery_name = $_POST['gallery_name']; $address = $_POST['address']; $website = $_POST['website']; $email = $_POST['email']; $phone = $_POST['phone']; $hours = $_POST['hours']; $description = $_POST['description']; if(empty($gallery_name)) { echo 'You need to enter your gallery name'. '<br/>'; $output_form =true; } if(empty($address)) { echo 'You need to enter your address'. '<br/>'; $output_form =true; } if(empty($website)) { echo 'You need to enter your website'. '<br/>'; $output_form =true; } if(empty($email)) { echo 'You need to enter your email address'. '<br/>'; $output_form =true; } if(empty($phone)) { echo 'You need to enter your phone number'. '<br/>'; $output_form =true; } if(empty($hours)) { echo 'You need to enter your hours of operation'. '<br/>'; $output_form =true; } if(empty($description)) { echo 'You need to enter a description'. '<br/>'; $output_form =true; } if(!empty($gallery_name) && !empty($address) && !empty($email)){ // define constant for upload folder define('UPLOAD_DIR', 'images/'); // replace any spaces in original filename with underscores // at the same time, assign to a simpler variable $file = str_replace(' ', '_', $_FILES['image']['name']); // convert the maximum size to KB $max = number_format(MAX_FILE_SIZE/1024, 1).'KB'; // create an array of permitted MIME types $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png'); // begin by assuming the file is unacceptable $sizeOK = false; $typeOK = false; // check that file is within the permitted size if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) { $sizeOK = true; } // check that file is of an permitted MIME type foreach ($permitted as $type) { if ($type == $_FILES['image']['type']) { $typeOK = true; break; } } if ($sizeOK && $typeOK) { switch($_FILES['image']['error']) { case 0: // check if a file of the same name has been uploaded if (!file_exists(UPLOAD_DIR.$file)) { // move the file to the upload folder and rename it $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$file); }else { // get the date and time ini_set('date.timezone', 'America/New York'); $now = date('Y-m-d-His'); $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$now.$file); } if ($success) { $result = '<img src="' . UPLOAD_DIR . $file . '" alt="Gallery Image" /><p>' . $gallery_name . ' ' . $address . '<p>Successfully Added</p>'; $dbc= mysqli_connect(DB_HOST, DB_USER, DB_PSW, DB_NAME); // Write the data to the database $query = "INSERT INTO gallery_info (gallery_name, address, website, email, phone, hours, description, join_date, image)". "VALUES ('$gallery_name', '$address', '$website', '$email', '$phone', '$hours', '$description', NOW(), '$file')"; mysqli_query($dbc, $query); } else { $result = "Error uploading $file. Please try again."; } break; case 3: $result = "Error uploading $file. Please try again."; default: $result = "System error uploading $file. Contact webmaster."; } } elseif ($_FILES['image']['error'] == 4) { $result = 'No file selected'; } else { $result = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png."; } } } // if the form has been submitted, display result if (isset($result)) { echo "<p>$result</p>"; } ?> <form method="POST" enctype="multipart/form-data" name="uploadImage" id="uploadImage"> <table border="0" cellpadding="0" cellspacing="0" id="tblsubmit"> <tr> <th scope="row">Gallery Name:</th> <td><input type="text" name="gallery_name" value="" /></td> </tr> <tr> <th scope="row">Address:</th> <td><input type="text" name="address" value="" /></td> </tr> <tr> <th scope="row">Website:</th> <td><input type="text" name="website" value="" /></td> </tr> <tr> <th scope="row">Email:</th> <td><input type="text" name="email" value="" /></td> </tr> <tr> <th scope="row">Phone:</th> <td><input type="text" name="phone" value="" /></td> </tr> <tr> <th scope="row">Hours:</th> <td><input type="text" name="hours" value="" /></td> </tr> <tr> <th scope="row">Description:</th> <td><input type="text" name="description" value="" /></td> </tr> <tr> <th scope="row">Image:</th> <td><input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" /> <input type="file" name="image" id="image" /> </td> </tr> <tr> <th scope="row"> </th> <td><input type="submit" name="upload" id="button" value="Register Gallery" /></td> </tr> </table> </form>
Last edited by Tekmaven; Jun 6th, 2009 at 9:45 pm. Reason: Code Tags
![]() |
Similar Threads
- ASP.NET Registration Page (ASP.NET)
- Email code problem (PHP)
- Help Spammers Are Killing My Forum!!! (Social Media and Online Communities)
- doesnt show anything (PHP)
- Perl question (Perl)
- Check existing users in the database (ASP)
- php error HELP!!! (PHP)
- MYSQL/PHP variable check (MySQL)
- Doesn't check if username or email exits in database properly (ASP.NET)
Other Threads in the PHP Forum
- Previous Thread: mysql datas is shown with a delay after adding
- Next Thread: Making PHP Extensions in C++
| Thread Tools | Search this Thread |
.htaccess action ajax apache api array auto beginner binary bounce broken cakephp checkbox class cms code cron curl database date display dynamic echo email error errorlog file files folder form format forms function functions google href htaccess html image include insert integration interactive ip java javascript joomla limit link login loop mail malfunctioning masterthesis menu mlm mod_rewrite multiple mysql nodes oop paypal pdf php popup problem query radio ram random recursion reference regex remote return script search server sessions sms soap source space sql syntax system table tutorial unset update upload url validation validator variable video web websitecontactform xml youtube






