user3114394
user3114394

Reputation: 13

PDO how to insert multiple data to database from an array?

Hi guys after several trials and researching still im not able to do it.

i got this arrays:

<td><textarea name="item[]" value=""></textarea></td>
<td><textarea name="description[]" value=""></textarea></td>
<td><input type="text" name="qty[]" value="" /></td>
<td><input type="text" name="amount[]" value="" /></td>

and my methods are here:

public function insertRecord($param1, $param2, $param3, $param4, $args1, $args2, $args3, $args4) {

    $query = $this->db->prepare('INSERT INTO `request_to_purchase` (`requestedby`, `date`, `jobtitle`, `supervisor`, `notes`) VALUES (?, NOW(), ?, ?, ?)');

    $query->bindValue(1, $param1, PDO::PARAM_STR);
    $query->bindValue(2, $param2, PDO::PARAM_STR);
    $query->bindValue(3, $param3, PDO::PARAM_STR);
    $query->bindValue(4, $param4, PDO::PARAM_STR);

    try {

        $query->execute();

        $lastIDInserted = $this->db->lastInsertId();

        $row = $query->rowCount();

        if(count($row) > 0) {               
            $this->insertItem($lastIDInserted,$args1, $args2, $args3, $args4);
        } else {                
            return false;
        }

    } catch(PDOException $e) {

        die($e->getMessage());

    }

}

public function insertItem($arg1, $arg2, $arg3, $arg4, $arg5) {



    $query = $this->db->prepare('INSERT INTO `ordered_item` (`rtp_id`, `item`, `description`, `qty`, `amount`, `date`) VALUES (?, ?, ?, ?, ?, NOW())');

    $query->bindValue(1, $arg1);
    $query->bindValue(2, $arg2);
    $query->bindValue(3, $arg3);
    $query->bindValue(4, $arg4);
    $query->bindValue(5, $arg5);

    try {

        $query->execute();

        $row = $query->rowCount();

        if(count($row) > 0){
            return true;
        }else {
            return false;

        }
    } catch(PDOException $e) {
        die($e->getMessage());
    }

}

i used to have four loops that i believe is a wrong practice.

for($a = 0; $a < count($item); $a++ ) {

   $test1 = $item[$a];
}

for($b = 0; $b < count($description); $b++ ) {

  $test2 = $description[$b];
}

for($c = 0; $c < count($qty); $c++ ) {

  $test3 = $qty[$c];
}

for($d = 0; $d < count($amount); $d++ ) {

 $test4 = $amount[$d];
}

how do i insert those multiple datas on ordered_item table?

Upvotes: 1

Views: 132

Answers (2)

LouisK
LouisK

Reputation: 1176

To solve the problem of the blank insertion after the first value you should change the for loop to increment $i before it checks it against the limit, seems like it's comparing it the first time while $i is still set to 0, so try changing it to:

for ($i = 0; $i < $argsLen; ++$i)

Hope this helps.

Upvotes: 0

Nouphal.M
Nouphal.M

Reputation: 6344

Assume that $args1= $_POST['item'],$args2 = $_POST['description'],$args2 = $_POST['qty'],$args2 = $_POST['amount'] are array values then change the following part as below :

    if(count($row) > 0) {               
        $argsLen = sizeof($args1);
        for($i=0;$i<$argsLen;$i++){ //loop array and insert
          $this->insertItem($lastIDInserted,$args1[$i], $args2[$i], $args3[$i], $args4[$i]);
        }
    } else {                
        return false;
    }

I hope this helps you

Upvotes: 1

Related Questions