I need help on the code below which I copied from basic PHP tutorial and trying to edit it. The agent will fill up the form with user id and password given by the administrator with a vehicle plate field for the agent to key in and store into the database, it'll then redirect the agent to a link. All codes are working properly including redirect user to different page except for the part on **// Add vehicle plate ----------------------------------------//**
section.
<?php
session_start();
// Change this to your connection info.
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = 'password123';
$DATABASE_NAME = 'databasetest123';
// Try and connect using the info above.
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if (mysqli_connect_errno()) {
// If there is an error with the connection, stop the script and display the error.
exit('Failed to connect to MySQL: ' . mysqli_connect_error());
}
// Now we check if the data from the login form was submitted, isset() will check if the data exists.
if (!isset($_POST['agentname'], $_POST['vehicleplate'], $_POST['password'])) {
// Could not get the data that should have been sent.
exit('All fields are required!');
}
// Prepare our SQL, preparing the SQL statement will prevent SQL injection.
if ($stmt = $con->prepare('SELECT agentcode, password FROM agentlist WHERE agentname = ?')) {
// Bind parameters (s = string, i = int, b = blob, etc), in our case the agentname is a string so we use "s"
$stmt->bind_param('s', $_POST['agentname']);
$stmt->execute();
// Store the result so we can check if the account exists in the database.
$stmt->store_result();
if ($stmt->num_rows > 0) {
$stmt->bind_result($agentcode, $password);
$stmt->fetch();
// Account exists, now we verify the password.
// Note: remember to use password_hash in your registration file to store the hashed passwords.
if (password_verify($_POST['password'], $password)) {
// Verification success! User has loggedin!
// Create sessions so we know the user is logged in, they basically act like cookies but remember the data on the server.
session_regenerate_id();
$_SESSION['loggedin'] = true;
$_SESSION['agentname'] = $_POST['agentname'];
$_SESSION['agentcode'] = $agentcode;
// Add vehice plate ----------------------------------------//
//$stmt = $mysqli->prepare("SELECT EXISTS(SELECT 1 FROM agentlist WHERE vehicleplate = ?)");
$stmt = $con->prepare("SELECT count(3) FROM agentlist WHERE vehicleplate = '?'");
$stmt->bind_param('s', $_POST['vehicleplate']);
$stmt->execute();
$stmt->bind_result($exists);
$stmt->fetch();
if ($exists) {
echo 'Vehicle plate already exist! Please enter another.';
//$_SESSION['error'] = "Vehicle plate already exist! Please enter another.";
} else {
$stmt = $con->prepare('INSERT INTO agentlist (vehicleplate, agentname, password, email) VALUES (?, ?, ?, ?)');
$stmt->bind_param('sssss', $_POST['vehicleplate'], $_POST['agentname'], $password, $_POST['email'], $uniqid);
$stmt->execute();
}
//if($exists) {
// $_SESSION['error'] = "Vehicle plate already exist! Please enter another.";
//} else
// if ($stmt = $con->prepare('INSERT INTO agentlist (vehicleplate, agentname, password, email) VALUES (?, ?, ?, ?)')) {
// $stmt->bind_param('sssss', $_POST['vehicleplate'], $_POST['agentname'], $password, $_POST['email'], $uniqid);
// $stmt->execute();
// }
error_reporting(E_ALL);
ini_set('display_errors', '1');
//----------------------------------------------------------//
header("Location: agent_axa_registration.php"); /*https://w12.financial-link.com.my/PremiumLink3/login?compcode=07&pagePurpose=QUO&agtcode=39962*/
} else {
// Incorrect password
$_SESSION['error'] = "Incorrect password!";
header("Location: agent_login.php"); //send user back to the login page.
}
} else {
// Incorrect agentname
$_SESSION['error'] = "Incorrect agentname!";
header("Location: agent_login.php"); //send user back to the login page.
}
//echo 'Welcome ' . $_SESSION['agentname'] . '!';
//} else {
// Incorrect password
//echo 'Incorrect password!';
//}
//} else {
// Incorrect agent name
//echo 'Incorrect agent name!';
//}
$stmt->close();
}
In my database, there's 5 column - agentcode, agentname, vehicleplate, email, password. But in the agent form, we only need the agent to key in agentcode, agentname, vehicleplate, and password. Email is not necessary for the agent to key in as it's only for admin reference. Is it possible to only have 4 fields in the form instead of all 5 fields? The 'sssss' is it referring to 5 fields?