Hi all. Here is my problem. I'm a newbie to PHP programming. For my project I just need to run 3 Insert statements in a single button click. The concept is I have a registration form. It has parent details and children details. If parents want to register more than one kid they need to click add more the entire form will reappear and they just enter the details for the number of children they want and save. During save I need to save the parent details in a separate table first. Then I need to pick the last inserted id from parent table and save the id in to next table for 'n' number of children. Again I need to pick the individual student id for the same exact parent and save it in to 'stud_class' table as stud_id for multiple class selection from checkbox.
<?php
$conn = mysql_connect("localhost","rock","bach");
mysql_select_db("rockandbach",$conn);
if(!empty($_POST["save"])) {
$name=mysql_real_escape_string($_POST['name']);
$update=mysql_query("INSERT INTO data1 (fname,status) VALUES ('$name','1')");
$last_id = mysql_insert_id($conn);
echo $last_id;
$itemCount = count($_POST["fname"]);
$itemValues=0;
$query = "INSERT INTO data_test (fname,lname,gender,status,dataid) VALUES ";
$queryValue = "";
for($i=0;$i<$itemCount;$i++) {
if(!empty($_POST["fname"][$i]) || !empty($_POST["lname"][$i]) || !empty($_POST["gender"][$i])) {
$itemValues++;
if($queryValue!="") {
$queryValue .= ",";
}
$queryValue .= "('" . $_POST["fname"][$i] . "', '" . $_POST["lname"][$i] . "','" . $_POST["gender"][$i] . "','1',$last_id)";
$last_id1 = mysql_insert_id($conn);
echo $last_id1;
$checkbox1 = $_POST['country'];
for( $i=0 ; $i<count($checkbox1); $i++){
$query = "insert into stud_class (stud_id,class_id) values ('$last_id1','".$checkbox1[$i]."')";
mysql_query($query) or die (mysql_error());
}
}
}
$sql = $query.$queryValue;
if($itemValues!=0) {
$result = mysql_query($sql);
if(!empty($result)) $message = "Added Successfully.";
}
}
?>