You're not sanitizing you're database inputs either. You're wide open to some idiot wiping your entire database. That might fix the error if you've got any weird input in your form.
MrCoder
05-01-2008, 05:35 AM
Use mysql_real_escape_string() and typecasting on your $_POST values.
Design-is-BS
05-01-2008, 08:21 AM
SyCo,
This is line 35, where the supposed error is:
while ($row = mysql_fetch_array($DBpass)) { $DBpass2 = "$row[1]"; }
Design-is-BS
05-01-2008, 08:27 AM
MyCoder,
I'm not sure what you mean by that. Can you please explain?
Design-is-BS
05-01-2008, 08:29 AM
You're not sanitizing you're database inputs either. You're wide open to some idiot wiping your entire database. That might fix the error if you've got any weird input in your form.
Woah, thats scary.
I mean right now this Database has nothing of significance. It's just a testing Database so I can get the code to work.
SyCo
05-01-2008, 09:38 AM
Yea it is, but it's easy to fix too. Even though it's said often lots of people still don't get sanitize their inputs. Look into MrCoder's suggestions.
Check this out too :)
http://xkcd.com/327/
Did the preceding query look OK? That's likely where the problem is. Try hard coding the value for testing. Also run it in a terminal or Putty (or whatever command line program) to see if it runs OK.
You'd get a can't connect error if the link was bad, so that's probably OK. If you're only connecting to one database on the page you don't have to add the link resource every time. Although a lot pf people do, not sure why. Is there an advantage to it? Here's the manual page it doesn't mention any advantage but like I say, I see it a lot.
http://us3.php.net/mysql_query
link_identifier
The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If by chance no connection is found or established, an E_WARNING level error is generated.
Design-is-BS
05-09-2008, 09:13 AM
OK, I've been trying to make this work but no matter what I do it gives an error of "syntax error, unexpected $end".
if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['email'])) {
// Connect
$hostname = "";
$username= "";
$password= "";
$dbid="";
$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbid) or die ("Unable to connect to MySQL");
if(!is_resource($link)) {
echo "Failed to connect to the server\n";
// ... log the error properly
} else {
// Reverse magic_quotes_gpc/magic_quotes_sybase effects on those vars if ON.
That error usually means you've not closed a curly bracket.
Actually, you've not closed 2 :) Close the first and second if()s
Design-is-BS
05-09-2008, 09:46 AM
Well don't I feel embarassed...
THAT portion is fixed. But now I'm being told (once submitted form) that:
"mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource"
For this:
while ($row = mysql_fetch_assoc($fetch)) {
echo "<br><br>" . $row['username'];
}
Seems fine to me, am I wrong? Those ARE the inputs I have, and it matches what's on the DB.
EDIT:
--
There I go again, I changed the code to:
// Make a safe query
$query = sprintf("SELECT * FROM products (username, password, email) VALUES (%s, %s, %s)",
mysql_real_escape_string($username, $link),
mysql_real_escape_string($password, $link),
mysql_real_escape_string($email, $link));
$fetch = mysql_query($query, $link);
echo $query;
Design-is-BS
05-09-2008, 10:03 AM
The MySQL error I get is:
"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 '(username, password, email) VALUES (usertest, passtest, emailtest)' at line 1"
Design-is-BS
05-09-2008, 10:05 AM
Sorry for the constant replies...
I think I might have found soemthing:
My DB Table is "users" not "products". I changed it and got an error of:
"Warning: Wrong parameter count for mysql_fetch_assoc()"
Design-is-BS
05-09-2008, 11:31 AM
OK, here is what I'm trying to do...
ANYONE FEEL FREE TO ASSIST! PLEASE!!!!
1) The user is sent an email containing a Randomly Generated Password and a User-name made up of the first-part of their email.
2) In this email is a link to a webpage, where the user enters in three bits of information:
2a) Username (provided)
2b) Password (provided)
2c) Email
3) upon clicking "Submit" the information is compared to what is on the DB (Database).
3a) If there is a match, the app carries on.
3b) If there isn't a match the user is prompted to try again.
4) Upon a successful match, the user is then directed to a page where they can customize their login (User-Name and Password) and they can enter in other information (Address, First Name, etc.)
Ok...Now...I haven't gotten that far yet; I'm stuck on #3. I am not able to compare what is on my DB to what the user has submitted. F0r some reason what I was doing earlier wasn't safe, so I'm forced to try a new way of doing things.
This is my Code, currently:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Testing - Investor Login</title>
if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['email'])) {
// Connect
$hostname = "myhost";
$username= "myuser";
$password= "mypass";
$dbid="myid";
$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbid) or die ("Unable to connect to MySQL");
if(!is_resource($link)) {
echo "Failed to connect to the server\n";
// ... log the error properly
} else {
// Reverse magic_quotes_gpc/magic_quotes_sybase effects on those vars if ON.
while ($row = mysql_fetch_assoc($fetch)) {
echo "<br><br>" . $row['username'];
}
I am still getting the same error:
MySQL: 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 '('username', 'password', 'email') VALUES ('usertest', 'passtest', 'emailtest')' at line 1
PHP: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource
The PHP Error is in response to:
while ($row = mysql_fetch_assoc($fetch)) {
SyCo
05-09-2008, 11:56 AM
If this is a syntax for select
SELECT * FROM users ('username', 'password', 'email') VALUES ('%s', '%s', '%s'
Its a new one on me. It's more like the syntax for an insert.
The syntax I've always used for a select is
query="SELECT * FROM users WHERE username='%s' AND password='%s' AND email='%s'"
I'm a little busy today so not giving this my full attention. Sorry about missing that :)
Design-is-BS
05-09-2008, 12:25 PM
Don't worry about it SyCo, I would have never even thought of that.
I tried this method like this:
$query="SELECT * FROM users WHERE username='%s' AND password='%s' AND email='%s'",
mysql_real_escape_string($username, $link),
mysql_real_escape_string($password, $link),
mysql_real_escape_string($email, $link));
And got an error of:
PHP: Parse error: syntax error, unexpected ',' in...on line 85
Line 85 is:
$query="SELECT * FROM users WHERE username='%s' AND password='%s' AND email='%s'"
So I tried:
//Make a safe query
$query="SELECT * FROM users WHERE username='$username' AND password='$password' AND email='$email'";
// mysql_real_escape_string($username, $link),
// mysql_real_escape_string($password, $link),
// mysql_real_escape_string($email, $link));
$fetch = mysql_query($query, $link);
I'll try the next step: comparing to what's on the DB. And because I have to learn I will try myself.
But trust my SyCo, you'll be hearing from me if I get stuck again, you always give me a hand!
SyCo
05-09-2008, 12:58 PM
You're welcome. You're keen to learn, provide lots of feedback on suggestions and let me know what you're doing, multile post are OK too, the more info the better. You even gave me the line the error war given for. It's that kind of thing that makes helping you a pleaseent experience. I've only ever once given up on someone because they didn't get that if it's hard work for me to help them, why would I help?
You still need to sanitize the inputs. Maybe this will do it for you.