在表opencart中插入带有一个请求的多个帖子

时间:2014-03-18 11:48:40

标签: php database post

尝试在数据库中插入多个记录的查询,但收到错误:

Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in

下面我附上代码类型和型号 请显示错误的位置或引擎的位置和方向

查看

<tr class="green_table">
                <td class="td">
                    <input type="text" name="forma[]" />
                     <?php if ($error_forma) { ?>
                        <span class="error"><?php echo $error_forma; ?></span>
                    <?php } ?>
                </td>
                <td class="td">
                    <input type="text" name="linkto[]" />

                </td>
                <td class="td">
                    <input type="text" name="description[]"  />
                     <?php if ($error_description) { ?>
                        <span class="error"><?php echo $error_description; ?></span>
                    <?php } ?>
                </td>
                <td class="td">
                    <input type="text" name="cvet[]" />
                     <?php if ($error_cvet) { ?>
                        <span class="error"><?php echo $error_cvet; ?></span>
                    <?php } ?>
                </td>
                <td class="td">
                    <input type="text" name="sizes[]" />
                     <?php if ($error_sizes) { ?>
                        <span class="error"><?php echo $error_sizes; ?></span>
                    <?php } ?>
                </td>
                <td class="td">
                    <input type="text" name="counts[]" />
                     <?php if ($error_counts) { ?>
                        <span class="error"><?php echo $error_counts; ?></span>
                    <?php } ?>
                </td>
                <td class="td">
                    <input type="text" name="tcena[]" />
                     <?php if ($error_tcena) { ?>
                        <span class="error"><?php echo $error_tcena; ?></span>
                    <?php } ?>
                </td>
            </tr>

模型:

foreach($data as $key => $value){
            $query = $this->db->query("INSERT INTO `" . DB_PREFIX 
            . "order` SET customer_id = '" . (int)$data['customer_id']
            ."',forma = '" . $this->db->escape($data['forma'])
            . "', linkto = '" .  $this->db->escape($data['linkto'])
            . "', description = '" .  $this->db->escape($data['description'])
            . "', cvet = '" .  $this->db->escape($data['cvet'])
            . "', sizes = '" .  $this->db->escape($data['sizes'])
            . "', counts = '" . (int)$data['counts']
            . "', tcena = '" . (int)$data['tcena']
            . "', sposob = '" .  $this->db->escape($data['sposob'])
            . "', delivery_usa = '" .  $this->db->escape($data['delivery_usa'])
            . "', hint = '" .  $this->db->escape($data['hint'])
            . "', novapochta = '" .  $this->db->escape($data['novapochta'])
            . "', customer_group_id = '" . (int)$data['customer_group_id'] 
            . "', firstname = '" . $this->db->escape($data['firstname'])
            . "', lastname = '" .  $this->db->escape($data['lastname'])
            . "', email = '" .  $this->db->escape($data['email'])
            . "', telephone = '" . $this->db->escape($data['telephone'])
            . "', date_added = '" . $this->db->escape(date('Y-m-d H:i:s'))
            ."', order_status_id =' 1"
            ."'");
            $new_order_id = $this->db->getLastId();
        }

谢谢!

2 个答案:

答案 0 :(得分:0)

您的表单字段是:forma[]linkto[]这样的数组,不能与$this->db->escape()一起使用。

请在循环echo$value变量foreach($data as $key => $value){并更新您的代码。

度过美好的一天!!

答案 1 :(得分:0)

这与OpenCart本身无关,它与基本PHP有关。您需要遍历所有已发布的值并一次插入一组值。

应该是这样的:

    $values_count = count($data['forma']);
    for ($i = 0; $i < $values_count; $i++) {
        $query = $this->db->query("INSERT INTO `" . DB_PREFIX . "order` SET"
        . " customer_id = " . (int)$data['customer_id']
        . ",forma = '" . $this->db->escape($data['forma'][$i])
        . "', linkto = '" .  $this->db->escape($data['linkto'][$i])
        . "', description = '" .  $this->db->escape($data['description'][$i])
        . "', cvet = '" .  $this->db->escape($data['cvet'][$i])
        . "', sizes = '" .  $this->db->escape($data['sizes'][$i])
        . "', counts = " . (int)$data['counts'][$i]
        . ", tcena = " . (int)$data['tcena'][$i]
        . ", sposob = '" .  $this->db->escape($data['sposob'][$i])
        . "', delivery_usa = '" .  $this->db->escape($data['delivery_usa'][$i])
        . "', hint = '" .  $this->db->escape($data['hint'][$i])
        . "', novapochta = '" .  $this->db->escape($data['novapochta'][$i])
        . "', customer_group_id = " . (int)$data['customer_group_id'] [$i]
        . ", firstname = '" . $this->db->escape($data['firstname'][$i])
        . "', lastname = '" .  $this->db->escape($data['lastname'][$i])
        . "', email = '" .  $this->db->escape($data['email'][$i])
        . "', telephone = '" . $this->db->escape($data['telephone'][$i])
        . "', date_added = NOW()"
        . ", order_status_id = 1");
        $new_order_id = $this->db->getLastId();
    }

当您将它们转换为'1'时,不需要使用(int)转义整数值。