dongshou1991 2015-04-04 06:44
浏览 110

爆炸是从字符串中删除最后一个字符

I am making an ecommerce website. I have the following issue which I have came across while I was testing:

  1. Updating the order status (In Process, Shipping, Completed and Cancelled) from the admin panel is not working only for 1 product.
  2. Explode is deleting the last character when values are retrieved from the database.
  3. If there is more than 1 product in the order, order status works completely fine, though the point 2 remains the same.

So for example: Lets say that in my order (ORD-00001),

I have only 1 product (ABC-000001). So while exploding, I get the value as (ABC-00000).

In my next order (ORD-00002)

I have 2 product (ABC-000001, PQR-00005). So while exploding, I get the value as (ABC-000001, PQR-0000).

In the second order, the order status works completely fine. But in the first order, it doesn't work at all.

Here's the php button click event:

<?php

if (isset($_POST["btnUpdateOrderStatus"])) {

    $qu = "SELECT * FROM orders WHERE OrderCode = '".$_GET["ord"]."'";
    $result = $validate->Query($qu);
    if (!$result) {
        echo $validate->Error();
        exit();
    }
    while ($rowp = $validate->FetchAllDatas()) {
        $p = explode(', ', $rowp["ProdCode"]);

        foreach ($_POST["updateOrderPerProductStatus"] as $key => $value) {
            //echo $key;
            for ($i=0; $i < count($p); $i++) { 
                if( $p[$i] == $key) {
                    $querToUpdateOrderStatus = "UPDATE orders SET OrderStatus = '".implode(', ',$_POST["updateOrderPerProductStatus"])."' WHERE OrderCode = '".$_GET["ord"]."' ";

                    if ($validate->Query($querToUpdateOrderStatus)) {
                        header("Location: http://www.example.com/nbs/Administrators/Orders/Update.php?ord=".$_SESSION["ord"]."&id=".$_SESSION["id"]);
                        exit();
                    } else {
                        echo $validate->Error();
                        exit();
                    }
                }
            }
        }
    }
}

?>

Here's the code that I have used:

<?php
$query = "SELECT o.*, com.* FROM orders o, commission com WHERE o.OrderCode = '".$ord."'";

$validate->Query($query);

if ($validate->NumRows() >= 1) {
    while ($row = $validate->FetchAllDatas()) {

        $products = explode(',', $row['ProdCode']); // <-- error line
        $qnty = explode(', ', $row['Quantity']);
        $orderStats = explode(', ', $row["OrderStatus"]);
        $productsWithQunty = array_combine($products, $qnty);

        echo '
        <tr>
            <td style="width: 50%">Order Code: </td>
            <td style="width: 50%">'.$row["OrderCode"].'</td>
        </tr>
        <tr>
            <td style="width: 50%">Order Date: </td>
            <td style="width: 50%">'.$row["OrderDate"].'</td>
        </tr>
        <tr>
            <td style="width: 50%">Order Status: </td>
            <td style="width: 50%">'.$row["OrderStatus"].'</td>
        </tr>
        <tr>
            <td style="width: 50%">Order Modified: </td>
            <td style="width: 50%">'.$row["OrderModified"].'</td>
        </tr>
        <tr>
            <td style="width: 50%">Order Payable: </td>
            <td style="width: 50%">'.number_format($row["PayableAmount"], 2).'</td>
        </tr>
        <tr>
            <td style="width: 50%">Products: </td>
            <td style="width: 50%">';
            foreach ($productsWithQunty as $key => $value) {
                echo $key . ":" . $value;
            }
            echo '</td>
        </tr>';
        $arrProductStatus = array_combine($products, $orderStats);
        ?>
        <form action="Update.php?ord=<?php echo $ord; ?>" method="POST">
            <?php
            foreach ($arrProductStatus as $key => $value) {
                echo '
                <tr>
                    <td style="width: 50%">'.$key.'</td>
                    <td style="width: 50%">                                 
                        <select name="updateOrderPerProductStatus['.$key.']">
                            <option value="'.$value.'">'.$value.' -- selected</option>
                            <option value="In Process">In Process</option>
                            <option value="Cancelled">Cancelled</option>
                            <option value="Completed">Completed</option>
                            <option value="Shipping">Shipping</option>
                        </select>
                    </td>
                </tr>';
            }
            ?>
            <tr>
                <td colspan="2">
                    <input type="submit" name="btnUpdateOrderStatus" class="btn btn-primary btn-block" value="Submit">
                </td>
            </tr>
        </form>
        <?php
    }
}
?>

EDIT 1:

The product array: For order (ORD-00001)

Array
(
    [0] => ABC-000001
)

For order (ORD-00002)

Array
(
    [0] => ABC-000001
    [1] => PQR-00005
)

EDIT 2:

After RickJames Comment, here's what I get from the actual database.

Code:

echo $row["ProdCode"];
$products = explode(', ', $row['ProdCode']);
print_r($products);

Result for the above code:

DC-0001-0002, PK-0002-000 // The actual product code coming from DB

Array ( [0] => DC-0001-0002 [1] => PK-0002-000 )

And in the database I have:

OrderCode    CustomerId    ProductCode
ORD-000002       1      DC-0001-0002, PK-0002-0004

Please help me out. Thank you in advance.

  • 写回答

2条回答 默认 最新

  • dongying195959 2015-04-04 07:28
    关注
    $products = explode(',', $row['ProdCode']); // <-- error line
    

    Use white space after (,) needle.

    $products = explode(', ', $row['ProdCode']);
    

    Use this code

    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改