我想你想要這樣的:
<?php
session_start();
if($_SERVER["REQUEST_METHOD"]=="POST") {
$_SESSION["data"]=array_merge($_POST['myfruit_list'],$_SESSION["data"]);
$data = array();
$data = $_SESSION["data"];
$t=2;
echo "Number of Fruit Bowls: " . count($data)/2 . "<br />";
foreach($data as $selection => $name) {
if($t%2==0) {
echo "<br>";
echo "data : ".($t/2)." ";
}
else {
echo ",";
}
echo $name;
$t++;
}
}
else {
$_SESSION["data"]=[];
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<select name="myfruit_list[]">
<option value="apples">Apples</option>
<option value="oranges">Oranges</option>
</select>
<select name="myfruit_list[]">
<option value="grapes">Grapes</option>
<option value="pears">Pears</option>
</select>
<input type="submit" value="Confirm Order">
</form>
更新的要求......
<?php
session_start();
if($_SERVER["REQUEST_METHOD"]=="POST") {
if(isset($_POST["del"]))
{
$oft=$_POST["del"];
unset($_SESSION["data"][2*$oft-1]);
unset($_SESSION["data"][(2*$oft-2)]);
}
else
{
$_SESSION["data"]=array_merge($_POST['myfruit_list'],$_SESSION["data"]);
}
$t=2;
$data=$_SESSION["data"];
echo "Number of Fruit Bowls: " . count($data)/2 . "<br />";
foreach($data as $selection => $name) {
if($t%2==0) {
echo "<br>";
echo "data : ".($t/2)." ";
}
else {
echo ",";
}
echo $name;
$t++;
}
}
else {
$_SESSION["data"]=[];
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<select name="myfruit_list[]">
<option value="apples">Apples</option>
<option value="oranges">Oranges</option>
</select>
<select name="myfruit_list[]">
<option value="grapes">Grapes</option>
<option value="pears">Pears</option>
</select>
<input type="submit" value="Confirm Order">
</form>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<input type="text" placeholder="row number to be deleted..." name="del">
<input type="submit" value="Delete row">
</form>
ok..one更form..add此代碼相同的頁面.. 。
<form action="add.php" method="POST">
<input type="submit" value="add"/>
</form>
add.php ........
<?php
session_start();
if($_SERVER["REQUEST_METHOD"]=="POST")
{
[email protected]_connect("localhost","root","") or die("could not connect to database");
[email protected]_select_db("stack",$conn) or die("could not select database");
$data=$_SESSION["data"];
$temp="";$t=0;
foreach($data as $selection => $name) {
if($t%2==0) {
$temp=$name;
echo $temp;
}
else {
$id=rand(1000,9999);
$row=ceil(($t/2));
$sql="insert into st(id,row,fruit1,fruit2) values(\"$id\",\"$row\",\"$name\",\"$temp\")";
$res=mysql_query($sql,$conn) or die("could not execute ");
}
$t++;
}
}
?>
你的第一個php文件應該會給你帶來很多錯誤。 – Rizier123
'fruits.php'不是PHP。它是PHP標籤之間的HTML,如果您在啓用錯誤報告的情況下運行它,它將會失敗壯觀。 – 2015-07-19 01:34:25
對不起,第一個文件是一個帶有php擴展名的HTML文件。主要原因是我必須在HTML標籤之前使用session_start()和另一個包含文件(php)。我沒有直截了當地想。 – Harris