A
A
ART42020-02-13 11:33:12
PHP
ART4, 2020-02-13 11:33:12

Bulk adding records in mysqli?

Good afternoon, how to make an array in the form of having .
When you enter a new line in c - a new entry in the mysqli database.

In one word:
5e450805374ae262209322.jpeg

As I understand it, the array in the request goes:

INSERT INTO tbl_name (id, code, expire, type, value, min_order_price, single, usages) VALUES(1, 2, 3, 4, 5, 6, 7, 8, 9), (1, 2, 3, 4, 5, 6, 7, 8, 9), (1, 2, 3, 4, 5, 6, 7, 8, 9);


Worth and works separately fill out:
if($this->request->post("new_code")){
                $new_expire = $this->request->post('new_expire');
                $new_coupon = new stdClass();
                $new_coupon->id = $this->request->post('new_id', 'integer');
                $new_coupon->code = $this->request->post('new_code', 'string');
                if(!empty($new_expire)) {
                    $new_coupon->expire = date('Y-m-d', strtotime($new_expire));
                } else {
                    $new_coupon->expire = null;
                }
                $new_coupon->value = $this->request->post('new_value', 'float');
                $new_coupon->type = $this->request->post('new_type', 'string');
                $new_coupon->min_order_price = $this->request->post('new_min_order_price', 'float');
                $new_coupon->single = $this->request->post('new_single', 'float');

                // Не допустить одинаковые URL разделов.
                if(($a = $this->coupons->get_coupon((string)$new_coupon->code)) && $a->id != $new_coupon->id) {
                    $this->design->assign('message_error', 'code_exists');
                } elseif(empty($new_coupon->code)) {
                    $this->design->assign('message_error', 'empty_code');
                } else {
                    $new_coupon->id = $this->coupons->add_coupon($new_coupon);
                    $new_coupon = $this->coupons->get_coupon($new_coupon->id);
                    $this->design->assign('message_success', 'added');
                }
            }


Shipping itself:
public function add_coupon($coupon) {
        if(empty($coupon->single)) {
            $coupon->single = 0;
        }
        $query = $this->db->placehold("INSERT INTO __coupons SET ?%", $coupon);
        
        if(!$this->db->query($query)) {
            return false;
        } else {
            return $this->db->insert_id();
        }
    }


The table itself
5e4509bd5f6aa172477808.jpeg
5e4509c68a5ab854667665.jpeg

How can I only send an array?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2020-02-13
@FanatPHP

You do not have mysqli, but some kind of crooked division.
And I'll show you how to do it in mysqli

$data = [
    ['[email protected]', password_hash('password1', PASSWORD_DEFAULT)],
    ['[email protected]', password_hash('password2', PASSWORD_DEFAULT)],
];

$stmt = $mysqli->prepare("INSERT INTO users VALUES (null,?,?)");
$mysqli->begin_transaction();
foreach ($data as $row) {
    $stmt->bind_param("ss", ...$row);
    $stmt->execute();
}
$mysqli->commit();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question