Jump to content

Recommended Posts

I have made this code for a login. It just shows a blank page showing nothing, so the code is not wrong, but I can;t see what is... I would like it if you could identify the problem for my. Thanks, Joe.

 

<?php
$username = $_POST['username'];
$password = $_POST['password'];

$con = mysql_connect("localhost","hhh","hhh");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("Blah", $con);

$result = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'");

while($row = mysql_fetch_array($result))
  {
$d_username = $row['username'];
$d_password = $row['password'];

if($d_username == $username && $d_password == $password) {
echo 'Win ';
}
else {
echo 'Fail :l';
}
  }
?>

Link to comment
https://forums.phpfreaks.com/topic/202998-php-help-my-sql/
Share on other sites

Basically, you are selecting the row from the database which contains the POSTed username and password, and then you don't trust MySQL and are checking it again ::) Why my dear boy?

 

<?php

$username = mysql_real_escape_string($_POST['username']); // read up on mysql_real_escape_string().
$password = mysql_real_escape_string($_POST['password']); // read up on mysql_real_escape_string().

$con = mysql_connect("localhost", "hhh", "hhh") or die('Could not connect: ' . mysql_error());
mysql_select_db("Blah", $con);

$result = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'") or die('Query failed: ' . mysql_error());
if(mysql_num_rows($result) > 0) {
    echo 'Win ';
} else {
    echo 'Fail :l';
}

?>

Link to comment
https://forums.phpfreaks.com/topic/202998-php-help-my-sql/#findComment-1063757
Share on other sites

Hmmm.... try this:

 

<?php
$username = $_POST['username'];
$password = $_POST['password'];

$con = mysql_connect("localhost","hhh","hhh");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("Blah", $con);

$query = "SELECT * FROM users WHERE username='". mysql_real_escape_string($username). "' AND password='". mysql_real_escape_string($password). "';";

if (!$result = mysql_query($query))
{
    die(mysql_error()); // it failed for whatever reason.
}
else
{
    if (mysql_num_rows($result) > 0)
    {
        echo 'Win ';
    }
    else 
    {
        echo 'Fail :l';
    }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/202998-php-help-my-sql/#findComment-1063761
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.