Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Fatal error: Call to a member function prepare() line 22

$
0
0
Not sure why I keep getting these errors all of a sudden...

Fatal error: Call to a member function prepare() on a non-object in login.php on 
line 35 Call Stack: 0.0005 675088 1. {main}() login.php:0 0.0017 678544 2.
LoginSubmit() login.php:21


Is it because of the way I'm handling my database connection?

Btw don't mind the fact that passwords are being stored as simple text because the system being built is for demo purposes only.

// database connection

$hostname = "xxx";
$username = "xxx";
$dbname = "xxx";
$password = "xxx";

try {
    $pdo = new PDO("mysql:host=$hostname;dbname=$dbname", 
    $username, $password);
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }

// login code     

if(isset($_POST['pmsubmit']))
{
  LoginSubmit('pm', 'pmname', 'pmpass');
}

if(isset($_POST['tssubmit']))
{
  LoginSubmit('ts', 'dept', 'tspass');
}

function LoginSubmit($pm_or_ts, $the_name, $the_pass)
{
  $the_name = $the_name;
  $posted_name = $_POST[$posted_name];
  $posted_pass = $_POST[$posted_pass];
  // check if password matches the one in the table
  $query = $pdo->prepare("SELECT * FROM db_pass WHERE pass = :pass"); // line 21
  $query->execute(array(":pass" => $posted_pass));
  // if there is a match then we log in the user
  if ($query->rowCount() > 0)
  {
    // session stuff
    $_SESSION[$the_name] = $posted_name;
    // refresh page
    header( 'Location: ' . $pm_or_ts . '/index.php' ) ;
  } 
  // if there is no match then we present the user with an error
  else
  {
    echo "error";
    exit;
  }
}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>