我有一个页面,它为每个游戏生成一个游戏列表和按钮,值为"购买游戏"。目标是在为特定游戏单击按钮时执行多个SQL查询。
访问"名称"、"集合名称"等的适当方式是什么。对于ARRAY中的每个游戏,并在另一个页面purchase.php中对其执行查询?
注意:我确实考虑过使用会话,但当我将变量$index作为SESSION变量传递时,我始终只得到最终值。
buygame.php
<?php
session_start();
//connect to db
dbConnect("root", "") ;
dbSelect("webdesign");
//SEARCH FOR GAMES
print "<h3>Games to Buy: </h3>";
$query = "SELECT gamecode,name, consolename,price, points, genre from game";
$result = runQuery($query);
while($row = mysql_fetch_array($result))
{
$array[] = $row;
}
$index =0 ;
//DISPLAYING A LIST OF GAMES
while($index<sizeof($array))
{
echo $array[$index]['name'];
echo $array[$index]['consolename'];
echo $array[$index]['genre'];
echo ("Price is: ".$array[$index]['price']." USD");
echo ("Loyalty Points: ".$array[$index]['points']);
//THE BUTTON "BUY GAME"
echo("<a href='purchase.php'><input type='submit' name='submit[$index]' value='Buy Game'/></a>");
$index = $index + 1;
}
?>
purchase.php
<?php
session_start();
//connect to database
dbConnect("root", "") ;
dbSelect("webdesign");
$index = 0;
echo "Button clicked ";
//I am trying this but I'm sure that this is not right
if (isset($_POST['submit[$index'])){
print "$index was clicked";
}
?>
在buygame.php
中将代码更改为:
//THE BUTTON "BUY GAME"
echo("<a href='purchase.php?gamecode={$array[$index]['gamecode']}'><input type='submit' name='submit[$index]' value='Buy Game'/></a>");
在purchase.php
中
<?php
if(isset($_GET['gamecode'])) {
echo $_GET['gamecode'];
//and make ......
}
//THE BUTTON "BUY GAME"
$gamecode = $array[$index]['gamecode'];
echo("<a href='purchase.php?gamecode=$gamecode'><input type='submit' name='submit[$index]' value='Buy Game'/></a>");
$index = $index + 1;
然后在purchase.php中,你确切地知道,买了什么游戏。