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();
	
		}
			
		
	
	
			
			
}

?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.