2

This is all really new to me and I only know the very basics. I'm creating a frontend login for a webpage (obviously security isn't a huge deal or I wouldn't be doing it). I keep getting in issue with my "where" clause, stating that the "user" does not exist. Database is setup like this: dbname=connectivity table=users users has id, user, and pass. Anyone want to give me some pointers? Thanks in advance.

<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'connectivity');
define('DB_USER','root');
define('DB_PASSWORD','');
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Ya done goofed:  " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Ya done goofed: " . mysql_error());

function SignIn()
{
session_start();   
if(!empty($_POST['user']))  


{
     $query = mysql_query("SELECT *  FROM users where user = `$_POST[user]`  AND pass = '$_POST[pass]'") or die(mysql_error());
    $row = mysql_fetch_array($query) or die(mysql_error());
    if(!empty($row['user']) AND !empty($row['pass']))
    {
        $_SESSION['user'] = $row['pass'];
        echo "SUCCESSFULLY LOGIN TO USER PROFILE PAGE...";

    }
else
{
    echo "SORRY... YOU ENTERD WRONG ID AND PASSWORD... PLEASE RETRY...";
}
}
}
if(isset($_POST['submit']))
{
    SignIn();
}

?>
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Joe
  • 21
  • 3

6 Answers6

4

Please stop using mysql_*. use mysqli_* or PDO. Have a look to the code:-

<?php
// Force PHP to show errors
error_reporting(E_ALL); // Get all type of errors if any occur in code
ini_set('display_errors',1); // Display those errors

session_start(); // start session

define('DB_HOST', 'localhost');
define('DB_NAME', 'connectivity');
define('DB_USER','root');
define('DB_PASSWORD','');

$con = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME) or die("connection not established"); Or use  $con = mysqli_connect('localhost','root','','connectivity') or die("connection not established");

if(isset($_POST['submit'])){
    SignIn();
}

function SignIn(){
   if(!empty($_POST['user'])) {
        $username = mysqli_real_escape_string($con , $_POST['user']); // prevent form SQL injection
        $password = mysqli_real_escape_string($con , $_POST['pass']); // prevent form SQL injection
        $query = mysqli_query($con,"SELECT *  FROM users where user = '".$username."'  AND pass = '".$password."'") or die(mysqli_error($con));
        if(mysqli_num_rows($query) > 0){ // check count of resultset
            $_SESSION['user'] = $_POST['pass'];
            echo "SUCCESSFULLY LOGIN TO USER PROFILE PAGE...";
        }else{
            echo "SORRY... YOU ENTERD WRONG ID AND PASSWORD... PLEASE RETRY...";
        }
    }
}
?>
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • I just got back from lunch and will try this out, Thanks for taking the time to look over my stuff and provide your feedback. I know this probably comes easy for a lot of you, but I'm just starting out. Thanks again! – Joe Jul 01 '15 at 18:14
  • On yours and SuperDJs code I keep getting "udefined variable" for $con. It;s already defined in both situations, not sure why I'm getting this. – Joe Jul 01 '15 at 19:41
  • That helped! Now I just get this error: "Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES) in C:\xampp\htdocs\OctopiPage\connectivity.php on line 13 connection not established" – Joe Jul 01 '15 at 19:51
  • now i get undefined variable for: $username = mysqli_real_escape_string($con , $_POST['user']); // prevent form SQL injection and for:$query = mysqli_query($con,"SELECT * FROM usersOcto where usersOcto = '".$username."' AND pass = '".$password."'") or die(mysqli_error($con)); – Joe Jul 02 '15 at 13:23
  • It also gives this error: mysqli_query() expects parameter 1 to be mysqli – Joe Jul 02 '15 at 13:26
  • I'm sorry but I do not know how to run that to output anything (print_r($_POST). I really appreciate your time in trying to help me out. I am currently reading through some PHP tutorials to see if I can't figure this out. Thanks again! – Joe Jul 02 '15 at 15:41
  • Just place this code after `$conn` and check what value it is giving. – Alive to die - Anant Jul 02 '15 at 15:54
  • Notice: Undefined variable: con in C:\xampp\htdocs\OctopiPage\connectivity.php on line 23 Array ( [user] => test [pass] => s [submit] => Log-In ) Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\OctopiPage\connectivity.php on line 23 Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\OctopiPage\connectivity.php on line 24 SORRY... YOU ENTERD WRONG ID AND PASSWORD... PLEASE RETRY... – Joe Jul 02 '15 at 17:59
  • check my edited answer and use second code for connection creation. remove first one and also remove print_r($_POST), and check. – Alive to die - Anant Jul 02 '15 at 18:08
2

There are some issues here:

SELECT *  FROM users where user = `$_POST[user]`  AND pass = '$_POST[pass]'

The quote styles are all over the place. Try this:

SELECT * FROM `users` WHERE `user` = '$_POST[user]' AND `pass` = '$_POST[pass]'

Also, you should pre-process for SQL injection if you're not already.

mike.k
  • 3,277
  • 1
  • 12
  • 18
  • To clarify: Use backticks (``) for quoting the names of tables and fields in the database. Use either single or double quotes (`'` and `"` respectively) for quoting your values. And as @mike.k rightly says - remember to escape your values to protect against SQL injection - see `mysql_real_escape_string()`. – Alex Jul 01 '15 at 17:47
  • Thanks so much for your advise. I know I have a lot to learn, and I appreciate you taking the time to comment. – Joe Jul 01 '15 at 18:12
0

This is the correct formatted SQL.

$query = mysql_query("SELECT * FROM `users` WHERE `user` = `'".$_POST["user"]."'` AND pass = '".$_POST["pass"]."'") or die(mysql_error());

One thing to note is that you MUST escape and validate all global variables. For more information I strongly recommend you to read this SO post: How can I prevent SQL injection in PHP?

Community
  • 1
  • 1
Stanimir Dimitrov
  • 1,872
  • 2
  • 20
  • 25
0

Just changed your code like follows:

SELECT * FROM users where user ='$_POST[user]'AND pass = '$_POST[pass]'

That line need to rewrite like follows:

SELECT * FROM users WHERE user = '".$_POST[user]."' AND pass = '".$_POST[pass]."'

I believe that should work in every server without any kind of trouble.

Evan Carslake
  • 2,267
  • 15
  • 38
  • 56
tisuchi
  • 129
  • 2
  • 11
0

There are multiple things wrong with your code check it down below:

<?php
session_start(); // This needs to be on top of every page

define('DB_HOST', 'localhost');
define('DB_NAME', 'connectivity');
define('DB_USER','root');
define('DB_PASSWORD','');

// Use mysqli_* as mysql_* is depracted and will be removed
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die("connection not established");

// Add a bit of security
$user = mysqli_real_escape_string($con, $_POST['user']);
$pass = mysqli_real_escape_string($con, $_POST['pass']);

function SignIn($user, $pass) {
    // Add backticks ` around column and table names to prevent mysql reserved word error
    $query = mysqli_query($con, "SELECT * FROM `users` WHERE `user` = '$user'  AND `pass` = '$pass'");
    // No need to fetch the data you already have
    // Check if the query returns atleast 1 row (result)
    if( mysqli_num_rows($query) >= 1 ) {
        $_SESSION['user'] = $pass;
        echo "SUCCESSFULLY LOGIN TO USER PROFILE PAGE...";
    } else {
        echo "SORRY... YOU ENTERD WRONG ID AND PASSWORD... PLEASE RETRY...";
    }
}

if(isset($_POST['submit']) && !empty($user) && !empty($pass) ) {
    SignIn($user, $pass);
} else {
    echo "SORRY... THERE ARE EMPTY FIELDS... PLEASE RETRY...";
}

?>
SuperDJ
  • 7,488
  • 11
  • 40
  • 74
-2

You are missing quotations

Corrected code:

$query = mysql_query("SELECT * FROM `users` WHERE `user` = `'".$_POST["user"]."'` AND pass = '".$_POST["pass"]."'") or die(mysql_error())
petebolduc
  • 1,233
  • 1
  • 13
  • 20
  • 1
    PHP will give errors when using single quotes on array keys inside of a string – mike.k Jul 01 '15 at 16:51
  • I do stand corrected... been a while not using PDO plus I always assign $_POST[''] to a variable prior to the query – petebolduc Jul 01 '15 at 16:56
  • @petebolduc this isn't PDO it's depracted `mysql_*` – SuperDJ Jul 01 '15 at 17:01
  • I know it is not PDO... my statement includes "not" indicating that it has been a long long time since I personally have used `mysql_*` sorry for the confusion – petebolduc Jul 01 '15 at 17:06