如何在一个查询中将每个数组项更新到mysqli数据库中

时间:2017-11-06 13:31:39

标签: php arrays record

使用下面的代码我有多个记录的数组项目i用一个查询将每条记录更新到数据库中但是这里只更新了每个数组记录的最后一项是我的代码 :

<?php
    require("init.php");
    $sql = "SELECT item_name, quantity
    FROM books WHERE book = 1644445";

    $query = mysqli_query($conn, $sql);

    while($row = mysqli_fetch_array($query))
    {
        $da = $row["item_name"];
        $qty = $row["quantity"];
        $sql = mysqli_query($conn, "SELECT * FROM promo WHERE code = '$da' LIMIT 1");
        $productCount = mysqli_num_rows($sql);
        if($productCount > 0)
        {
            while ($row = mysqli_fetch_array($sql))
            {
                $id = $row["id"];
                $type = $row["name"];
                $code = $row["recharge"];
            }
        }
        $set="123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $coe=substr(str_shuffle($set), 0, 12);
        if(preg_match('/(65265)/i', $type))
            $type   = "20";
        if(preg_match('/(562546)/i', $type))
            $type   = "13";
        if(preg_match('/(MTN)/i', $type))
            $type   = "12";
        if(preg_match('/(56556)/i', $type))
            $type   = "16";
        $disp = str_split($code, $type);
        for($b = 0; $b<$qty; $b++){
            $pin = "$disp[$b]";
            $gam = array(0 => array("post" => $pin));
            foreach($gam as $gg)
            {
                $pp = $gg["post"];
                $go = mysqli_query($conn, "UPDATE promo SET recharge='$coe$pp' WHERE id=$id");
                if($go)
                {
                    echo "<br/> $pp";
                    echo "<br/> $coe";
                }
            }
        }
    }
?>

我申请你的影响

1 个答案:

答案 0 :(得分:0)

试试这个:

<?php
    require("init.php");
    $sql = "SELECT item_name, quantity
    FROM books WHERE book = 1644445";

    $query = mysqli_query($conn, $sql);

    while($row = mysqli_fetch_array($query))
    {
        $da = $row["item_name"];
        $qty = $row["quantity"];
        $sql = mysqli_query($conn, "SELECT * FROM promo WHERE code = '$da' LIMIT 1");
        $productCount = mysqli_num_rows($sql);
        if($productCount > 0)
        {
            while ($row = mysqli_fetch_array($sql))
            {
                $id = $row["id"];
                $type = $row["name"];
                $code = $row["recharge"];
            }

        $set="123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $coe=substr(str_shuffle($set), 0, 12);
        if(preg_match('/(65265)/i', $type))
            $type   = "20";
        if(preg_match('/(562546)/i', $type))
            $type   = "13";
        if(preg_match('/(MTN)/i', $type))
            $type   = "12";
        if(preg_match('/(56556)/i', $type))
            $type   = "16";
        $disp = str_split($code, $type);
        for($b = 0; $b<$qty; $b++){
            $pin = "$disp[$b]";
            $gam = array(0 => array("post" => $pin));
            foreach($gam as $gg)
            {
                $pp = $gg["post"];
                $go = mysqli_query($conn, "UPDATE promo SET recharge='$coe$pp' WHERE id=$id");
                if($go)
                {
                    echo "<br/> $pp";
                    echo "<br/> $coe";
                }
            }
        }
      }
    }
?>

仅当$ productCount&gt;执行更新时才会执行0