1

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?

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

hi nouphal!wow! its working!your awesome!but the problem is when insertion happens into the database, i input two values for the four fields so it gave me like item1 descp1 qty1 amount1 and blank item blank desc blank qty blank amount and item2 desc2 qty2 amount2 >>>>as you can see it inserted the two datas but it gave me a blank data, so it inserted 3 datas, item1 blank item and item3
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.