Go to Post It's the corollary that you pick them no matter what shape they start the event in. Pink is known for showing up at their 1st regional with the robot in an unfinished state. And somehow, magically by the end of qualifications it just works, and they win. - Nuttyman54 [more]
Home
Go Back   Chief Delphi > Technical > IT / Communications > Website Design/Showcase
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 23-05-2004, 16:41
Raven_Writer's Avatar
Raven_Writer Raven_Writer is offline
2004 Detroit & Pittsburgh Winners
AKA: Eric Hansen
FRC #0005 (RoboCards)
Team Role: Mentor
 
Join Date: Jan 2003
Rookie Year: 2002
Location: Melvindale
Posts: 1,549
Raven_Writer is just really niceRaven_Writer is just really niceRaven_Writer is just really niceRaven_Writer is just really niceRaven_Writer is just really nice
Send a message via ICQ to Raven_Writer Send a message via AIM to Raven_Writer Send a message via MSN to Raven_Writer Send a message via Yahoo to Raven_Writer
MySQL/PHP Query question

I've decided to create a comment script for a beginning project. Right now, I'm working on the registering part of the script. My problem is checking to see if the username already exists within the database. Here's the code I'm using right now:

PHP Code:
<?
$user 
$_POST['username'];
$pass $_POST['password'];
$check $_POST['passcheck'];
$conn mysql_connect(/* connect to mysql */);
if(!
$conn){
 echo(
"Unable to connect to MySQL: "mysql_error());
 exit;
}
$db mysql_select_db(/* try to connect to database */);
if(!
$db){
 echo(
"Unable to select database: "mysql_error());
 exit;
}
 
/* ::: LOOK BELOW ::: */
$result mysql_query("SELECT * FROM information WHERE user = '"$user ."'");
if(
$result != ""){
 echo(
"Username already exists in database.  Please choose a different username to continue.");
 exit;
}
/* ^^^^  LOOK ABOVE ^^^^ */
?>
I can connect to MySQL and the database just fine.
__________________
AIM: wisprmylastbreth
EMail: nightskywriter@gmail.com
Y!: synsoflife

"ai yoru ga" -- "Love the nights"
  #2   Spotlight this post!  
Unread 23-05-2004, 20:57
Guest
 
Posts: n/a
Re: MySQL/PHP Query question

Two suggestions:

1. Instead of using ($result !="") use:
PHP Code:
(mysql_num_rows($result)>0
2. (Only try 2 if 1 does not work) Put double quotes instead of single quotes around the user in the query.
  #3   Spotlight this post!  
Unread 24-05-2004, 09:52
mtrawls's Avatar
mtrawls mtrawls is offline
I am JVN! (John von Neumann)
#0122 (NASA Knights)
Team Role: Programmer
 
Join Date: Mar 2003
Location: Hampton, VA
Posts: 295
mtrawls is a splendid one to beholdmtrawls is a splendid one to beholdmtrawls is a splendid one to beholdmtrawls is a splendid one to beholdmtrawls is a splendid one to beholdmtrawls is a splendid one to beholdmtrawls is a splendid one to behold
Send a message via AIM to mtrawls
Re: MySQL/PHP Query question

Now, I'm no PHP-guru, actually I've never used it before ... but from my experience using perl/cgi I tend to be a bit paranoid. But safely so. The cardinal rule is never trust the user; taint check, taint check, taint check! Not being familiar with PHP, like I said, I don't know if it's possible to do any sort of attack against a script of this nature, but I suspect there is. (Granted the short snippet you show may not suceptible, it's still a good idea to check your input.) E.g., if someone is entering a user-name make sure it only contains allowed characters and is correctly formatted. In perl, anyway, you'd be suprised how easy it is to pass 'rm -r .*' as an argument to something and watch for disastrous effects!

Just a friendly security reminder ... it never hurts to start good habbits early (since you said you were learning PHP). Maybe someone more experienced can comment about specific security woes that you'll have to look out for. But remember that paranoia is good.
  #4   Spotlight this post!  
Unread 24-05-2004, 11:31
Robert Hafner's Avatar
Robert Hafner Robert Hafner is offline
FIRST Alumni
no team
 
Join Date: Mar 2003
Rookie Year: 2000
Location: Springfield. MA
Posts: 34
Robert Hafner is on a distinguished road
Send a message via AIM to Robert Hafner Send a message via MSN to Robert Hafner Send a message via Yahoo to Robert Hafner
Re: MySQL/PHP Query question

Change
PHP Code:
if($result != ""){
 echo(
"Username already exists in database.  Please choose a different username to continue.");
 exit; 
to

PHP Code:
if($result){
  echo(
"Username already exists in database.  Please choose a different username to continue.");
 exit; 


If you need any more MySQL or PHP help, email me, go to my team's website (which was built by me using PHP/MySQL), or check out this project I'm heading, supermod.org.
  #5   Spotlight this post!  
Unread 24-05-2004, 15:18
Raven_Writer's Avatar
Raven_Writer Raven_Writer is offline
2004 Detroit & Pittsburgh Winners
AKA: Eric Hansen
FRC #0005 (RoboCards)
Team Role: Mentor
 
Join Date: Jan 2003
Rookie Year: 2002
Location: Melvindale
Posts: 1,549
Raven_Writer is just really niceRaven_Writer is just really niceRaven_Writer is just really niceRaven_Writer is just really niceRaven_Writer is just really nice
Send a message via ICQ to Raven_Writer Send a message via AIM to Raven_Writer Send a message via MSN to Raven_Writer Send a message via Yahoo to Raven_Writer
Re: MySQL/PHP Query question

Thank you all for your help. But while waiting for a reply, I was messing around with it, trying to see if I could fix it myself. I still have the same trouble, and here's my code now:

PHP Code:
$result mysql_query("SELECT `user` FROM information WHERE `user` = '"$user ."'");
if(!
$result){
 
$result mysql_query("INSERT INTO `information` (`user`, `pass`) VALUES('"$user ."', '"$pass ."')");
 if(!
$result){
  echo(
"Unable to add user: "$user ." to database.  Please try again.");
  exit;
 } else{
  echo(
"Thank you for registering <b>"$user ."</b>!  Please enjoy your stay here.");
 }
} else{
 echo(
"The username <b>"$user ."</b> was already found in the database.  Please go back and fix this problem.");

Anyone have any idea?
__________________
AIM: wisprmylastbreth
EMail: nightskywriter@gmail.com
Y!: synsoflife

"ai yoru ga" -- "Love the nights"
  #6   Spotlight this post!  
Unread 24-05-2004, 18:04
Guest
 
Posts: n/a
Re: MySQL/PHP Query question

Did you try the mysql_num_rows suggestion above?
  #7   Spotlight this post!  
Unread 24-05-2004, 18:09
Raven_Writer's Avatar
Raven_Writer Raven_Writer is offline
2004 Detroit & Pittsburgh Winners
AKA: Eric Hansen
FRC #0005 (RoboCards)
Team Role: Mentor
 
Join Date: Jan 2003
Rookie Year: 2002
Location: Melvindale
Posts: 1,549
Raven_Writer is just really niceRaven_Writer is just really niceRaven_Writer is just really niceRaven_Writer is just really niceRaven_Writer is just really nice
Send a message via ICQ to Raven_Writer Send a message via AIM to Raven_Writer Send a message via MSN to Raven_Writer Send a message via Yahoo to Raven_Writer
Re: MySQL/PHP Query question

Quote:
Originally Posted by SilverStar
Did you try the mysql_num_rows suggestion above?
I just tried it, and it works perfectly. Thank you very much (man I wish I would've done that before...heh).
__________________
AIM: wisprmylastbreth
EMail: nightskywriter@gmail.com
Y!: synsoflife

"ai yoru ga" -- "Love the nights"
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Question of the Week [05-02-04]: The Very Merry Month of May EddieMcD Rumor Mill 10 05-05-2004 14:02
MIM's question of the day on programming. Gene F Programming 3 24-02-2004 16:32
A question about control system options computhief263 Control System 7 04-02-2004 14:46
MySQL query question Jack Website Design/Showcase 9 02-02-2003 00:11


All times are GMT -5. The time now is 00:33.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi