I have this SQL statement which pulls ONE value (the OrderID) from the orders table.
"SELECT DISTINCT (MAX(OrderID)) FROM orders WHERE Username = '[email protected]';
I run that via PHPMyAdmin and it returns one Order ID for that user which is EXACTLY what I want. However, I am trying to run this in PHP...
"SELECT DISTINCT (MAX(OrderID)) FROM orders WHERE Username = '". $_SESSION['Username'] ."'";
I want that to pull the Order ID so I can then set $OrderID = (the result from the select statement), then run an update statement as below.
$sql = "UPDATE orders SET Status = 'Failed' WHERE Status = 'Success' and OrderID = ". $OrderID ."";
If I hard code $OrderID = 90; it works perfectly but I need the OrderID to pull through via the session of who is logged in.
Complete code:
<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'order-failed') !== false) {
$servername = "NULL";
$username = "NULL";
$password = "NULL";
$dbname = "NULL";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check Connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query = "SELECT DISTINCT (MAX(OrderID)) FROM orders WHERE Username = '". $_SESSION['Username'] ."'";
echo $query . "<br />";
$OrderID = 93;
echo $OrderID;
echo "<br />";
$sql = "UPDATE orders SET Status = 'Failed' WHERE Status = 'Success' and OrderID = ". $OrderID ."";
echo $sql;
if ($conn->query($sql) === TRUE) {
echo "<br />Record updated successfully";
}
else {
echo "<br />Error updating record: " . $conn->error;
}
$conn->close();
}
else {
session_destroy();
}
?>
session_start()at the top of your file. Are you sure it isn't the issue?