dongteng2534 2015-11-12 16:22
浏览 68

使用php和javascript将数据插入数据库

I have been developing an appointment system over the past few weeks using php, mysql and javascript. I've come across a problem when trying to insert data into a table. I am using javascript on a php page to send a GET request to a php file which generates a table from database records. I have added a 'Book Appointment' button at the end of each row which should let the user confirm their appointment if its available and book it. Ive wrote all the code to get all relevant data from the table and then insert into a new table. The problem is that it does not insert. If I did this in php i could do this the way how im doing it now, but it would not be dynamic. I do not get any errors, it just does not insert the data. Do I need to do a ajax request in the javascript? I know theres lots of available help online with these sorts of problems, but cant seem to find an answer for my particular problem.

Heres my code:

<body>

<div id="textDisplay"<h1>Doctor availability and appointments will be  listed here...</h1></div>

</body>
<script>
function showAppointments(str, patient_id, patient_surname) {
if (str == "" || patient_id == "") {
document.getElementById("txtHint").innerHTML = "";
return;
 } else { 
 if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
} else {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        document.getElementById("textDisplay").innerHTML =   xmlhttp.responseText;
    }
}
xmlhttp.open("GET","getAppointments.php?listOption="+str +  "&patient_id="+patient_id + "&surname="+patient_surname, true);
xmlhttp.send();
}
}
}

This javascript function sends a get request to a php page with some data appended in the url so it can be picked up by the php page.

getAppointments.php then generates the table with relevant database records.

***** getAppointments.php: *****

 include 'dbConnect.php';
$listOptionVal = intval($_GET['listOption']);
$patient_id = $_GET['patient_id'];
$surname = $_GET['surname'];


 if ($listOptionVal == 0) {

$sql="SELECT * FROM patient_info, appointment_slots INNER JOIN doctors ON appointment_slots.doctor_id=doctors.doctor_id WHERE
 appointment_time BETWEEN '09:00:00' AND '12:00:00' AND appointment_date = '2015-11-02' AND patient_id = $patient_id;";
$result = $conn->query($sql);

echo "<center><h2>Available Appointments</h2><center>";
echo "<center><table>
    <tr>
    <th>Patient ID</th>
    <th>Patient Surname</th>
    <th>Date</th>
    <th>Times</th>
    <th>Avaialable</th>
    <th>Doctor</th>
    <th>Book Appointment</th>
    </tr>";
while($row = $result->fetch_assoc()) {
echo '<input type="hidden" name="patient_id" value="' .       $row['patient_id'] . '" />';
echo '<input type="hidden" name="surname" value="' . $row['surname'] . '" />';
echo '<input type="hidden" name="appointment_date" value="' .     $row['appointment_date'] . '" />';
echo '<input type="hidden" name="appointment_time" value="' . $row['appointment_time'] . '" />';
echo '<input type="hidden" name="appointment_avaliability" value="' . $row['appointment_avaliability'] . '" />';

echo "<form action='confirmed-appointments.php' method='post'    enctype='multipart/form-data'>";
echo "<tr>";
echo "<td>" . $row['patient_id'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['appointment_date'] . "</td>";
echo "<td>" . $row['appointment_time'] . "</td>";
echo "<td>" . $row['appointment_avaliability'] . "</td>";

 if ($row['appointment_avaliability'] == 'Y') {
    echo "<td style='color: #ACFB62;'>".$row['doctor_forename']."     </td>";
 } else if ($row['appointment_avaliability'] == 'N') {
    echo "<td style='color:  #FF6666;'>".$row['doctor_forename']."   </td>";
}
  echo "<td>
        <input type='submit' name='bookAppointment' value='Book  Appointment'>
    </td>";
  echo "</tr>";
 } 
 }
echo "</table></center>";
echo "</form>";

This will then generate a table with appointment availability times. There is a button appended to each row allowing the user to book that appointment. This is where I want to send the data to the database and then loop through the results and show on a new page.

**** confirmed appointments.php - should be looping through rows of inserted data ****

<?php
include 'dbConnect.php';

if (isset($_POST['bookAppointment'])) {
var_dump($_POST);
$patient_id = $_POST['patient_id'];
$surname = $_POST['surname'];
$forename = $_POST['appointment_date'];
$dob = $_POST['appointment_time'];
$doctor = $_POST['doctor_forename'];

$sql = "INSERT INTO confirmed_appointments SET patient_id='" .   $patient_id . "', surname='" . $surname .  "', appointment_date='" . $appointment_date . "', appointment_time='" . $appointment_time . "', doctor='" . $doctor . "'";

if ($conn->query($sql) === TRUE) {
echo "Record added successfully";
} else {
    echo "Error: " . $sql . "
        " . $conn->error;
}
}

$sql = "SELECT * from confirmed_appointments;";
$result = $conn->query($sql);

while ($row = $result->fetch_assoc()) {
echo "<table border = 1>";
echo "<tr>";
echo "<td>" . $row['patient_id'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['appointment_date'] . "</td>";
echo "<td>" . $row['appointment_time'] . "</td>";
echo "<td>" . $row['doctor'] . "</td>";
echo "</tr>";
echo "</table>";
}
$conn->close();
?>

The data does not get inserted so therefore does not get displayed on the confirmed-appointments.php page. I hope i've been clear enough and I hope my question does not get closed!! Thanks for your time

  • 写回答

1条回答 默认 最新

  • dongshan4518 2015-11-12 16:32
    关注

    assuming all the post data is correct , db connection success and there's no typo from any of your variable.. sometimes just doesn't works without closing double quote...

    $sql = "INSERT INTO confirmed_appointments SET patient_id='" . $patient_id . "', surname='" . $surname .  "', appointment_date='" . $appointment_date . "', appointment_time='" . $appointment_time . "', doctor='" . $doctor . "'";
    

    instead, did you try to var_dump your $_POST? i dont think you have any value post from your form.

    while($row = $result->fetch_assoc()) {
    echo "<form method='get'>";
    echo "<tr>";
    echo "<td>" . $row['patient_id'] . "</td>";
    echo "<td>" . $row['surname'] . "</td>";
    echo "<td>" . $row['appointment_date'] . "</td>";
    echo "<td>" . $row['appointment_time'] . "</td>";
    echo "<td>" . $row['appointment_avaliability'] . "</td>";
    
    if ($row['appointment_avaliability'] == 'Y') {
        echo "<td style='color: #ACFB62;'>".$row['doctor_forename']."     </td>";
    } else if ($row['appointment_avaliability'] == 'N') {
        echo "<td style='color:  #FF6666;'>".$row['doctor_forename']." </td>";
    }
    echo "<td>
            <input type='submit' name='bookAppointment' value='Book  Appointment' action='confirmed-appointments.php'>
        </td>";
    echo "</tr>";
    echo "</form>";
     } 
    }
    

    you're just displaying the value and the form method is get, do you have any input value?

    i saw your variable at

    $patient_id = $_POST['patient_id'];
    $surname = $_POST['surname'];
    $forename = $_POST['appointment_date'];
    $dob = $_POST['appointment_time'];
    $doctor = $_POST['doctor_forename'];
    

    but where is your

    input type="text" name="patient_id" value=" . $row['patient_id'] 
    

    at your getAppointments.php?


    getAppointments.php

    include 'dbConnect.php';
    $listOptionVal = intval($_GET['listOption']);
    $patient_id = $_GET['patient_id'];
    $surname = $_GET['surname'];
    
    
     if ($listOptionVal == 0) {
    
     $sql="SELECT * FROM patient_info, appointment_slots INNER JOIN doctors ON appointment_slots.doctor_id=doctors.doctor_id WHERE
     appointment_time BETWEEN '09:00:00' AND '12:00:00' AND     appointment_date = '2015-11-02' AND patient_id = $patient_id;";
    $result = $conn->query($sql);
    
    echo "<center><h2>Available Appointments</h2><center>";
    echo "<form method='post' action = 'confirmed-appointments.php'>";
    echo "<center><table>
    <tr>
    <th>Patient ID</th>
    <th>Patient Surname</th>
    <th>Date</th>
    <th>Times</th>
    <th>Avaialable</th>
    <th>Doctor</th>
    <th>Book Appointment</th>
    </tr>";
    while($row = $result->fetch_assoc()) {
    
    // things that you might miss out 
    echo '<input type="hidden" name="patient_id" value="' . $row['patient_id'] . '" />';
    echo '<input type="hidden" name="surname" value="' . $row['surname'] . '" />';
    echo '<input type="hidden" name="appointment_date" value="' . $row['appointment_date'] . '" />';
    echo '<input type="hidden" name="appointment_time" value="' . $row['appointment_time'] . '" />';
    echo '<input type="hidden" name="appointment_avaliability" value="' . $row['appointment_avaliability'] . '" />';
    echo '<input type="hidden" name="doctor_forename" value="' . $row['doctor_forename'] . '" />';
    
    
    echo "<tr>";
    echo "<td>" . $row['patient_id'] . "</td>";
    echo "<td>" . $row['surname'] . "</td>";
        echo "<td>" . $row['appointment_date'] . "</td>";
        echo "<td>" . $row['appointment_time'] . "</td>";
        echo "<td>" . $row['appointment_avaliability'] . "</td>";
    
        if ($row['appointment_avaliability'] == 'Y') {
            echo "<td style='color: #ACFB62;'>".$row['doctor_forename']."     </td>";
        } else if ($row['appointment_avaliability'] == 'N') {
            echo "<td style='color:  #FF6666;'>".$row['doctor_forename']." </td>";
        }
        echo "<td>
                <input type='submit' name='bookAppointment' value='1'>
            </td>";
    echo "</tr>";
     } 
    }
     echo "</table></center>";
    echo "</form>";
    

    confirm appointments.php

    if (isset($_POST['bookAppointment'])) {
        $patient_id = $_POST['patient_id'];
        $surname = $_POST['surname'];
        $forename = $_POST['appointment_date'];
        $dob = $_POST['appointment_time'];
        $doctor = $_POST['doctor_forename'];
    
        $sql = "INSERT INTO confirmed_appointments SET patient_id='" . $patient_id . "', surname='" . $surname .  "', appointment_date='" . $appointment_date . "', appointment_time='" . $appointment_time . "', doctor='" . $doctor . "'";
    
        if ($conn->query($sql) === TRUE) {
        echo "Record added successfully";
        } else {
            echo "Error: " . $sql . "
                " . $conn->error;
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line