rguitar Posted March 22, 2013 Share Posted March 22, 2013 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 https://forums.phpfreaks.com/topic/276001-insert-to-two-different-tables-in-database/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.