I have 3 tables: list1, vegetables, list2 In pag1.php i am inserting values in "list1" table, from here i am carrying the last inserted id(fruitid) and carrying it to next form page2.php. in this page i have a list of items in form of checkbox is shown from "vegetables" table, from here the user can select multiple values that get saved in third table "list2". what i am trying to do is to carry fruit id from first page to last and insert it in the third table "list2". (e.g if the user selectes 3 values from the checkbox then those 3 values should get saved in 3 rows of "list2" table, each row having fruitid.
Table View What i have list1 table
id fruit
1 mango
2 apple
vegetables table
id vegetables
1 potato
2 Cauliflower
What i wish to get list2 table
id vegetableid fruitid
1 1 2
2 2 2
page1.php
<?php
ob_start();
include('co_session.php');
$con=mysqli_connect("localhost","root","pwd","root");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if($_POST){
// escape variables for security
$fruit = mysqli_real_escape_string($con, $_POST['fruit']);
$sql="INSERT INTO list1 (fruit) VALUES ('$fruit')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
$fruitid= mysqli_insert_id($con);
header("Location: page2.php?id=$fruitid ");
mysqli_close($con);
}
?>
<form class="form-horizontal" role="form" action="" enctype="multipart/form-data" method="post">
<div class="col-md-6">
<div class="form-group">
<label class="col-lg-4 control-label">Country</label>
<div class="col-lg-6">
<input class="form-control" value="" type="text" name="fruit">
</div>
</div>
</div>
<div class="col-md-11">
<div class="form-group">
<center><input class="btn btn-primary" value="Next " type="submit" name="submit"></center>
</div>
</div>
</form>
STEP TWO After the submission of first form it gets redirected to page2.php where it gets the id. e.g url is page2.php?id=1
<form class="form-horizontal" role="form" action="insert.php?id=<?php echo $_GET['fruitid'];?>" enctype="multipart/form-data" method="post">
<?php
$servername = "localhost";
$username = "root";
$password = "pwd";
$dbname = "root";
$con = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
echo "<label class='heading'>Select :</label></br>";
$sql = "SELECT * FROM vegetables";
$result = $con->query($sql);
while($row = $result->fetch_assoc()) {
echo " <input type='checkbox' name='vegetable[]' value='".$row['id']."'> '<label>' '".$row['vegetable']."' '</label>' <br>";
}
?>
</form>
STEP THREE This form gets redirected to insert.php page to store values
insert.php
<?php
ob_start();
include('co_session.php');
$con=mysqli_connect("localhost","root","pwd","root");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['submit']))
$fruitid = $_GET['fruitid'];
{
if(!empty($_POST['vegetable']))
{
$checked_count = count($_POST['vegetable']);
foreach($_POST['vegetable'] as $selected)
{
//echo "<p>".$selected ."</p>";
$sql="INSERT INTO list2 (vegetableid,fruitid) VALUES ('$selected','$fruitid')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
}
}
else
{
echo "<b>Please Select Atleast One Option.</b>";
}
}
?>
the problem is that till url of page2 i am getting id but after that i am not able to carry it forward in a proper manner