Jonathan
Jonathan

Reputation: 13

Insert each data different rows PDO

I have 10 inputs with the same name, and i'm trying to insert them in each rows but i'm not sure how should i do that, i searched on google to see if there's any answers but failed.

i hope i explained clearly.

My codes are below:

TQ

I loop my inputs with PHP

for($x = 0; $x <= 10; $x++){
    print '
    <tr>
                <td><input class="form-mod" type="text" name="sn[]"/></td>
                <td><input class="form-mod" type="text" name="ename[]"/></td>
                <td><input class="form-mod" type="text" name="hrs[]"/></td>
                <td><input class="form-mod" type="text" name="sales[]"/></td>
                <td><input class="form-mod" type="text" name="wages[]"/></td>
                <td ><input class="form-mod" type="text" name="inc[]"/></td>

            </tr>
    ';
}

PHP CODE

    if(isset($_POST['submited'])){
    $rows = array( array($_POST['pc'],$_POST['wn'],$_POST['from'],$_POST['to'],$_POST['sn'],$_POST['ename'],$_POST['hrs'],$_POST['sales'],$_POST['wages'],$_POST['inc'] )


 );

    $args = array_fill(0, count($rows[0]), '?');

    $query = "INSERT INTO wages (program_code,week_no,`from`,`to`,serial_no,name,hrs,sales,wages,incentive) VALUES (".implode(',', $args).")";
    $stmt = $dbo->prepare($query);

    foreach ($rows as $row) 
    {
       $stmt->execute($row);
    } 


    if(!$stmt->rowCount() >= '1'){
        print "unsuccessfully insert";
    }else{
        print "successfully inserted";
    }
}

UPDATED

i have revised the code that @Vitor Lima type. i remove this code ""implode(',', $row)"" and replace it with ?,?,?,? and it worked. but if i enter only 2 rows with values in the input page it will still insert the remaining 8 to the database, so 2 rows with values and 8 are empty. any idea how to overcome that

if(isset($_POST['submited'])){
    for($x = 0; $x < 10; $x++){
        $row = array($_POST['pc'], $_POST['wn'], $_POST['from'], $_POST['to'],
                     $_POST['sn'][$x], $_POST['ename'][$x], $_POST['hrs'][$x],
                     $_POST['sales'][$x], $_POST['wages'][$x], $_POST['inc'][$x]);

        $query = "INSERT INTO wages (program_code, week_no,`from`,`to`,serial_no, 
               name,hrs,sales,wages,incentive) VALUES (?,?,?,?,?,?,?,?,?,?)";
        $stmt = $dbo->prepare($query);
        $stmt->execute($row);

        if(!$stmt->rowCount() >= '1'){
            print "unsuccessfully insert";
        }else{
            print "successfully inserted";
        }
    }
}

Upvotes: 1

Views: 80

Answers (1)

Look for your page: the input's names have [], it means they will be passed as an array to the server, so, for example, $_POST['sn'] will be an array with ten positions (you can use var_dump to certify...)

So, in your PHP script, you should make a loop, something like that

if(isset($_POST['submited'])){

    $stmt = $dbo->prepare(
        "INSERT INTO wages
         (program_code, week_no,`from`,`to`,serial_no, name, hrs, sales, wages, incentive)
         VALUES (?,?,?,?,?,?,?,?,?,?)"
        );

    for($x = 0; $x < 10; $x++){
        $row = array($_POST['pc'], $_POST['wn'], $_POST['from'], $_POST['to'],
                     $_POST['sn'][$x], $_POST['ename'][$x], $_POST['hrs'][$x],
                     $_POST['sales'][$x], $_POST['wages'][$x], $_POST['inc'][$x]);
   
        $stmt->execute($row);
    
        if(!$stmt->rowCount() >= '1'){
            print "unsuccessfully insert";
        }else{
            print "successfully inserted";
        }
    }
}

Upvotes: 1

Related Questions