javascript发送变量但插入数组php无法正常工作

标签 javascript php jquery mysql

我对这段代码有疑问,在 INSERT 中只在每个插入中插入第一个 item_price,例如我有三个不同的 item_price,谢谢你的帮助抱歉英语不好

表格中的html部分:

<table>
<tr id="shop1">
<td>1</td> <!-- item_id -->
<td><input type="text" value="33" name="price"></td> <!-- item_price -->
</tr>
<tr id="shop2">
<td>2</td> <!-- item_id -->
<td><input type="text" value="11" name="price"></td> <!-- item_price -->
</tr>
<tr id="shop3">
<td>3</td> <!-- item_id -->
<td><input type="text" value="65" name="price"></td> <!-- item_price -->
</tr>
</table>
<button id="checkoutinventory" class="btn-danger"></button>

javascript部分:

$(document).on("click","button#checkoutinventory, a[href$='checkoutinventory']",function()
    {
        var items = "";
        var price = "";

        $("tr[id^=shop] button.btn-danger").each(function()
        {
            items += $(this).parents("tr").attr("id").replace(/\D/g,'')+",";
            price += $(this).parents("tr").find('input[name="price"]').val().replace(/\D/g,'')+","; //this code send price in this format: 1,2,3,etc
        });

        if (!items)
            return displayResponse("ERROR","an error ocurred try again.");

        var settings = {};
        settings['action'] = "checkoutinventory";
        settings['items'] = items;
        settings['price'] = price;

        $.get("includes/process.php", settings, function(data)
        {
            processResponse(data);
        });

        return false;
    });

process.php部分:

if ($_GET['action'] == "checkoutinventory")
    {
        if (!@$character)
            exit("REFRESH");

        if (!$VALIDATE->blank(@$_GET['items']))
            exit();

        $items = explode(",", rtrim($_GET['items'],",")); //Here obtain item_id in this format: id_1,id_2,id_3,etc
        $price = $_GET['price']; //Here obtain item_price in this format: price1,price2,price3,etc

        $isonline = $SERVER->select($query['characterIsonline'],array($COOKIE->get("character")));

        if(!$isonline)
        {
            $itemData = $SERVER->select("SELECT * FROM `items` WHERE `object_id` IN (".implode(",",$items).");",array()); //Here obtain perfectly information for insert later in foreach
        } else {
            exit("RESPONSE^ERROR^Your character <b>".$character['char_name']."</b> is online disconnect please and try again..");
        }
        if($itemData) {
            foreach($itemData as $itemlist)
                {
                $selling = $SERVER->insert("INSERT INTO `shop_items` (`itemId`,`itemAmount`,`itemPrice`,`itemEnchant`,`vendedor`,`object_id`) VALUES (?,?,?,?,?,?);",array($itemlist['item_id'],$itemlist['count'],$_GET['price'],$itemlist['enchant_level'],$character['char_name'],$itemlist['object_id']));
            }
        }

        if (count($items) != count($itemData))
            exit("RESPONSE^ERROR^One of the items is no longer available for sale.");

        if($selling) {
            exit("RESPONSE^SUCCESS^Congrats <b>".$character['char_name']."</b>, you sell ".count($items)." item(s)!");
            }

        exit("RESPONSE^ERROR^Unexpected error occured.");
    }

最佳答案

这假设您希望按位置将价格与商品匹配(即价格数组中的第一个值属于商品数组中的第一个值)

if($itemData) {
    $prices = explode(',',$price); //create an array of prices

    foreach($itemData as $itemlist) {

        $single_price = array_shift($prices); //remove the 1st price from array

        $selling = $SERVER->insert("INSERT INTO `shop_items` 
            (`itemId`,`itemAmount`,`itemPrice`,`itemEnchant`,`vendedor`,`object_id`) 
            VALUES (?,?,?,?,?,?);",
            array($itemlist['item_id'],$itemlist['count'],$single_price,$itemlist['enchant_level'],$character['char_name'],$itemlist['object_id']));
    }
}

关于javascript发送变量但插入数组php无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27762824/

相关文章:

javascript - JQuery Range Slider 如何让多个标 checkout 现?

javascript - 尝试创建谷歌通知,未捕获的 TypeError 错误

javascript - vue.js VeeValidate - 自定义验证器正确编译错误但不切换错误类

javascript - 同时分配和检查并有一个很好的语法错误

php - PHP 中的 cURL : curl_exec() or exec ('curl' )?

php - 关于在PHP中嵌入HTML的一个问题

php - 扩展用于发布到页面的 Facebook Open Graph token

php - 使用 2 个查询的 Ajax PHP 更新

javascript - 如果字符串的任何变体在 URL 中匹配,则返回 true

javascript - 对可排序列表进行排序