我的$_SESSION变量只获取数组中每个索引的第一个字母


My $_SESSION variables are only getting the first letter of each index in my array

我正在做一个类项目,我需要一些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。感谢您提供的任何建议或帮助。

尝试更改此项:

$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).

您需要将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将创建多个表单;数组中的每个项目各一个。

试试这个

    $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++;
    }