Adding Records with PHP

Next, we will replace the line below with the code to add the patient to the database:

if($ok) echo 'The information is complete.<BR>'; //can be entered in database

if($ok) {
  include($_SERVER[DOCUMENT_ROOT]."/data/connection.php");  //wherever yours is
  connect();
  $bday="$year/$month/$day";
  $sql='INSERT INTO Patients(FirstName,LastName,Birthdate,Gender) '.
    'VALUES("'.$firstName.'","'.$lastName.'","'.$bday.'","'.$gender.'")';
  echo $sql.'<BR>';
  $result = mysql_query($sql);
  $sql='SELECT LAST_INSERT_ID() AS ID';
  $result = mysql_query($sql);
  $row=mysql_fetch_array($result);
  extract($row);
  echo $ID.'<BR>';
  foreach($disease as $key=>$info) {
    $sql='INSERT INTO Disease(ID,Disease) VALUES("'.$ID.'","'.$info.'")';
    echo $sql.'<BR>';
  }
}

It is important to note that the SELECT LAST_INSERT_ID() is connection specific. If another person adds a person you will still retrieve the correct ID for the person you added.

The results from an SQL query can not be accessed directly. First you need to get a row, then you need to extract the array.


INDEX, INSERT: Adding records, Add related data
Next lesson: Data NormalizationMAIN INDEX
EXAMPLES INDEX

Copyright © Zebra0.com
All rights reserved worldwide.

 
 

Add related data