Banging my head against a wall, I’m NOT a php coder but I do know the code I’m posting is deprecated now, should be using Prepared Statements, but I cannot get this work to save my life. It is not adding the data if no record is found. Can someone please point me in the right direction to what I’m missing or a better way to write this code? Very much appreciated!
<?php
header('Access-Control-Allow-Origin: *');
if ($_SERVER['REQUEST_METHOD'] != 'POST')
{
echo "POST request expected";
return;
}
error_reporting(E_ALL && E_WARNING && E_NOTICE);
ini_set('display_errors', 0);
ini_set('log_errors', 1);
require_once 'includes/common.inc.php';
$username = $_POST['USER_NAME'];
$email = $_POST['USER_EMAIL'];
$quiz = $_POST['qt']; //qt
$DL = $_POST['DLNUMBER'];
$State = $_POST['STATE'];
$Points =$_POST['tp'];
$PPercent = $_POST['psp'];
$Earned = $_POST['sp'];
$Time = $_POST['fut'];
$Date = $_POST['ts'];
$Status = $_POST['rs'];
$result = substr($Status, 1, 6);
$link = mysqli_connect("XXXXXXX", "XXXXXX", "XXXXXX", "Database");
$query = "SELECT * FROM students WHERE QuizTitle='$quiz' and DriverLicense='$DL'";
$result = $link->query($query);
if ($result) {
if (mysqli_num_rows($result) > 0) {
//die;
} else {
$sql ="INSERT INTO students (UserName,UserEmail,QuizTitle,DriverLicense,State,Points,PPercent,Earned,TimeTaken,Status)
VALUES('$username', '$email', '$quiz', '$DL' ,'$State', '$Points','$PPercent','$Earned','$Time','$result')";
$link->query($sql);
}
} else {
echo 'Error: ' . mysqli_error();
}
// close connection
mysqli_close($link);