我正在开发一个类项目,我需要一些PHP的帮助。我正在尝试返回产品表,然后将有关产品的信息存储到数组中。然后我使用数组将信息存储到一些$ _SESSION变量中。我使用这些会话来填充一些表格。
现在会话正在返回空白。
让我知道是否有更好的方法来实现这一点。
session_start();
//DB login and info here
$dbc = mysqli_connect($host, $user, $password, $db)
or die('Unable to connect to Database. Process aborted');
$query = "SELECT * FROM product";
$result = mysqli_query($dbc, $query)
or die(mysqli_error($dbc));
// close dbc
mysqli_close($dbc);
if (mysqli_num_rows($result) == 0)
{
echo "ERROR: PRODUCTS NOT LOADED";
exit;
}
$row = mysqli_fetch_array($result);
$id [] = $row['Product_PK'];
$name [] = $row['Name'];
$desc [] = $row['Description'];
$price [] = $row['Price'];
$qty [] = $row['Quantity'];
for ($i = 0; $i < count($id); $i++)
{
$_SESSION['prod' . $i . '_ID'] = $id[$i];
$_SESSION['prod' . $i . '_Name'] = $name[$i];
$_SESSION['prod' . $i . '_Price'] = $price[$i];
$_SESSION['prod' . $i . '_Desc'] = $desc[$i];
$_SESSION['prod' . $i . '_Qty'] = $qty[$i];
}
我把信息放到这样的表格中:
<form method="post" action="" class="jcart">
<fieldset>
<input type="hidden" name="jcartToken" value="<?php echo $_SESSION['jcartToken'];?>" />
<input type="hidden" name="my-item-id" value="<?php echo $_SESSION['prod1_ID'];?>" />
<input type="hidden" name="my-item-name" value="<?php echo $_SESSION['prod1_Name'];?>" />
<input type="hidden" name="my-item-price" value="<?php echo $_SESSION['prod1_Price'];?>" />
<ul>
<li><strong><?php echo $_SESSION['prod1_Name'];?></strong></li>
<li>Price: $<?php echo $_SESSION['prod1_Price'];?></li>
<li>
<?php echo $_SESSION['prod1_Desc'];?>
</li>
<li>
Available: <?php echo $_SESSION['prod1_Qty'];?>
</li>
<label>Qty: <input type="text" name="my-item-qty" value="1" size="3" /></label>
</li>
</ul>
<input type="submit" name="my-add-button" value="add to cart" class="button" />
</fieldset>
</form>
我非常沮丧,只是想得到一些帮助。这件事几乎到期了,我不能浪费时间去学习PHP的所有细节。
编辑:对不起,如果我冒犯了任何人。我已经在这个项目上磨了大约2个月了,刚到我的php。感谢您提供的任何建议或您给予的任何帮助。答案 0 :(得分:2)
尝试更改此内容:
$row = mysqli_fetch_array($result);
$id [] = $row['Product_PK'];
$name [] = $row['Name'];
$desc [] = $row['Description'];
$price [] = $row['Price'];
$qty [] = $row['Quantity'];
对此:
// While $row is an array created by the result of the query...
while ($row = mysqli_fetch_array($result)) {
// The $id array continutes to have $row['Product_PK'] piled onto it.
$id[] = $row['Product_PK'];
// Same with the $name array, etc, etc.
$name[] = $row['Name'];
$desc[] = $row['Description'];
$price[] = $row['Price'];
$qty[] = $row['Quantity'];
}
// NOW start with your FOR statement. PHP will remember the values assigned during
// the WHILE loop, even though WHILE has ended (closing curly bracket).
答案 1 :(得分:1)
您需要将mysqli_fetch_array放入循环中。默认情况下,mysqli_fetch_array(及其前身mysql_fetch_array)仅从mysqli_query(和mysql_query)返回的对象中检索当前行。
while($row = mysqli_fetch_array($result)){
array_push($id, $row["Product_PK"];
array_push($name, $row["Name"];
array_push($desc, $row["Descripition"];
array_push($price, $row["Price"];
array_push($qty, $row["Quantity"];
}
当然,你必须首先定义你的数组,然后把它放在上面的while循环之前。
$id = $name = $desc = $price = $qty = array();
在PHP中断运行上面的操作之后,你最终会得到5个数组(不是5个常规变量)。这是我认为你使用的逻辑有点令人困惑的部分。在HTML标记上,您只有一个表单。所以问题是,您希望在表单中插入从数据库中返回的“产品”信息吗?
根据您目前所拥有的内容,您需要在整个表单周围包装PHP标记。以下是一个例子
<php?
for ($i = 0; $i < count($id); $i++):
$current_id = $_SESSION['prod' . $i . '_ID'] = $id[$i];
$current_name = $_SESSION['prod' . $i . '_Name'] = $name[$i];
$current_price = $_SESSION['prod' . $i . '_Price'] = $price[$i];
$current_desc = $_SESSION['prod' . $i . '_Desc'] = $desc[$i];
$current_qty = $_SESSION['prod' . $i . '_Qty'] = $qty[$i];
?>
<form method="post" action="" class="jcart">
<fieldset>
<input type="hidden" name="jcartToken" value="<?php echo $_SESSION['jcartToken'];?>" />
<input type="hidden" name="my-item-id" value="<?php echo $current_id;?>" />
<input type="hidden" name="my-item-name" value="<?php echo $current_name; ?>" />
<input type="hidden" name="my-item-price" value="<?php echo $current_price; ?>" />
<ul>
<li><strong><?php echo $current_name;?></strong></li>
<li>Price: $<?php echo $current_price;?></li>
<li>
<?php echo $current_desc;?>
</li>
<li>
Available: <?php echo $current_qty;?>
</li>
<label>Qty: <input type="text" name="my-item-qty" value="1" size="3" /></label>
</li>
</ul>
<input type="submit" name="my-add-button" value="add to cart" class="button" />
</fieldset>
</form>
<?php
endfor
?>
现在表单标记用PHP标记包装。 $ i将创建多个表格;数组中每个项目各占一个。
答案 2 :(得分:0)
试试这个
$i=0
while(rows = mysqli_fetch_array($result)) {
$_SESSION['prod' . $i . '_ID'] = $row['Product_PK'];;
$_SESSION['prod' . $i . '_Name'] = $row['Name'];
$_SESSION['prod' . $i . '_Price'] = $row['Price'];
$_SESSION['prod' . $i . '_Desc'] = $row['Description'];
$_SESSION['prod' . $i . '_Qty'] = $row['Quantity'];
$i++;
}