So in the following code that is supposed to check availability when a user books a restaurant table I don't know how to fetch the result:
$sql = "SELECT MIN(total_availability) FROM restaurant WHERE date='$date'";
$result = mysqli_query($link, $sql);
$sql_value = "";
/*
assign some value to $sql_value with the minimum value of the $sql statement
*/
if($sql_value > 0){
/*
insert row into restaurant table
*/
}
What should I do?Thank you.
Optional, not so relevant, this is the original source code after some comments I received, I don't use PDO.
<?php
include "sessione.php";
include "connessione.php";
$name = mysqli_real_escape_string($connection, $_POST["name"]);
$surname = mysqli_real_escape_string($connection, $_POST["surname"]);
$people = $_POST["pax"];
$date = $_POST["date"];
$time = $_POST["time"];
$preference = $_POST["preference"];
$email = mysqli_real_escape_string($connection, $_POST["email"]);
if(preg_match("/^[a-zA-Z\s]*$/", $name) && preg_match("/^[a-zA-Z\s]*$/", $surname) && preg_match("/^[a-zA-Z0-9\.\-\_@]*$/", $email) && !empty($name) && !empty($surname) && !empty($people) && !empty($date) && !empty($time) && !empty($preference) && !empty($email)){
$sql = "SELECT * FROM il_leone_ristorante WHERE data='$date' AND disponibilita_totale <= 1";
$result = mysqli_query($connection, $sql);
$count = mysqli_num_rows($result);
if($count > 0){
mysqli_free_result($result);
mysqli_close($connection);
echo "
<script type=\"text/javascript\">
window.alert('Disponibilità terminata.');
window.location.replace('ristorante.php');
</script>
";
} else {
$username = $_SESSION["username"];
$sql = "SELECT MIN(disponibilita_totale) FROM il_leone_ristorante WHERE data='$date'";
$result = mysqli_query($connection, $sql);
$row = mysqli_fetch_all($result, MYSQLI_ASSOC);
foreach ($row as $r){
$min_value = $r["MIN(disponibilita_totale)"];
}
$availability = $min_value;
if($availability > 0){
$availability -= 1;
} else {
$availability = NULL;
}
if($availability > 0 && $availability != NULL){
$sql = "INSERT INTO il_leone_ristorante (nome, cognome, persone, data, ora, preferenza, utente, disponibilita_totale) VALUES ('$name', '$surname', '$people', '$date', '$time', '$preference', '$username', '$availability')";
mysqli_query($connection, $sql);
$date = explode("-", $date); // for italian date format, data means date (day) in italian
$data = "".$date[2]."/".$date[1]."/".$date[0]."";
$subject = "Tavolo prenotato - conferma digitale";
$to = "$email";
$message = "
<html>
<head>
<title>Conferma</title>
</head>
<body>
<h1 style=\"color: blue; font-weight:bold;\">Conferma prenotazione del tavolo</h1><br />
<p>Nome: $name</p>
<p>Cognome: $surname</p>
<p>Persone: $people</p>
<p>Data: $data</p>
<p>Ora: $time</p>
<p>E-mail: $email</p>
</body>
</html>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
mail($to, $subject, $message, $headers);
mysqli_free_result($result);
mysqli_close($connection);
echo "
<script type=\"text/javascript\">
window.alert('Tavolo prenotato 1, controlla la tua mail per la conferma digitale.');
window.location.replace('index.php');
</script>
";
} else if($availability == NULL){
$sql = "SELECT * FROM il_leone_tavoli WHERE tipologia = 'disponibilita_totale'";
$result = mysqli_query($connection, $sql);
$row = mysqli_fetch_assoc($result);
$tables = $row["quantita"];
$tables -= 1;
$availability = $tables;
$sql_ = "INSERT INTO il_leone_ristorante (nome, cognome, persone, data, ora, preferenza, utente, disponibilita_totale) VALUES ('$name', '$surname', '$people', '$date', '$time', '$preference', '$username', '$availability')";
mysqli_query($connection, $sql_);
$date = explode("-", $date); // for italian date format, data means date (day) in italian
$data = "".$date[2]."/".$date[1]."/".$date[0]."";
$subject = "Tavolo prenotato - conferma digitale";
$to = "$email";
$message = "
<html>
<head>
<title>Conferma</title>
</head>
<body>
<h1 style=\"color: blue; font-weight:bold;\">Conferma prenotazione del tavolo</h1><br />
<p>Nome: $name</p>
<p>Cognome: $surname</p>
<p>Persone: $people</p>
<p>Data: $data</p>
<p>Ora: $time</p>
<p>E-mail: $email</p>
</body>
</html>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
mail($to, $subject, $message, $headers);
mysqli_free_result($result);
mysqli_close($connection);
echo "
<script type=\"text/javascript\">
window.alert('Tavolo prenotato 2, controlla la tua mail per la conferma digitale.');
window.location.replace('index.php');
</script>
";
} else {
mysqli_free_result($result);
mysqli_close($connection);
echo "
<script type=\"text/javascript\">
window.alert('Disponibilità terminata.');
window.location.replace('index.php');
</script>
";
}
}
} else {
mysqli_close($connection);
echo "
<script type=\"text/javascript\">
window.alert('Formato dati errato.');
window.location.replace('ristorante.php');
</script>
";
}
?>
It could be better I know but I'm just a beginner, this is the website I'm programming for a webdevelopment course I'm making, it's all in Italian unfortunately https://corsowebgo.sbmsp.it/il_leone_pizzeria_ristorante_affittacamere/ I'm doing this only for practice.