0

This is my final year engineering project. And i am stuck at this point where the data from the form won't get entered into the database for some reason. Error logs in apache2 also show me no errors that would give me more insight into my problem.

manage.php code: Handles entering of patient data onto a simple form

<?php

session_start();
if (!isset($_SESSION['loggedin']))
{
    echo 'Please log into your profile before you continue. Redirecting in 5 seconds to login';
    header ('Refresh:5 ; URL=doctorlogin.html');
    exit;
}

$generated_id = uniqid('HFH-',FALSE);

?>

<!DOCTYPE HTML>
<html>
    <head>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
        <script src="https://kit.fontawesome.com/7ef9230af3.js" crossorigin="anonymous"></script>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css">
        <title>Doctor's Portal</title>
    </head>
    <body>
        <div class="container-fluid">
            <div class="row">
                <div class="col-sm-auto bg-light sticky-top">
                    <div class="d-flex flex-sm-column flex-row flex-nowrap bg-light align-items-center sticky-top">
                        <a href="/" class="d-block p-3 link-dark text-decoration-none" title="" data-bs-toggle="tooltip" data-bs-placement="right" data-bs-original-title="Icon-only">
                            <i class="bi bi-bandaid fs-1"></i>
                        </a>
                        <ul class="nav nav-pills nav-flush flex-sm-column flex-row flex-nowrap mb-auto mx-auto text-center align-items-center">
                            <li class="nav-item">
                                <a href="doctorhome.php" class="nav-link py-3 px-2" title="" data-bs-toggle="tooltip" data-bs-placement="right" data-bs-original-title="Home">
                                <i class="bi-house fs-1"></i>
                                </a>
                            </li>
                            <li>
                                <a href="#" class="nav-link py-3 px-2" title="" data-bs-toggle="tooltip" data-bs-placement="right" data-bs-original-title="Dashboard">
                                    <i class="bi bi-bar-chart-line fs-1"></i>
                                </a>
                            </li>
                            <li>
                                <a href="#" class="nav-link py-3 px-2" title="" data-bs-toggle="tooltip" data-bs-placement="right" data-bs-original-title="Generate Reports">
                                    <i class="bi bi-book-half fs-1"></i>
                                </a>
                            </li>
                            <li>
                                <a href="manage.php" class="nav-link py-3 px-2 active" aria-current="page" title="" data-bs-toggle="tooltip" data-bs-placement="right" data-bs-original-title="Manage Patients">
                                    <i class="bi-people fs-1"></i>
                                </a>
                            </li>
                            <li>
                                <a href="viewpatients.php" class="nav-link py-3 px-2" title="" data-bs-toggle="tooltip" data-bs-placement="right" data-bs-original-title="View Patients">
                                    <i class="bi bi-table fs-1"></i>
                                </a>
                            </li>
                        </ul>
                        <div class="dropdown">
                            <a href="#" class="d-flex align-items-center justify-content-center p-3 link-dark text-decoration-none dropdown-toggle" id="dropdownUser3" data-bs-toggle="dropdown" aria-expanded="false">
                                <i class="bi bi-person-check fs-1"></i>
                            </a>
                            <ul class="dropdown-menu text-small shadow" aria-labelledby="dropdownUser3">
                                <li><a class="dropdown-item" href="doctorpasswordupdate.php">Update Details</a></li>
                                <li><a class="dropdown-item" href="doctorprofile.php">Profile</a></li>
                                <li><a class="dropdown-item" href="logout_doctor.php">Logout</a></li>
                            </ul>
                        </div>
                    </div>
                </div>
                <div class="col-sm p-3 min-vh-100">
                    <h3 class="mb-2">Add patients to the system</h6>
                    </br>
                    <form action="addpatient.php" method="post" autocomplete="off">
                        <div class="row">
                            <div class="col-md-3 mb-4">
                                <div class="form-outline">
                                    <input type="text" name="patient_id" id="patient_id" value="<?php echo $generated_id?>" class="form-control form-control-lg" disabled>
                                    <label class="form-label" for="patient_id">Pre-generated Patient ID</label>
                                </div>
                            </div>
                            <div class="col-md-3 mb-4">
                                <div class="form-outline">
                                    <input type="text" name="dr_incharge" id="dr_incharge" class="form-control form-control-lg" required>
                                    <label class="form-label" for="dr_incharge">Doctor incharge of the patient</label>
                                </div>
                            </div>
                            <div class="col-md-6 mb-4">
                                <div class="form-outline">
                                    <select name="machine_available" id="machine_available" class="select form-control-lg">
                                        <option value="">Choose Available Machines</option>
<?php 
    $DATABASE_HOST='localhost';
    $DATABASE_USER='admin';
    $DATABASE_PASS='dsouza';
    $DATABASE_NAME='patient_monitoring';
    
    $con = mysqli_connect($DATABASE_HOST , $DATABASE_USER , $DATABASE_PASS , $DATABASE_NAME);
    
    if (mysqli_connect_error()) {
        exit ('Failed to connect to MYSQL: ' . mysqli_connect_error());
    }

    $sql = "SELECT machine_identifier FROM machine_available WHERE availability = 'available'";
    $result = $con -> query($sql);
    $con -> close();
    while ($row = $result -> fetch_assoc()) {
?>
                                                <option value="<?php echo $row['machine_identifier'];?>">
                                                    <?php echo $row['machine_identifier'];?>
                                                </option>
<?php
}
?>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6 mb-4">
                                <div class="form-outline">
                                    <input type="text" name="full_name" id="full_name" class="form-control form-control-lg" required>
                                    <label class="form-label" for="full_name">Full Name</label>
                                </div>
                            </div>
                            <div class="col-md-6 mb-4">
                                <div class="form-outline">
                                    <input type="text" name="phone_number" id="phone_number" class="form-control form-control-lg" required>
                                    <label class="form-label" for="phone_number">Phone Number</label>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-12">
                                <div class="form_outline">
                                    <input type="text" name="address" id="address" class="form-control form-control-lg required">
                                    <label class="form-label" for="address">Address</label>
                                </div>
                            </div>
                        </div>
                        </br>
                        <div class="row">
                            <div class="col-12">
                                <div class="form_outline">
                                    <input type="text" name="admitted_for" id="admitted_for" class="form-control form-control-lg required">
                                    <label class="form-label" for="admitted_for">Admitted for which treatment</label>
                                </div>
                            </div>
                        </div>
                        </br>
                        <div class="row">
                            <h6 class="mb-2">Corresponding Machines and Room and Bed Number Details</h6>
                        </div>
                        <div class="row">
                            <table class="table table-striped">
                                <tr>
                                    <th>Machine Identifier</th>
                                    <th>Room Number</th>
                                    <th>Bed Number</th>
                                </tr>
<?php 
$DATABASE_HOST='localhost';
$DATABASE_USER='admin';
$DATABASE_PASS='dsouza';
$DATABASE_NAME='patient_monitoring';

$con = mysqli_connect($DATABASE_HOST , $DATABASE_USER , $DATABASE_PASS , $DATABASE_NAME);

if (mysqli_connect_error())
{
exit ('Failed to connect to MYSQL: ' . mysqli_connect_error());
}

$sql = "SELECT machine_identifier , room_number , bed_number FROM room_details";
$result = $con -> query($sql);
$con -> close();
while ($row = $result -> fetch_assoc())
{
?>
                                        <tr>
                                            <td><?php echo $row['machine_identifier'];?></td>
                                            <td><?php echo $row['room_number'];?></td>
                                            <td><?php echo $row['bed_number'];?></td>
                                        </tr>
<?php
}
?>
                            </table>
                        </div>
                        <div class="mt-4 pt-2">
                            <input class="btn btn-primary btn-lg" type="submit" value="Add Patient to system">
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>

addpatient.php: This file is responsible for getting the data from manage.php file and enter it into the database. With this file, i am updating 2 tables. One is patient_data table where the patient details will get added and the machine_available table where the details and the availability of machines is stored. Once a machine is allocated through the form to the patient, the machine_available table has the updated value as occupied for that machine_identifier.

<?php

session_start();
if (!isset($_SESSION['loggedin'])) {
    echo 'Please log into your profile before you continue. Redirecting in 5 seconds to login';
    header ('Refresh:5 ; URL=doctorlogin.html');
    exit;
}

$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'admin';
$DATABASE_PASS = 'dsouza';
$DATABASE_NAME = 'patient_monitoring';

$con = mysqli_connect($DATABASE_HOST , $DATABASE_USER , $DATABASE_PASS , $DATABASE_NAME);

if (mysqli_connect_error()) {
    exit ('Failed to connect to MYSQL: ' . mysqli_connect_error());
}

$rnumber = "";
$bnumber = "";

if ($stmt = $con -> prepare('SELECT patient_id , full_name FROM patient_data WHERE patient_id = ?'))
{
    $stmt -> bind_param('s' , $_POST['patient_id']);
    $stmt -> execute();
    $stmt -> store_result();
    if ($stmt -> num_rows > 0)    {
        echo 'Patient already exists in database';
    } else {
        if ($stmt = $con -> prepare ('SELECT room_number , bed_number FROM room_details WHERE machine_identifier = ?'))
        {
            $stmt -> bind_param('s' , $machine_identifier);
            $stmt -> execute();
            $stmt -> bind_result($rnumber , $bnumber);
            $stmt -> fetch();
        } else {
            echo "SQL Error" . $stmt->error();
        }
        if ($stmt = $con -> prepare ('INSERT INTO patient_data (patient_id , full_name , address , phone_number , room_no , bed_no , dr_incharge , machine_identifier , admitted_for , added_by) VALUES (? , ? , ? , ? , ? , ? , ? , ? , ? , ?)'))
        {
            $stmt -> bind_param('ssssssssss' , $_POST['patient_id'] , $_POST['full_name'] , $_POST['address'] , $_POST['phone_number'] , $rnumber , $bnumber , $_POST['dr_incharge'] , $machine_identifier , $_POST['admitted_for'] , $_SESSION['username']);
            $stmt -> execute();
        } else {
            echo "SQL Error" . $stmt->error();
        }
        if ($stmt = $con -> prepare ("UPDATE machine_available SET availability = 'occupied' WHERE machine_identifier = ? "))
        {
            $stmt -> bind_param('s' , $machine_identifier);
            $stmt -> execute();
        } else {
            echo "SQL Error" . $stmt -> error();
        }
    }
    $stmt -> close();
    echo 'Redirecting to add patients in 5 seconds';
    header ('Refresh:5 ; URL=manage.php');
}
?>

When i enter the details and click on the button, the last line echo 'Redirecting to add patients in 5 seconds'; gets executed which tells me that wait the database has been updated. However, when i check the database, the data is not there. The machine_available table is also not updated and the patient_data table is also not updated. The machine_available table data is fetched onto a dropdown list from where i select one of the available machines. The room and bed number is available in room_details table where the details are then fetched into variables and stored there using the machine_identifier. However, the data is not being inserted into the database and there are no errors as well in the apache2 logs. I am using a Raspberry Pi 4B as my webserver. The room details and the machine availability is all sent by my arduino model using esp8266 and php post method. The mysql db has the availability section defaulted to available which gets updated once i allocate it through my website. However, the data doesnt seem to change or upload and the logs doesn't show me any errors as well.

Attaching photos for my mysql DB as well: -

machine-available table

patient-data table

room-details table

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Rosch Rocks
  • 9
  • 1
  • 1
  • 4
  • To get errors out of PHP _even in a LIVE environment_ add these 4 lines **temporarily, while debugging**, to the top of any `MYSQLI_` based script you want to debug `ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);`. This will force any `MYSQLI_` errors to generate an Exception that you can see on the browser as well as normal PHP errors. – RiggsFolly Mar 18 '22 at 16:53
  • Next time read this first: https://stackoverflow.com/help/minimal-reproducible-example You have NO error checking on any of the query executions. – symcbean Mar 18 '22 at 16:55
  • You only need to connect to the database ONCE PER Script and then reuse the connection handle. If you do it once for every query you run in a script you will very soon be greeted with a lack of connections. Imagine 5 connects per script x 1000 users! PLUS the process of making the connection can be quite a time consuming one – RiggsFolly Mar 18 '22 at 16:58
  • Also [**Please Never** post images of code, data or error messages](https://meta.stackoverflow.com/a/285557/2310830). Please edit your question and include copy/paste the text into the question, formatted. This is so that we can try to reproduce the problem without having to re-type everything, and your question can be properly indexed or read by screen readers. – RiggsFolly Mar 18 '22 at 17:00
  • Well, after enabling errors, the patient ID is not being passed and its throwing an exception error that patient_id cannot be null. this gives me a huge insight into the problem – Rosch Rocks Mar 18 '22 at 17:52

0 Answers0