Code Issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post by TheHappyPeanut »

Celauran wrote:Without seeing what line 5 is (and the surrounding lines, really), I can't help you much.
Whoops

Code: Select all

<?php

var_dump($_POST)

if (!empty($_POST))
{
    if (isset($_POST['username']) && isset($_POST['password']))
    {
    $connect = mysql_connect("localhost","root","") or die("Could not connect");
    mysql_select_db("tracker") or die("Could not find database");
    }
    else
        die("Incorrect information");    
}
   
?>
Line 5 being

Code: Select all

if (!empty($_POST))
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post by Celauran »

You're missing the semicolon after var_dump
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post by TheHappyPeanut »

Celauran wrote:You're missing the semicolon after var_dump
Well, I'm dumb. :P

[text]array(2) { ["username"]=> string(0) "" ["password"]=> string(8) "password" }[/text]
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post by Celauran »

So $_POST['username'] is empty, which is why the if fails.
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post by TheHappyPeanut »

So what would that point to? An issue with my HTML markup or an issue with my database?
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post by TheHappyPeanut »

Actually, that's a flawed output because I didn't put anything in the log-in box (if I should have). Here's one where I filled in both fields.

Code: Select all

array(2) { ["username"]=> string(4) "John" ["password"]=> string(8) "password" }
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post by Celauran »

That one should work fine. You won't see any output to screen because you haven't done anything that produces any. You can echo some dummy text after selecting your database to confirm it's working if you like.
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post by TheHappyPeanut »

Like this?

Code: Select all

<?php


if (!empty($_POST))
{
    if (isset($_POST['username']) && isset($_POST['password']))
    {
    $connect = mysql_connect("localhost","root","") or die("Could not connect");
    mysql_select_db("tracker") or die("Could not find database");
    echo'testing';
    }
    else
        die("Incorrect information");
}
   
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post by Celauran »

Sure. Using your previous form submission, you should see testing echoed.
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post by TheHappyPeanut »

Celauran wrote:Sure. Using your previous form submission, you should see testing echoed.
Yes, it's echoing the text when I attempt to log in.
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post by TheHappyPeanut »

I implemented mysqli a few minutes ago and I've not received any errors, so I'm assuming I've done it correctly. With that being said, the issue I'm having still exists. :P

Code: Select all

<?php

if (!empty($_POST))
{
    if (isset($_POST['username']) && isset($_POST['password']))
    {
    $mysqli = new mysqli();
    $mysqli = new mysqli('localhost', 'root', '', 'tracker');
    }
    else
        die();
}
   
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post by Celauran »

Code: Select all

<?php

if (!empty($_POST))
{
    if (isset($_POST['username']) && isset($_POST['password']))
    {
        $mysqli = new mysqli('localhost', 'root', '', 'tracker');
    }
    else
        die();
}
   
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post by Celauran »

TheHappyPeanut wrote:With that being said, the issue I'm having still exists.
What issue is that? As per above, everything seems to be working fine given the little code you're written.
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post by TheHappyPeanut »

I can type anything I want into the form and it never gives me an error. Perhaps I've left a bit of code out that I require?

Code: Select all

if (isset($_POST['username']) && isset($_POST['password']))
    {
    $mysqli = new mysqli();
    $mysqli = new mysqli('localhost', 'root', '', 'tracker');
    }
    else
        die('Unable to proceed');
If I am correct, then this line of code basically says "If you don't provide me with a correct username or password, I'm closing the connection and telling you "Unable to proceed".

Code: Select all

else
        die('Unable to proceed');
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post by Celauran »

It's not performing any authentication. That block, in English, would ready "If a (any) username and password have been provided, open a connection to the database. Otherwise, don't and inform the user you couldn't." Or something to that effect.
Post Reply