duancong7358 2014-11-17 18:29
浏览 23
已采纳

将id从一个表单传递到另一个表单并插入另一个表

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

  • 写回答

1条回答 默认 最新

  • douzhi4991 2014-11-17 19:04
    关注

    The form on page2.php should have its action value action="insert.php?id=<?php echo $_GET['id'];?>"

    And in insert.php $fruitid = $_GET['id'];

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 socket通信实现多人聊天室疑惑
  • ¥15 DEV-C++编译缺失
  • ¥33 找熟练码农写段Pyhthon程序
  • ¥100 怎么让数据库字段自动更新
  • ¥15 antv g6 力导向图布局
  • ¥15 quartz框架,No record found for selection of Trigger with key
  • ¥15 锅炉建模+优化算法,遗传算法优化锅炉燃烧模型,ls-svm会搞,后面的智能算法不会
  • ¥20 MATLAB多目标优化问题求解
  • ¥15 windows2003服务器按你VPN教程设置后,本地win10如何连接?
  • ¥15 求一阶微分方程的幂级数