Log in Register FAQ Memberlist Search Welcome to RCF - WHF Forum Index
alt : test.swf
Welcome to RCF - WHF
4fx3.gif 
calendar_open_closeCalendar 
More PHP troubles
Post new topic   Reply to topic View previous topic :: View next topic
Welcome to RCF - WHF Forum Index -> Talk PC Add To Bookmarks
More PHP troubles
PostPosted: 08/20/2007 6:59 PM Reply with quote
Citation
poomerio
Citation
Posts 407
Word Cnt. 10,993
BDay May 28
Sign Gemini
Sex Sex:Male
Joined: Feb 25, 2007
Local time: 9:39 PM
Location: SE England
greatbrE.gif
Hi NR Smile
I'm having trouble with this script.
It's supposed to insert the info submitted into the form, into the database, but it isnt working.

Code:
<?php
//Connects to database
include("config.php");

$username=$_POST['user'];
$password=$_POST['password'];

$sql="SELECT * FROM users";
$result="mysql_sql($sql)";
$numrows="mysql_numrows($result)";
$username="strtolower($username)";

if ($username=="")
{
}
else
{
do
{
$users[$num]="mysql_result($result,$num,'username')";

if ($username==$users[$num])
{
$userok="";
}
else
{
$userok="ok";
}
$num+=1;
}
while ($num<$numrows);

if ($userok=="ok")
{

$input="INSERT INTO users VALUES ('','$username','$password','2')";

mysql_query($input);
echo "Done";
}
else
{
echo "User already exists";
}

mysql_close($connect);
}
?>

<html>
<head>
</head>
<body>
<form action=" <?php echo $_POST["PHP_SELF"]; ?>" method=POST">
Username:<input type="text" name="user" /><br />
Password:<input type="password" name="password" />
<br />

<input type="submit" value="Submit" /><input type="reset" value="Cancel" />
</form>
</body>
</html>


Thanks,
- Poomie
Back to Top
View user's profile Find all posts by poomerio Send private message   Phoogle Map
Re: More PHP troubles
PostPosted: 08/21/2007 3:49 PM Reply with quote
Site Admin
Nightrider
Site Admin
Posts 30757
Word Cnt. 2,628,690
BDay Jul 28
Sign Leo
Sex Sex:Male
Joined: Sep 25, 2004
Local time: 5:39 PM
Location: St Pete, FL
peace.gif
This code seems to have several flaws that would prevent it from working.  The first obvious error I found was that the post method in the form tag doesn't have both begin and ending quotes:

FIND
Code:
method=POST"

REPLACE WITH
Code:
method="POST"

Now this loop doesn't make any sense either:

Code:
do
{
$users[$num]="mysql_result($result,$num,'username')";

if ($username==$users[$num])
{
$userok="";
}
else
{
$userok="ok";
}
$num+=1;
}
while ($num<$numrows);

Since the mysql_result($result,$num,'username') function is quoted, it is counted as a string.  So the $users array will be filled with identical strings of mysql_result($result,$num,'username') rather than the intended value returned from the mysql_result function.  So you would correct it like this:

FIND
Code:
$users[$num]="mysql_result($result,$num,'username')";

REPLACE WITH
Code:
$users[$num]=mysql_result($result,$num,'username');

I assume that the whole purpose of this is to determine whether the user exists in the database and if not, to add it.  There are several better ways to handle this though simply by changing the SQL query.  If the username is keyed and unique, then you can simply attempt to add the username to the database.  If the username already exists, it won't be added because you can't add duplicated keyed records...

You could also change the query like this:

FIND
Code:
$sql="SELECT * FROM users";

REPLACE WITH
Code:
$sql="SELECT * FROM users WHERE username='" . $username . "'";

If the following returned zero rows, you know that the username doesn't exist in the table and can be added:

FIND
Code:
$sql="SELECT * FROM users";
$result="mysql_sql($sql)";
$numrows="mysql_numrows($result)";
$username="strtolower($username)";

REPLACE WITH
Code:
$username=strtolower($username);
$sql="SELECT * FROM users WHERE username='" . $username . "'";
$result=mysql_query($sql);
$numrows=mysql_numrows($result);

if ($numrows == 0)
{
   $input="INSERT INTO users VALUES ('','$username','$password','2')";
   mysql_execute($input);
}
else
{
   echo "User already exists";
}

headbang
Back to Top
View all pictures posted by this userView user's profile Find all posts by Nightrider Send private message   AIM Address Yahoo Messenger Phoogle Map ICQ Number
 Post new topic  Reply to topic
Information
Welcome to RCF - WHF Forum Index -> Talk PC

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum
All times are GMT - 5 Hours

Page 1 of 1


Add To Bookmarks

 
  
  


  Google

Powered by phpBB © 2001, 2005 phpBB Group

Page generation time: 0.0736s (PHP: 63% - SQL: 37%) - SQL queries: 33 - GZIP disabled - Debug on