我正在尝试将一些cd名称从一个SQL数据库打印到一个数组中,这个数据库之前我已经选择了但是到处都得到了相同的结果。你能帮助我吗?我试图在第二个PHP的代码中的不同位置添加[$ x]但是得到了相同的错误。
我只从数据库中获取第一个选择的cd名称,其中username = loggein_user。
require 'connectmysql.php';
//COUNT how many cart history has each user
mysql_select_db('shop');
$data = mysql_query("SELECT COUNT(Username) AS num FROM cart WHERE Username = '{$_SESSION['Username']}' ") or die(mysql_error());
$row2 = mysql_fetch_assoc($data);
$numUsers = $row2['num'];
$_SESSION['count_cart_history'] = $numUsers;
mysql_close($con);
问题出现在for循环之下,但我无法找到它。我试图改变它,如$ result = ....或$ row3 = ....没有[$ x]但没有。
for($x=0;$x<=$_SESSION['count_cart_history']; $x++)
{
$selecthistory[$x] = "SELECT * FROM cart WHERE Username = '{$_SESSION['Username']}'";
$result[$x] = mysql_query($selecthistory[$x], $con);
$row3[$x] = mysql_fetch_assoc($result[$x]);
// $_SESSION['historymarket_id'][$x] = $row3[$x]["Market_ID"];
$_SESSION['historycd_name'][$x] = $row3[$x]["CD_NAME"];
// $_SESSION['historydate_sold'][$x] = $row3[$x]["Date_Sold"];
// $_SESSION['historyprice'][$x] = $row3[$x];["Price"];
// $_SESSION['historytotalprice'][$x] = $row3[$x]["TotalPrice"];
// $_SESSION['historyquantity'][$x] = $row3[$x]["Quantity"];
// $_SESSION['historycredit'][$x] = $row3[$x]["Credit"];
}
echo'edw';
echo $_SESSION['historycd_name'][0];
echo $_SESSION['historycd_name'][1];
echo $_SESSION['historycd_name'][2];
for($x=0;$x<=$_SESSION['count_cart_history']; $x++)
{
?>
<p>
Market_ID :
<input name="market_id" type="text" readonly value=" <?php //echo $_SESSION['historymarket_id'][$x]; ?>">
</p>
<p>
CD Name :
<input name="CD_NAME" type="text" readonly value="<?php echo $_SESSION['historycd_name'][$x]; ?>">
</p>
<p>
Date Sold :
<input name="Date_Sold" type="text" readonly value="<?php //echo $_SESSION['historydate_sold'][$x]; ?>">
</p>
<p>
Price:
<input name="price" type="text" readonly value="<?php //echo $_SESSION['historyprice'][$x]; ?>">
</p>
<p>
Total Price:
<input name="totalprice" type="text" readonly value="<?php //echo $_SESSION['historytotalprice'][$x]; ?>">
</p>
<p>Credit Card:
<input name="Credit" type="text" readonly value="<?php //echo $_SESSION['historycredit'][$x]; ?>">
</p>
<p>Quantity:
<input name="quantity" type="text" readonly value="<?php //echo $_SESSION['historyquantity'][$x]; ?>">
</p>
<p>PostAddress:
<input name="PostAddress" type="text" readonly value="">
</p>
<?php } mysql_close($con);?>
答案 0 :(得分:0)
您在每个循环上执行相同的SELECT
查询,因此您存储的所有项目都是相同的。
您可以尝试这样的事情:
$x = 0;
$selecthistory = "SELECT * FROM cart WHERE Username = '{$_SESSION['Username']}'";
$result = mysql_query($selecthistory[$x], $con);
// loop through all the items from the cart
// this will loop $_SESSION['count_cart_history'] times as long as the count hasn't changed
while($result && $row3 = mysql_fetch_assoc($result)){
// $_SESSION['historymarket_id'][$x] = $row3["Market_ID"];
$_SESSION['historycd_name'][$x] = $row3["CD_NAME"];
// $_SESSION['historydate_sold'][$x] = $row3["Date_Sold"];
// $_SESSION['historyprice'][$x] = $row3["Price"];
// $_SESSION['historytotalprice'][$x] = $row3["TotalPrice"];
// $_SESSION['historyquantity'][$x] = $row3["Quantity"];
// $_SESSION['historycredit'][$x] = $row3["Credit"];
$x++;
}
建议您不要使用mysql_
个功能;而是升级到mysqli_
功能。在查询中使用它之前,您还应该转义$_SESSION['Username']
。