Jump to content

Insert to two different tables in Database


rguitar

Recommended Posts

 Im inserting information in the first table then getting InsertID for the foreign key for the second table to tie the records together. The second table will not insert and

 


if(isset($_POST['submit']))
{


$date = $_POST['txt_date'];
$hospital = $_POST['hospital_dd'];

//prepare statement
$stmt = $conn->stmt_init();

//sql
$sql = "INSERT INTO Brucella (ClientName, PatientName,IDNumber,Age,AgeType,Sex,Doctor,Results,TestId) values (?,?,?,?,?,?,?,?,?)";
$sqlTest = "Insert Into BrucellaTest (DateReported, DateSubmitted, Technician, Samples) values (?,?,?,?)";


if($_POST['hospital_dd'] >= 1){

		if(isset($_GET['TestId']) ){


			$TheTestId = $_GET['TestId'];

			$sqlUpdate = "Update BrucellaTest set DateReported = ?, DateSubmitted = ?, Technician = ?, Samples=? where Id ='$TheTestId'";
 

				$stmt->prepare($sqlUpdate);
				$stmt->bind_param('sssi',$_POST['date_reported'],$_POST['txt_date'],$_POST['tech'],$_POST['samples']);
				$stmt->execute();
				 $InsertId = $_GET['TestId'];
 


				}else
				{



 
 
				$stmt->prepare($sqlTest);
				$stmt->bind_param('sssi',$_POST['date_reported'],$_POST['txt_date'],$_POST['tech'],$_POST['samples']);
				$stmt->execute();
				$InsertId = $stmt->insert_id;
				
				}

			}
			
				if($stmt->prepare($sql)){

	//bind parameters
	$stmt->bind_param('ssssssssi', $_POST['hospital_dd'],$_POST['txt_patient_name'], $_POST['txt_id'],$_POST['txt_age'],$_POST['age_value'],$_POST['sel_sex'],$_POST['txt_doctor'],$_POST['sel_results'], '$InsertId');
	$stmt->execute();
	
		}
			
		
	
	
			
			
}

?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.