For practice, I'm trying to write a simple login code for a table that consists of three fields: id, teamnname and score.
There is a db_connect.php file that files that require it include at the top. I know this is working correctly (as other functions in the functions.php file appear to be working). This file defines $mysqli, which seems to be working fine elsewhere.
This file looks like:
<?php
$mysqli = mysql_connect('host', 'database', 'password');
ob_start();
?>
However, the only function that isn't working is my process_login function. The login.php file will not output anything other than "Connection Successful" from the database connection working properly. It should either indicate "invalid request" or redirect the page.
Here is the login.php script using this function...
<?php
include_once 'db_connect.php';
include_once 'functions.php';
custom_session();
if (isset($_POST['teamname'])) {
$teamname = $_POST['teamname'];
if (process_login($teamname, $mysqli) == true) {
// redirect to the right place
header('Location: ../index.php?start_game');
} else {
// redirect to an error page
header('Location: ../index.php?error1')
}
} else {
// the correct POST variables were not sent to this page.
echo 'Invalid Request';
}
And here is the problematic function in functions.php:
function process_login($teamname, $mysqli) {
if ($stmt = $mysqli->prepare("SELECT id, teamname, score FROM users WHERE teamname = ? LIMIT 1")) {
$stmt->bind_param('s', $teamname);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id, $teamname, $score);
$stmt->fetch();
if ($stmt->num_rows == 1) {
$_SESSION['id'] = $id;
$_SESSION['teamname'] = $teamname;
$_SESSION['score'] = $score;
return true;
}
}
return false;
}
I've played with this and the prepared statement at the top seems to be the culprit. I've run that SELECT syntax through the database directly so I know that it can't be wrong. I'm stumped having tried many things and would appreciate some seasoned help.