0

code:

<?php
session_start();
$_SESSION['msg'] = "";
$con = mysql_connect('localhost','me','omglol');
mysql_select_db('test',$con);
$q = mysql_query(sprintf("select * from UserTable where (nick=\"%s\") AND (pass=SHA1(\"%s\"))",$_POST['nick'],$_POST['pass']),$con) or die(mysql_error());

This looks right to me. And yes I know 'test' exists. And contains UserTable.

First, Thanks rid for adding php4 to the tags I forgot :(

As per Laser_wizard's recommendations I did the following: (Entire Code):

<?php
session_start();
$_SESSION['msg'] = "";
$con = mysql_connect('localhost','me','omglol');
if(!$con)
{
    die("The connection to mysql server is not being made.");
}
$db = 'test';
$selected = mysql_select_db($db,$con);
if(!$selected)
{
    die(sprintf("Cannot use database %s.",$db));
}
//$q = mysql_query(sprintf("select * from UserTable where (nick=\"%s\") AND (pass=SHA1(\"%s\"))",$_POST['nick'],$_POST['pass']),$con) or die(mysql_error());
$q = mysql_query("select * from UserTable",$con) or die("The query statement still isn't working");
$row = mysql_fetch_assoc($q);
$dest=0;
if(mysql_num_rows($q)==0)
{
    //$testn = mysql_query(sprintf("select * from UserTable where nick=(\"%s\")",$_POST['nick']),$con);
        $testn = mysql_query("select * from Category",$con) or die("The 2nd query statement still isn't working");
        if(mysql_num_rows($testn)==0)
        {
               $_SESSION['msg'] = "Nick ".$_POST['nick']." was not found. Check spelling or <a href=\\\"register.php\\\">register</a>";
        }
        else
        {
                $_SESSION['msg'] = "Password incorrect";
        }
        if(isset($_SESSION['attempts']))
        {
                $_SESSION['attempts'] = $_SESSION['attempts'] + 1;
        }
        else
        {
                $_SESSION['attempts'] = 1;
        }
    mysql_free_result($q);
    mysql_free_result($testn);
        mysql_close($con);
    $dest = 'Location:http://cs4.sunyocc.edu/~me/onestopshop/login.php';
}
else
{
        $_SESSION['nick'] = $_POST['nick'];
    $_SESSION['email'] = $row['email'];
    mysql_free_result($q);
    mysql_close($con);
    $dest = 'Location:http://cs4.sunyocc.edu/~me/onestopshop/index.php';
}
header($dest);
exit();
?>

Same error as above. So $con is set and $selected reads true, so I'm confused what to check next. I'm guessing mysql_select_db($db,$con); Nor is $testn is still not working but still reading true? I'm confused what to do next.

jason dancks
  • 1,152
  • 3
  • 9
  • 29
  • 3
    some definite SQL injection vulnerabilities there... – doublesharp Oct 19 '12 at 04:14
  • 1
    might want to check the value of `$con` before attempting to select a database or execute queries. Same goes for the return value of `mysql_select_db()`. You know the MySQL extension is now deprecated, right? – Phil Oct 19 '12 at 04:16
  • 4
    The 1st question would be php4? – itachi Oct 19 '12 at 04:17
  • @Phill: he is in php4 so that's the only extension he can use i suppose. – itachi Oct 19 '12 at 04:19
  • I plan on going back and adding 'escaping' my input when I find out how to do that exactly. I was hoping to get this working first. And yes its php4 :( I should've been more informative sorry – jason dancks Oct 19 '12 at 04:56

2 Answers2

1

Throw in some die statements to test the connection and make sure it's setting up. Other than than that I'd say to comment out your query line to see if that's causing a problem.

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}
davepmiller
  • 2,620
  • 3
  • 33
  • 61
0

If you have PHP >= 5.1.0 forget these instructions and use PDO:

<?php
    $conn = new PDO('mysql:host=localhost;dbname=test', 'user', 'pass');
    foreach($conn->query('SELECT * from TEST') as $row) {
        print_r($row);
    }
?>
Laleft
  • 75
  • 1