Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

$conn = mysqli _ connect ( $servername, $username, $password, $dbname ) ; / / if ( ! $conn ) { die ( Connection failed:

$conn = mysqli_connect($servername, $username, $password, $dbname);
//
if (!$conn){
die("Connection failed: ". mysqli_connect_error());
}
if ($_SERVER["REQUEST_METHOD"]== "POST"){
//
if (!empty($_POST["name"]) && !empty($_POST["date_of_birth"]) && !empty($_POST["phone"]) && !empty($_POST["center_name"]) && !empty($_POST["reservation_date"])){
//
$name = $_POST["name"];
$date_of_birth = $_POST["date_of_birth"];
$phone = $_POST["phone"];
$center_name = $_POST["center_name"];
$reservation_date = $_POST["reservation_date"];
// ID
$center_id = null;
$stmt = $conn->prepare("SELECT center_id FROM P_Blood_Center WHERE center_name =?");
if (!$stmt){
die("Prepare failed: ". $conn->error);
}
$stmt->bind_param("s", $center_name);
$stmt->execute();
$stmt->bind_result($center_id);
$stmt->fetch();
$stmt->close();
if ($center_id === null){
die("Center ID not found");
}
// ID
$donor_id = getDonor_id($conn, $phone);
// ID
if ($donor_id){
// ID
insertP_Donation($conn, $donor_id, $center_id, $reservation_date);
} else {
// ID
$donor_id = insertP_Donor($conn, $name, $date_of_birth, $phone);
if ($donor_id){
insertP_Donation($conn, $donor_id, $center_id, $reservation_date);
} else {
echo "alert('Error adding new donor');";
}
}
} else {
echo "alert('All fields must be filled out');";
}
}
$stmt->close();
//
function insertP_Donation($conn, $donor_id, $center_id, $reservation_date){
$stmt = $conn->prepare("INSERT INTO P_Donation (center_id, reservation_date, donor_id) VALUES (?,?,?)");
if (!$stmt){
die("Prepare failed: ". $conn->error);
}
$stmt->bind_param("iss", $center_id, $reservation_date, $donor_id);
if ($stmt->execute()){
echo "alert('Data Inserted Successfully');";
} else {
echo "alert('Error Inserting Data: ". $stmt->error ."');";
}
$stmt->close();
}
function insertP_Donor($conn, $name, $date_of_birth, $phone){
$stmt = $conn->prepare("INSERT INTO P_Donor (name, date_of_birth, phone) VALUES (?,?,?)");
if (!$stmt){
die("Prepare failed: ". $conn->error);
}
$stmt->bind_param("sss", $name, $date_of_birth, $phone);
if ($stmt->execute()){
return $stmt->insert_id;
} else {
echo "alert('Error Inserting Donor: ". $stmt->error ."');";
return null;
}
$stmt->close();
}
function getDonor_id($conn, $phone){
$stmt = $conn->prepare("SELECT donor_id FROM P_Donor WHERE phone =?");
if (!$stmt){
die("Prepare failed: ". $conn->error);
}
$stmt->bind_param("s", $phone);
$stmt->execute();
$stmt->bind_result($donor_id);
$stmt->fetch();
$stmt->close();
return $donor_id;
}
I omitted some codes including connection info. (connection doesn't have any problems) I tried to insert data in DB when form in this html file registered. But nothing happens including error text. What's the matter?
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions