【发布时间】:2016-10-30 15:31:37
【问题描述】:
我正在尝试将输入到一个 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>';}
?>
【问题讨论】:
-
您需要一些存储空间。可能是sessions。
-
您可以使用不同的获取参数:
?p1=param1&p2=param2&p3=param3等等,稍后您可以使用$_GET['p1']等等来接收它们......所以基本上不要使用?submit,你应该做?action =submit 另外,如果您使用带有 POST 方法的表单,您可以禁用选项以刷新带有 URL 的页面 100 次,在此处阅读有关表单的信息:w3schools.com/tags/tag_form.asp -
是的,所有这些都发生在会话中 - 用户登录后,将位置添加到他们的行程中,然后将序列号添加到该行程中的每个位置,然后将其保存到数据库。跨度>
-
不知道你为什么把这个复杂化了。就像@MaanusIndov 所说的那样,它看起来像是 html
form的完美案例。