将值从一个PHP文件传递到另一个PHP文件

时间:2016-06-28 10:40:38

标签: php sql

我试图将输入到一个Php页面(itinerary.php)上的框中的值传递到另一个Php页面(' submit.php')所以它可以从那里,保存到数据库中。但我无法弄清楚如何解决这个问题。我已尝试使用GET,您可以从下面的代码中看到,但我已经使用GET语句来接收并确认来自同一页面的其他值'提交'。我想我过于复杂了,但是我对Php的了解在这个阶段仍然非常有限,所以任何想法都会受到赞赏!

这是itinerary.php文件的摘录(它位于Bootstrap / Html框架内。注意包含序列号输入框的条目)。

<h3><br>YOUR ITINERARY</h3>

<?php

//Display contents of itinerary
    if(!empty($_SESSION['itinerary'])){

        //Retrieve details of each location in array from database
        $query = "SELECT * FROM locations WHERE loc_id IN (";
        foreach ($_SESSION['itinerary'] as $loc_id=>$value)
        {$query.=$loc_id.',';}
        $query = substr($query, 0, -1).')ORDER BY loc_id ASC';
        $result = mysqli_query($db, $query);

        echo'<table><tr><th colspan="5">LOCATIONS IN YOUR ITINERARY</th></tr>';

    //Display locations in array

    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){

    $loc_id = $row['loc_id'];

        echo"<br><td>{$row['loc_name']}</td></tr><br>
        <td>{$row['loc_desc']}</td>
        <td><p>Seq. No</p><input type=\"text\" size=\"3\"  name=\"sequence\" value=????????>NOT SURE WHAT TO ADD INTO THIS LINE TO RETAIN THE INFO THAT IS INPUT</td>
        <td><a href=remove_loc.php?value=$loc_id>Remove location</a><br></br></td>
    </tr><br></table>";
    }//while
    print_r($_SESSION);

    mysqli_close($db);

    }//if

    else {echo '<p><br>Your itinerary is empty.<br></p>';}

echo '<br><p>

<a href=submit.php?submit>Save itinerary</a>
<a href=clear_itin.php?clear>Clear itinerary</a>
<a href="user_home.php">Your details</a>
<a href="logout.php">Logout</a>
</p>';


?>

这是我尝试接收它然后在SQL命令中使用它添加到数据库的地方。 (同样,这是在Bootstrap框架内)您可以忽略第一个SQL Insert语句,因为它正在成功传递其他信息..

<div class="row">
        <div class="col-md-9">

<?php

if (isset($_GET['sequence'])){
    $sequence = $_GET['sequence'];

}//if

    if (isset($_GET['submit'])){

        $query = "INSERT INTO itineraries (user_id, date_created) VALUES (".$_SESSION['user_id'].", NOW())";
        $result = mysqli_query($db, $query);

        $itinerary_id = mysqli_insert_id($db);

        $retrieve_locs = "SELECT * FROM locations WHERE loc_id IN (";
        foreach ($_SESSION['itinerary'] as $id=>$value)
        {$retrieve_locs.=$id.',';}
        $retrieve_locs = substr($retrieve_locs, 0, -1).')ORDER BY loc_id ASC';
        $result = mysqli_query($db, $retrieve_locs);

        //Store items in itin_locs db
        while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC)){

            //This is the command I have been trying to use, commented out. The second one below this works fine, but obv doesn't input a sequence number.

            //$insert_locs = "INSERT INTO itin_loc (itinerary_id, loc_id, sequenceNo) VALUES ($itinerary_id, ".$row['loc_id'].", $sequence)";

            $insert_locs = "INSERT INTO itin_loc (itinerary_id, loc_id) VALUES ($itinerary_id, ".$row['loc_id'].")";
            $insert_result = mysqli_query($db, $insert_locs);

            echo mysqli_error($db);

            if ($insert_result === FALSE) {
                die("Query failed!" . mysql_error() . $insert_locs);}


        }//while


        mysqli_close($db);

        echo"<p>Your itinerary is saved!. Itinerary number is #".$itinerary_id."</p><br>";
        $_SESSION['itinerary']= NULL;

        echo '<p>

<a href="user_home.php">Your details</a>
<a href="logout.php">Logout</a>
</p>';

    }//if


    else {echo '<p>Your itinerary is empty.<br></br></p>';}

?>

1 个答案:

答案 0 :(得分:1)

使用HTML页面上的表单来保存表格中的所有输入字段。 而不是锚&#34; a&#34;标签使用提交按钮将表单提交到其他页面。 使用类似的东西:

Send value of submit button when form gets posted