我有一个页面,为每个游戏生成一个游戏列表和按钮,价值为#34;购买游戏"。 目标是在单击特定游戏的按钮时执行多个SQL查询。
访问',' consolename'等的合适方式是什么?对于来自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";
}
?>
答案 0 :(得分:1)
//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你完全知道,已经买了什么游戏。
答案 1 :(得分:1)
在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 ......
}