【问题标题】:My $_SESSION variables are only getting the first letter of each index in my array我的 $_SESSION 变量只获取数组中每个索引的第一个字母
【发布时间】:2013-11-30 04:35:08
【问题描述】:

我正在做一个课堂项目,我需要一些关于我的 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.ini 文件。感谢您提供的任何建议或提供的任何帮助。

【问题讨论】:

  • 那就雇个程序员吧,我们不是来做你不感兴趣的作业的。
  • 我很感兴趣我只是很沮丧。不应该这样说。我还有很多事情要做,只是想不通。
  • “这件事快到期了,我不能浪费时间去学习 PHP 的所有细节。”这就是任务的目的。花时间学习某事的来龙去脉。
  • @DrixsonOseña 我明白这段代码应该做得很好。我不确定如何正确地将我的 fetch_array 解析为多个数组,以便我可以正确访问它们。我认为没有必要侮辱我的智力。

标签: php mysql arrays database session


【解决方案1】:

尝试改变这个:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      试试这个

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

      【讨论】:

        猜你喜欢
        • 2013-02-25
        • 1970-01-01
        • 2012-04-30
        • 2018-06-06
        • 2023-01-23
        • 1970-01-01
        • 1970-01-01
        • 2017-09-05
        • 1970-01-01
        相关资源
        最近更新 更多