-3

I keep getting this error:

PHP Error Message
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/a6941725/public_html/php/usersystem/signup.php on line 4

Here is the code:

if (mysql_query("SELECT * FROM Main WHERE username LIKE $_POST['username']"); !== "$_POST['username']") {
        $username = $_POST['username'];
}

I am normally good at php but I can't figure out why it is not working!

LarsTech
  • 80,625
  • 14
  • 153
  • 225

1 Answers1

-1

You should try it like,

$query = sprintf("SELECT * FROM Main WHERE username LIKE '%s'",
        mysql_real_escape_string($_POST['username']);
$result = mysql_query($query);// gives result
while ($row = mysql_fetch_array($result)) {
    if ($row['username'] !== $_POST['username']) { // remove quotes from post field
       $username = $_POST['username']; 
       break;// if both are not same then break
    }
}

Also mysql is removed in PHP 7 so use PDO or mysqli instead.

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • Im using php 5 and thanks it worked! – UltroGames YT Aug 29 '16 at 10:38
  • im new so I cant vote – UltroGames YT Aug 29 '16 at 10:41
  • 2
    @RohanKumar: It's not uncommon for answers with blatant SQL injections to get down-voted. Granted, the OP also had that problem. Just saying, it could be the reason. – David Aug 29 '16 at 10:41
  • Not my downvote neither but you may have gotten it because of putting in an answer for such a minimal issue and the fact about the amount of rep you have there Rohan. – Funk Forty Niner Aug 29 '16 at 11:11
  • Actually OP is using code in a wrong way and the logic was not clear for him, as he was trying to compare a mysql result with a string which is not correct. Of course he need more to learn from Google, but if he posted a question on SO then SO users must help him at least to increase his courage. – Rohan Kumar Aug 29 '16 at 11:16
  • What you just wrote in your comment now, may have been what you should have written rather than just your opening statement. Maybe that's why the person downvoted. We hardly ever know when they do. – Funk Forty Niner Aug 29 '16 at 11:20