Help with my PHP code?

Michael

New member
I get this error message: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in ... on line 13.

Here's line 11 through 15:
//test for duplicate names
$query = "SELECT * FROM users WHERE username = $_POST["username"]";
$result = mysql_query($query);
$num = mysql_num_rows ($result);

I've double checked my database, and users database with username field exists.

I've tried to change $_POST["username"] into $_POST['username'], but it still gives me the same error message.

Could somebody help me to fix the error? Thanks :)
 
Try this:

$db = "database_name"
$query = "SELECT * FROM users WHERE username = $_POST['username']";
$result = mysql_query($query, $db);
$num = mysql_num_rows ($result);

Sorry if this doesn't work I'm a rookie at this PHP and MySQL stuff.
I think that you forgot to tell to what database to query in.
 
//test for duplicate names
$query = "SELECT * FROM users WHERE username = " . $_POST['username'];
$result = mysql_query($query);
$num = mysql_num_rows ($result);

Usually if you use associative arrays, I recommend putting the key name in single quotes and calling it outside of " " strings.
That way you won't have any problems with " " or ' ' clashing with the variable.

Example:
Not
$a = "abc$var['value1']def";
but
$a = "abc" . $var['value1'] . "def";
 
Back
Top