registration form - check if username is taken

Thread Solved

Join Date: Oct 2006
Posts: 200
Reputation: MaxMumford is an unknown quantity at this point 
Solved Threads: 2
MaxMumford's Avatar
MaxMumford MaxMumford is offline Offline
Posting Whiz in Training

registration form - check if username is taken

 
0
  #1
Jun 27th, 2008
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)

  1. <?php
  2. mysql_connect("", "", "") or die(mysql_error());
  3. mysql_select_db("") or die(mysql_error());;
  4.  
  5. $fname = $_POST["fname"];
  6. $lname = $_POST["lname"];
  7. $uname = $_POST["uname"];
  8. $pword = $_POST["pword"];
  9. $email = $_POST["email"];
  10. //above values are taken from form on previous page...
  11. //first name, last name, username, password and email
  12.  
  13. $subject = "Thankyou for registering with Grafax!";
  14. $from = "**myemail**";
  15. $message = 'You have just registered the following details at Grafax.co.uk:<hr/>
  16. <table bgcolor="#E0E0E0">
  17. <strong>First Name: </strong>'. $fname .'<br/>
  18. <strong>Last Name: </strong>'. $lname .'<br/>
  19. <strong>Username: </strong>'. $uname .'<br/>
  20. <strong>Password: </strong>'. $pword .'</table>
  21. <hr/>
  22. <h3><a href="#">Click here to login</a></h3>';
  23.  
  24.  
  25. //this is where i tried to check the username against whats in the database.
  26. //If $uname entered by user is different to the result from the mysql query
  27. //then insert information. it didnt work.... :P
  28. $dbunames = mysql_query("SELECT * FROM logins WHERE uname='$uname'");
  29. if ($uname != $dbunames)
  30.  
  31.  
  32. {
  33. mysql_query("INSERT INTO ``.`` (`fname`, `lname`, `uname`, `pword`, `email`) VALUES ('$fname', '$lname', '$uname', '$pword', '$email')");
  34. mail($email,$subject,$message,"From:$from\r\nReply-to: $from\r\nContent-type: text/html; charset=us-ascii");
  35. echo "<h1>Successfully added: </h1><br><h3>fname:</h3>";
  36. echo $fname;
  37. echo "<br /><h3>lname:</h3>";
  38. echo $lname;
  39. echo "<br /><h3>uname:</h3>";
  40. echo $uname;
  41. echo "<br /><h3>pword:</h3>";
  42. echo $pword;
  43. echo "<hr>";
  44. echo "<h1>Email sent to ". $email ."</h1>";
  45. }
  46. else
  47. {
  48. echo "Username taken.";
  49. }
  50. ?>
  51. <head>
  52. <title>Insert Outcome</title>
  53. </head>

Thanks all.

Max
Ill solve somebody's thread someday! xD
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: registration form - check if username is taken

 
0
  #2
Jun 27th, 2008
Instead of
if ($uname != $dbunames)
You can do,
  1. if(mysql_num_rows($dbunames) > 0 ) { //check if there is already an entry for that username
  2. echo "Already taken";
  3. } else {
  4. //insert to table
  5. }
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 200
Reputation: MaxMumford is an unknown quantity at this point 
Solved Threads: 2
MaxMumford's Avatar
MaxMumford MaxMumford is offline Offline
Posting Whiz in Training

Re: registration form - check if username is taken

 
0
  #3
Jun 27th, 2008
worked perfectly, thanks
Ill solve somebody's thread someday! xD
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: registration form - check if username is taken

 
0
  #4
Jun 27th, 2008
You are welcome
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 1
Reputation: allihoge is an unknown quantity at this point 
Solved Threads: 0
allihoge allihoge is offline Offline
Newbie Poster

Re: registration form - check if username is taken

 
0
  #5
