 | More PHP troubles |  |
Posted: 08/20/2007 6:59 PM |
|
|
|
|
|
| Citation |
| Posts |
407 |
| Word Cnt. |
10,993 |
| BDay |
May 28 |
| Sign |
Gemini |
| Sex |
 |
|
|
|
Joined: Feb 25, 2007
Local time: 9:39 PM
Location: SE England
|

|
|
|
|
 |
Hi NR
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 |
|
|
 | Re: More PHP troubles |  |
Posted: 08/21/2007 3:49 PM |
|
|
|
|
|
| Site Admin |
| Posts |
30757 |
| Word Cnt. |
2,628,690 |
| BDay |
Jul 28 |
| Sign |
Leo |
| Sex |
 |
|
|
|
Joined: Sep 25, 2004
Local time: 5:39 PM
Location: St Pete, FL
|

|
|
|
|
 |
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
REPLACE WITH
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";
}
|
 |
|
|
 |
 |
| Back to Top |
|
|
 |
 | 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
|
|
|
|
|