Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Checking Form Input to that of a MySQL DB
Old 03-21-2005, 09:53 PM Checking Form Input to that of a MySQL DB
SmokingTequila's Avatar
Master of Disaster

Posts: 344
Location: Richmatic
Trades: 0
I've been working on handcoding some forums for my website for a long time now, and had previously given up (temporarily) because I couldn't get the form to write to the database. Now that I've got that working, I have encountered another error.

The registration script does a few things (well, it should):
1. Take the information from the form
2. Check to see if the username already exists
2a. If it does, prompt the user to try again
2b. If it is unique, continue to step 3
3. Input the information to the database
4. Send the user an email with their username and password

The problem is that the script won't check to see if the username already exists. This could result in 20+ people with the same username, and I definately don't want that. Mass confusion would result. It does everything else, though.

No one that I know seems to see a problem with this script, so I turn to you guys for help (not as a last resort, of course, simply a forgotten resource. :-D)

reg.php
PHP Code:
<?php

// inputting registration data
echo "<b>Register an Account</b>";
echo 
"<br><br><br>By signing up for the Inevitable Forums, you hereby agree not to use these forums to discuss any illegal activities whatsoever, including, but not limited to, warez, hacking and drug dealing / using.  You also agree not to post or discuss pornography.<br><br><br>";
echo 
"<form method=post action=/dev/test/reg2.php>";
echo 
"Username: <input name=uname type=text id=uname><br>";
echo 
"Password: <input name=pword type=password id=pword><br>";
echo 
"eMail Address: <input name=email type=text id=email><br>";
echo 
"<input type=submit value=Join!>";
echo 
"</form><br><br><br>";

?>
reg2.php
PHP Code:
<?php

$result
=mysql_num_rows("SELECT user_name FROM forum_users WHERE user_name=$_GET[uname]");

// if username is already in use, display error
if($result!=0)
    echo(
"Sorry, but the username \"".$_GET[uname]."\"is already in use.  Please hit your browser's back button and try a new name.");
    
// get info from form
$uname $_GET['uname'];
$pword $_GET['pword'];
$email $_GET['email'];

// remove characters that may cause an error
$uname stripslashes($uname);
$pword stripslashes($pword);
$email stripslashes($email);

// prepare password for database
$dbpword md5($pword);
   
// inserting into the database and displaying a "thank you" message
mysql_query("INSERT INTO `forum_users` (user_name,user_pword,user_email,user_regdate) VALUES ('$uname','$dbpword','$email','now()')");    

// information for email to new user
$subject 'theInevitable Forums Account Registration';
$message "Greetings $uname,
This email has been sent to notify you of your account details.  Please keep this information in a safe location!

Username: 
$uname
Password: 
$pword

To log in to your account, head to http://theinevitable.org/forum.php?type=login
Thank you and enjoy your stay,
Trespasser"
;
    
// send the email
mail($email,$subject,$message,"From: Trespasser <trespasser@theinevitable.org>\nX-Mailer: PHP/" phpversion());

?>
Any ideas?
__________________
I might leave in a bodybag, but never in cuffs.
SmokingTequila is offline
Reply With Quote
View Public Profile Visit SmokingTequila's homepage!
 
 
Register now for full access!
Old 03-21-2005, 11:20 PM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Trades: 0
SmokingTequila, you cannot submit a string to mysql_num_rows() -- but rather a mysql resource. Try this code instead:

PHP Code:

<?php 

$result
=mysql_query("SELECT user_name FROM forum_users WHERE user_name=$_GET[uname]"); 
$numrows mysql_num_rows$result );

// if username is already in use, display error 
if( $numrows != 
{
    echo(
"Sorry, but the username \"".$_GET[uname]."\"is already in use.  Please hit your browser's back button and try a new name."); 
}
__________________
—Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 03-22-2005, 06:26 AM
SmokingTequila's Avatar
Master of Disaster

Posts: 344
Location: Richmatic
Trades: 0
It gives me an error (again...)

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/again2/public_html/dev/test/reg2.php on line 12

:-\
__________________
I might leave in a bodybag, but never in cuffs.
SmokingTequila is offline
Reply With Quote
View Public Profile Visit SmokingTequila's homepage!
 
Old 03-22-2005, 09:02 AM
Novice Talker

Posts: 7
Trades: 0
cudnt see any error.
__________________

Please login or register to view this content. Registration is FREE


Get unlimited space and unlimted bandwidth with PHP, MySQL, CPanel and everything you need. No forced ads at all.
aaditya is offline
Reply With Quote
View Public Profile
 
Old 03-22-2005, 10:55 AM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Trades: 0
Ok, change add error checking to your SQL operations by changing the code to match these two lines of code:

PHP Code:

$result
=mysql_query("SELECT user_name FROM forum_users WHERE user_name=$_GET[uname]") or die( mysql_error() );
$numrows mysql_num_rows$result ) or die( mysql_error() ); 
__________________
—Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 03-22-2005, 02:26 PM
SmokingTequila's Avatar
Master of Disaster

Posts: 344
Location: Richmatic
Trades: 0
Still not working, but it appears as if we're getting closer. Supposedly, the query is now a valid resource, but the syntax is incorrect. I still can't find anything myself, but I appreciate you helping. :-)

Notice: Undefined index: uname in /home/again2/public_html/dev/test/reg2.php on line 11
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 11


e: Oh, I just checked, and now it doesn't write to the database.

e2: [b]Thanks to one of my friends, this problem is now resolved. I was missing a set of single quotes in this line:
PHP Code:
$query=mysql_query("SELECT user_name FROM forum_users WHERE user_name='$_POST[uname]'") or die(mysql_error()); 
What now reads user_name='$_POST[uname]' used to read user_name=$_POST[uname]
I feel stupid. XD
__________________
I might leave in a bodybag, but never in cuffs.

Last edited by SmokingTequila; 03-22-2005 at 07:15 PM..
SmokingTequila is offline
Reply With Quote
View Public Profile Visit SmokingTequila's homepage!
 
Reply     « Reply to Checking Form Input to that of a MySQL DB
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.49898 seconds with 12 queries