how to insert data into two table using in php mysql

May 28 2015 3:46 AM

how to insert data into two table using one single query in php mysql? my table name are popup_question and popup_answer . popup_answer table has multiple row and i went to insert multiple row on the table popup_answer and popup_question . popup_question does not have multiple row. please help me. my code is bellow
thank's
 
<?php
include('config.php');
session_start();
$msg="";
if(isset($_POST['h_action']))
{
$val_h_action=$_POST['h_action'];
}
if(isset($_POST['h_id']))
{
$val_h_id=$_POST['h_id'];
}
if($val_h_action=="delete")
{
mysql_query("DELETE FROM popup_question WHERE Id='$val_h_id'");
mysql_query("DELETE FROM popup_answar WHERE Question_id='$val_h_id'");
$msg=md5("1");
header("location:Add_pop_up_question.php?msg=$msg");
}
/* $option_b_campaign="<option value='' id='radi_id1'>- SELECT A CAMPAIGN -</option>";
$result2=mysql_query("select DISTINCT campaign_id from popup_question");
while($qdata=mysql_fetch_array($result2))
{
$campaign_id=$qdata['campaign_id'];
//$asset_name=$qdata['asset_type'];
$option_b_campaign .= "<option id='radi_id1' value='$campaign_id'>$campaign_id</option>";
} */
//$sql=mysql_query("select DISTINCT campaign_id from popup_question");
//while($row=mysql_fetch_array($sql))
//{
//echo "<option onclick='change("<?php echo $row['campaign_id'];
$msg1="";
if(isset($_POST['h_action']))
{
$val_h_action=$_POST['h_action'];
}
if($val_h_action=="insert")
{
$campaign=$_POST['campaign_id'];
$question=$_POST['question'];
$catagory=$_POST['mode'];
$sid=$_POST['sid'];
$qid=$_POST['qid'];
$ast=$_POST['answar_text'];
$sql=("INSERT INTO popup_question(Id,campaign_id,Question_text,Mod_id,Set_ID,Question_ID)VALUES('','$campaign','$question','$catagory','$sid','$qid')");
mysql_query($sql);
//if(isset($_POST['h_action']))
//{
// $val_h_action=$_POST['h_action'];
//}
//if($val_h_action=="insert")
//{
$total_rows = $_POST['total_record_count'];
if($total_rows <2)
{
$ast=$_POST['qid_1'];
$sql=mysql_query("INSERT INTO popup_answar(Answar_text )VALUES('$ast')");
$msg=md5("1");
}
else
{
for($m=1;$m<=$total_rows;$m++)
{
$ast = $_POST['qid_'.$m];
$sql=mysql_query("INSERT INTO popup_answar(Answar_text)VALUES('$ast')");
//echo "INSERT INTO reportsequencequestions(QuestionID)VALUES(['$qst'])";
}
$msg=md5("1");
header("location:report_sequence.php?msg=$msg");
}
//}
mysql_close($con);
//tableid1 & tableid2 are auto-incrementing primary keys
mysql_query($sql2);
$msg1=md5("1");
header("location:Add_pop_up_question.php?msg1=$msg1");
//echo"INSERT INTO popup_question(campaign_id,Question_text,Mod_id,Set_ID,Question_ID)VALUES('$campaign','$question','$catagory','$sid','$qid')";
}
?>
<html>
<head>
<link rel="stylesheet" href="themes/ui-lightness/jquery.ui.all.css">
<link href="css/cssmenu.css" rel="stylesheet" type="text/css" />
<link href="css/inner.css" rel="stylesheet" type="text/css" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script src="popjquery/jquery-1.10.2.js"></script>
<script src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('.p').hide()
$('#text').click(function(){
$('.p').show()
});
});
</script>
<script type="text/JavaScript">
function insert()
{
document.form1.h_action.value="insert";
document.form1.submit();
}
</script>
<script>
/* function change(id){
alert(id);
document.form1.h_value.value=id;
document.form1.h_action.value='selectcam';
document.form1.submit();
}*/
</script>
<script>
function upload()
{
document.form1.h_action.value="upload";
document.form1.submit();
}
</script>
<script language="javascript">function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "checkbox";
element1.name = "chkbox[]";
cell1.appendChild(element1);
var cell2 = row.insertCell(1);
cell2.innerHTML = rowCount;
var cell3 = row.insertCell(2);
var element2 = document.createElement("input");
element2.type = "text";
element2.name = "qid_"+rowCount;
cell3.appendChild(element2);
document.getElementById('total_record_count').value = rowCount;
}
function deleteRow(tableID) {
try {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for (var i = 0; i < rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if (null != chkbox && true == chkbox.checked) {
table.deleteRow(i);
rowCount--;
i--;
}
}
} catch (e) {
alert(e);
}
}
</script>
<script type="text/JavaScript">
function validation()
{
if(document.form1.campaign_id.value=="blank")
{
alert("Please select campaign Id..");
return false;
}
if(document.form1.question.value=="")
{
alert("Enter text question ..");
return false;
}
if(document.form1.mode.value=="blank1")
{
alert("Please select catagory mode..");
return false;
}
if(document.form1.sid.value=="")
{
alert("Please enter set id..");
return false;
}
if(document.form1.qid.value=="")
{
alert("Please enter Question id..");
return false;
}
}
</script>
<script type="text/JavaScript">
function del(i)
{
//var r=confirm("Are you Confirm to delete");
if (confirm("Are you Confirm to delete!") == true){
document.form1.h_id.value=i;
document.form1.h_action.value="delete";
document.form1.submit();
}
}
</script>
<script type="text/JavaScript">
function change(){
document.getElementById("myform").submit();
}
</script>
</head>
<body>
<?php include ('header.php');?>
<div class="maindiv">
<div class="wapper">
<div class="mainbody" style="height:auto;">
<div class="loginbar">
<h3>Qualify Question Entry</h3>
</div>
<div class="content-box-content">
<form action="" name="form1" method="post" onsubmit="return validation();" id="myform">
<input type="hidden" name="h_action" >
<input type="hidden" name="h_value" />
<input type="hidden" name="h_id">
<table border="10">
<tr><td class="style1">
<label style="font-size:15px;font-style:italic;">Campaign</label></td><td>
<select class="medium" name="campaign_id" style="width:450px;" onChange="this.form.submit()" >
<option value="blank">Select campaign</option>
<?php
$sql=mysql_query("select DISTINCT campaign_id from popup_question");
while($row=mysql_fetch_array($sql))
{
?>
<option id="id_radio1" value="<?php echo $row['campaign_id']; ?>" <?php if($row['campaign_id'] == $_REQUEST['campaign_id']){ echo 'selected';}?> ><?php echo $row['campaign_id']; ?> </option>
<?php
}
?>
</select>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
<label style="font-size:15px;font-style:italic;">Question</label></td><td>
<input type="text" name="question" Class="medium" style="width:450px;height:35px;" >
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
<label style="font-size:15px;font-style:italic;">Category</label></td><td>
<select Class="medium" name="mode" style="width:450px;">
<option value="blank1">select mode</option>
<option value="1">Manual</option>
<option value="2" id="text">Multiple Option</option>
</select>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
<label style="font-size:15px;font-style:italic;">Set ID</label></td><td>
<input type="text" name="sid" Class="medium" style="width:450px;height:35px;" >
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
<label style="font-size:15px;font-style:italic;">Question ID</label></td><td>
<input type="text" name="qid" Class="medium" style="width:450px;height:35px;">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
<label style="font-size:15px;font-style:italic;">ANSWAR OPTION</label>
<input type="text" name="answar_text" Class="medium" style="width:450px;height:35px;">
</td>
<td>
<div class="p">
<table id="dataTable" width="350px" border="1">
<tr bgcolor="#33FFFF"><th>Action</th><th>SL</th><th>Value</th></tr>
<tbody><tr>
<td><input type="checkbox" name="chk"></td>
<td> 1 </td>
<td> <input type="text" name="qid_1"> </td>
<input type="hidden" id="total_record_count" name="total_record_count" value="">
</tr>
</tbody>
</table>
<table border="1">
<tr><td><input type="button" value="Add row" onClick="addRow('dataTable')" class="btnz"></td>
<td><input type="button" value="Delete row" onClick="deleteRow('dataTable')" Class="btnz"></td></tr>
</table>
</div>
</td>
</tr>
<tr><?php if(isset($_GET['msg1'])!=""){?><td style="color: green;">Insert Successfully</td><?php } ?><?php if(isset($_GET['msg'])!=""){?><td style="color: red;">Deleted Successfully</td><?php } ?></tr>
<tr><td><input type="button" value="Submit" onClick="insert();" Class="btnz"></td></tr>
</table>
</div>
<div class="loginbar">
<h3>Existing Pop Up Questions</h3>
</div>
<div class="content-box-content">
<p style="width: 100%; height: auto">
<?php
/* $val_h_action=$_POST['h_action'];
$val_h_id=$_POST['h_value'];
if($val_h_action=='selectcam')
{ */
//$Campaign_Name=$_POST['campaign_id'];
/* $query="SELECT * FROM popup_question WHERE campaign_id='$val_h_id'";
$run=mysql_query($query);
$numrow = mysql_num_rows($run); */
?>
<table border="2" >
<tr bgcolor="#33FFFF">
<th>SL</th>
<th>Question</th>
<th>Answer Mode</th>
<th>Answer Text</th>
<th>Condition Status</th>
<th>Edit</th>
<th>Delete</th>
<th></th></tr>
<?php
$campain=$_POST['campaign_id'];
$query=mysql_query("SELECT popup_question.*,popup_answar.*,answar_mode.*,GROUP_CONCAT(Answar_text SEPARATOR ',')Answar_text FROM popup_question, popup_answar, answar_mode
WHERE popup_question.Id = popup_answar.Question_id AND popup_question.Mod_id=answar_mode.id AND campaign_id='$campain' GROUP BY Question_text");
$count=1;
while($row = mysql_fetch_array( $query))
{
?>
<tr bgcolor="#D8D8D8 "><td><?php echo $row['Id']; ?></td>
<td><a href="popup_selection.php?campaign_id=<?php echo $campain; ?>" style="font-size:15px;font-family:normal;"><?php echo $row['Question_text']; ?></a></td>
<td style="font-size:15px;font-family:normal;"><?php echo $row['Mode']; ?></td>
<td style="font-size:15px;font-family:normal;"><?php echo $row['Answar_text']; ?></td>
<td style="font-size:15px;font-family:normal;"><input type="checkbox"></td>
<td><a href="edit_popup_question.php?id=<?php echo $row['Id']; ?>"><img src="images/edit-24.png"></a></td>
<td><input type="image" src="images/delete-24.png" value="Delete" onclick="del(<?php echo $row['Question_id'];?>)"></td>
<td><a href="edit_condition.php"><button type="button">Edit Condition</button></a></td>
</tr>
<?php
$count++;
}
?>
</table>
</p>
</div>
</div>
</div>
<!--Main Body Ends-->
</div>
</form>
<?php include('footer.php');?>
</body>
</html>
 

Answers (2)