Hello, I want to link a site on the netlify host to a database linked directly to a form on my site. This database is on the host Alwaysdata.
<?php
// Connect to the database
$servername = "phpmyadmin.alwaysdata.com";
$username = "root";
$password = "Azerty";
$dbname = "test_reservation";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Database connection error: " . $conn->connect_error);
}
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve the data sent by the form
$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$destination = $_POST["destination"];
$category = $_POST["category"];
$date_depart = $_POST["date_depart"];
$date_return = $_POST["date_return"];
$nb_people = $_POST["nb_people"];
$comments = $_POST["comments"];
// Prepare the SQL insertion query
$sql = "INSERT INTO reservation (name, email, telephone, destination, category, date_departure, date_return, nb_people, comments)
VALUES ('$name', '$email', '$telephone', '$destination', '$category', '$date_departure', '$date_return', '$nb_persons', '$comments')";
// Execution of the SQL query
if ($conn->query($sql) === TRUE) {
echo "Reservation saved successfully.";
} else {
echo "Error saving reservation: " . $conn->error;
}
}
// Close the connection to the database
$conn->close();
?>
And the following API.php page:
<?php
header('Content-Type: application/json');
// Step 2: Include the connection file
require_once 'login.php';
// Step 3: Retrieve all reservations
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$query = $con->query("SELECT * FROM reservation");
if ($query) {
$reservations = [];
while ($row = $query->fetch_assoc()) {
$reservations[] = $row;
}
$response['error'] = false;
$response['reservations'] = $reservations;
$response['message'] = 'Retrieval of reservations was successful.';
} else {
$response['error'] = true;
$response['message'] = 'Failed to retrieve reservations.';
}
echo json_encode($response);
}
// Retrieve a reservation by name
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['name'])) {
$name = $_POST['name'];
$query = $con->prepare("SELECT * FROM reservation WHERE name =?");
$query->bind_param("s", $name);
if ($query->execute()) {
$result = $query->get_result();
if ($result->num_rows > 0) {
$reservation = $result->fetch_assoc();
$response['error'] = false;
$response['reservation'] = $reservation;
$response['message'] = 'Retrieval of reservation by name was successful.';
} else {
$response['error'] = true;
$response['message'] = 'No reservation found with this name.';
}
} else {
$response['error'] = true;
$response['message'] = 'Failed to retrieve reservation by name.';
}
echo json_encode($response);
}
$con->close();
// Example of sending a POST request to api.php from JavaScript to retrieve a reservation by name
const reservationName = 'John Doe'; // Replace 'John Doe' with the desired name
fetch('api.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: reservationName
})
})
.then(response => response.json())
.then(data => {
// Process response data
console.log(data);
})
.catch(error => {
// Error management
console.error('An error has occurred:', error);
});
?>
I have the following error:
This page of [âŠ].netlify.app cannot be found
No web page found at: https[âŠ]/processing.php
HTTP ERROR 404
Yet the page is located in the same directory as the other files in my github repository.
And the netlify deploy log tells me everything is âcompleteâ (initialization, building, deployment, cleanup and post-processing)
Can you clarify the error for me please? Thank you, I wish you a good day.