Jun 6th, 2009
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.
  1. <?php require_once('includes/dbc.php');?>
  2. <?php
  3. // define a constant for the maximum upload size
  4. define ('MAX_FILE_SIZE', 51200);
  5.  
  6. if (array_key_exists('upload', $_POST)) {
  7.  
  8. $gallery_name = $_POST['gallery_name'];
  9. $address = $_POST['address'];
  10. $website = $_POST['website'];
  11. $email = $_POST['email'];
  12. $phone = $_POST['phone'];
  13. $hours = $_POST['hours'];
  14. $description = $_POST['description'];
  15.  
  16.  
  17.  
  18. if(empty($gallery_name)) {
  19. echo 'You need to enter your gallery name'. '<br/>';
  20. $output_form =true;
  21. }
  22.  
  23. if(empty($address)) {
  24. echo 'You need to enter your address'. '<br/>';
  25. $output_form =true;
  26. }
  27.  
  28. if(empty($website)) {
  29. echo 'You need to enter your website'. '<br/>';
  30. $output_form =true;
  31. }
  32.  
  33. if(empty($email)) {
  34. echo 'You need to enter your email address'. '<br/>';
  35. $output_form =true;
  36. }
  37.  
  38. if(empty($phone)) {
  39. echo 'You need to enter your phone number'. '<br/>';
  40. $output_form =true;
  41. }
  42.  
  43. if(empty($hours)) {
  44. echo 'You need to enter your hours of operation'. '<br/>';
  45. $output_form =true;
  46. }
  47.  
  48. if(empty($description)) {
  49. echo 'You need to enter a description'. '<br/>';
  50. $output_form =true;
  51. }
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. if(!empty($gallery_name) && !empty($address) && !empty($email)){
  59.  
  60.  
  61. // define constant for upload folder
  62. define('UPLOAD_DIR', 'images/');
  63. // replace any spaces in original filename with underscores
  64. // at the same time, assign to a simpler variable
  65. $file = str_replace(' ', '_', $_FILES['image']['name']);
  66. // convert the maximum size to KB
  67. $max = number_format(MAX_FILE_SIZE/1024, 1).'KB';
  68. // create an array of permitted MIME types
  69. $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
  70. // begin by assuming the file is unacceptable
  71. $sizeOK = false;
  72. $typeOK = false;
  73.  
  74. // check that file is within the permitted size
  75. if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
  76. $sizeOK = true;
  77. }
  78.  
  79. // check that file is of an permitted MIME type
  80. foreach ($permitted as $type) {
  81. if ($type == $_FILES['image']['type']) {
  82. $typeOK = true;
  83. break;
  84. }
  85. }
  86.  
  87. if ($sizeOK && $typeOK) {
  88. switch($_FILES['image']['error']) {
  89. case 0:
  90. // check if a file of the same name has been uploaded
  91. if (!file_exists(UPLOAD_DIR.$file)) {
  92. // move the file to the upload folder and rename it
  93. $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$file);
  94.  
  95.  
  96. }else {
  97. // get the date and time
  98. ini_set('date.timezone', 'America/New York');
  99. $now = date('Y-m-d-His');
  100. $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$now.$file);
  101. }
  102.  
  103.  
  104.  
  105. if ($success) {
  106.  
  107.  
  108.  
  109.  
  110. $result = '<img src="' . UPLOAD_DIR . $file . '" alt="Gallery Image" /><p>' . $gallery_name . ' ' . $address . '<p>Successfully Added</p>';
  111.  
  112.  
  113. $dbc= mysqli_connect(DB_HOST, DB_USER, DB_PSW, DB_NAME);
  114.  
  115.  
  116.  
  117. // Write the data to the database
  118. $query = "INSERT INTO gallery_info (gallery_name, address, website, email, phone, hours, description, join_date, image)".
  119. "VALUES ('$gallery_name', '$address', '$website', '$email', '$phone', '$hours', '$description', NOW(), '$file')";
  120. mysqli_query($dbc, $query);
  121.  
  122.  
  123.  
  124.  
  125. }
  126. else {
  127. $result = "Error uploading $file. Please try again.";
  128. }
  129. break;
  130. case 3:
  131. $result = "Error uploading $file. Please try again.";
  132. default:
  133. $result = "System error uploading $file. Contact webmaster.";
  134. }
  135. }
  136. elseif ($_FILES['image']['error'] == 4) {
  137. $result = 'No file selected';
  138. }
  139. else {
  140. $result = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png.";
  141. }
  142. }
  143. }
  144.  
  145.  
  146. // if the form has been submitted, display result
  147. if (isset($result)) {
  148. echo "<p>$result</p>";
  149.  
  150. }
  151.  
  152.  
  153.  
  154. ?>
  155. <form method="POST" enctype="multipart/form-data" name="uploadImage" id="uploadImage">
  156. <table border="0" cellpadding="0" cellspacing="0" id="tblsubmit">
  157. <tr>
  158. <th scope="row">Gallery Name:</th>
  159. <td><input type="text" name="gallery_name" value="" /></td>
  160. </tr>
  161. <tr>
  162. <th scope="row">Address:</th>
  163. <td><input type="text" name="address" value="" /></td>
  164. </tr>
  165.  
  166. <tr>
  167. <th scope="row">Website:</th>
  168. <td><input type="text" name="website" value="" /></td>
  169. </tr>
  170.  
  171. <tr>
  172. <th scope="row">Email:</th>
  173. <td><input type="text" name="email" value="" /></td>
  174. </tr>
  175.  
  176. <tr>
  177. <th scope="row">Phone:</th>
  178. <td><input type="text" name="phone" value="" /></td>
  179. </tr>
  180.  
  181. <tr>
  182. <th scope="row">Hours:</th>
  183. <td><input type="text" name="hours" value="" /></td>
  184. </tr>
  185.  
  186. <tr>
  187. <th scope="row">Description:</th>
  188. <td><input type="text" name="description" value="" /></td>
  189. </tr>
  190.  
  191.  
  192.  
  193.  
  194. <tr>
  195. <th scope="row">Image:</th>
  196. <td><input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
  197. <input type="file" name="image" id="image" /> </td>
  198. </tr>
  199. <tr>
  200. <th scope="row">&nbsp;</th>
  201. <td><input type="submit" name="upload" id="button" value="Register Gallery" /></td>
  202. </tr>
  203. </table>
  204.  
  205. </form>
Last edited by Tekmaven; Jun 6th, 2009 at 9:45 pm. Reason: Code Tags
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC