-1

I am trying to delete a patient record and any appointments associated with their Patient ID at the same time but neither are working, could someone please tell me where I am going wrong?

My code is:

<?php
include("includes/staffmenu.php");
include("includes/staffsession.php");
@require_once("includes/dbconfig.inc");

$patid = $_GET['patid'];

$patientname = mysql_query("SELECT * From patient WHERE Patient_ID=$patid");
    while($row = mysql_fetch_array($patientname))
        {   $pfname=$row['Patient_First_Name'];
            $pmname=$row['Patient_Middle_Name'];
            $psname=$row['Patient_Surname'];
        }

  echo "<h1>Success $pfname $pmname $psname (Patient ID: $patid) has been removed from our database, along with any appointments in their name</h1>";

    mysql_query("DELETE FROM appointment, patient 
    USING patient INNER JOIN appointment ON (patient.Patient_ID = appointment.Patient_ID) 
    WHERE patient.Patient_ID='$patid'");

  ?>

It definitely get's the patient ID because the name of patient ID echoes fine...

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • does it even work if you run the query manually? – Sebas Mar 17 '13 at 18:35
  • 2
    Side note: Consider using params instead of direct entry or you leave yourself open to sql injections, especially since you are getting patid directly from GET http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection-in-php – Christian Mar 17 '13 at 18:44
  • @Sebas by deleting manually do you mean from phpMyAdmIn? if so yes it works fine from there... – Tim Butterfield Mar 17 '13 at 18:51

1 Answers1

1
DELETE patients, appointments FROM patients
LEFT JOIN appointments USING(Patient_ID)
WHERE Patient_ID = 1

ref http://dev.mysql.com/doc/refman/5.5/en/delete.html

ps. consider using params to pass in patid as you are opening yourself up to sql injection using the value directly from GET

http://php.net/manual/en/mysqli-stmt.bind-param.php

pps. you should also put the echo last since you havent actually deleted at that point :)

Christian
  • 3,708
  • 3
  • 39
  • 60