rocky a
rocky a

Reputation: 144

MySQL Insert only last row with while loop

I have a while loop. It is within a form.I want to insert all rows from while loop when submitting submit button. Below is the code of while loop.

<form method="POST" action="insert.php" n>
  <table>
                    <?php                           
                     $i =0;
                     $link = mysqli_connect('localhost', 'root', '', 'shibu');
                     $sql = "select `cus_id`, `mobile`, `date`, `time`,
                            sum(`d1` + `d2` + `d3`) as `credit`,
                            from `finale`;
                        $result=mysqli_query($link, $sql);
                        $count = mysqli_num_rows($result);
                        if($count == 0){
                        ?>
                        <tr> 
                        <th><?php echo 'No Records Founds!' .mysqli_error($link); ?></th> 
                        </tr>
                        <?php 
                        }else{
                            while($sql_result = mysqli_fetch_assoc($result)){
                        ?>
<tr>
<td><?php echo $i += 1; ?></td>
<td><input type="text" name="cus_id" value="<?php echo $sql_result['cus_id']; ?>" ></td>
<td><input type="text" name="mobile" value="<?php echo $sql_result['mobile']; ?>" ></td>
<td><input type="text" name="date" value="<?php echo $sql_result['date']; ?>" ></td>
<td><input type="text" name="time" value="<?php echo $sql_result['time']; ?>"</td>
<td><input type="text" name="credit" value="<?php echo $sql_result['credit']; ?>"></td>
</tr>
    <?php
    }
    }
    ?>
 </table>
<input name="submit" type="submit"  />
</form>

This is my insert query.

<?php

$link = mysqli_connect('localhost', 'root', '', 'shibu');

$stmt = mysqli_prepare($link, "INSERT INTO single 
                 (`cus_id`, `mobile`, `date`, `time`, `credit`) VALUES (?,?,?,?,?)");

mysqli_stmt_bind_param($stmt, 'isssi', $cus_id, $mobile, $date, $time, $credit);

foreach ((array)$_POST['cus_id'] as $i => $cus_id) {
                $mobile = $_POST['mobile'];
                $date = $_POST['date'];
                $time = $_POST['time'];
                $credit  = $_POST['credit'];

mysqli_stmt_execute($stmt);
                }

if(!$stmt){ 
echo "error". mysqli_error($link);
}

My problem is when I enter submit button, only last row enters not the whole rows.

I need your help heartily.

Upvotes: 2

Views: 931

Answers (2)

paulz
paulz

Reputation: 313

Please move your

mysqli_stmt_bind_param

line into foreach loop, just above

mysqli_stmt_execute

line.

Upvotes: 0

Ravinder Reddy
Ravinder Reddy

Reputation: 3879

In your html code you need to have input names defined as array.

Like

<td><input type="text" name="cus_id[]" value="<?php echo $sql_result['cus_id']; ?>" ></td>
<td><input type="text" name="mobile[]" value="<?php echo $sql_result['mobile']; ?>" ></td>
<td><input type="text" name="date[]" value="<?php echo $sql_result['date']; ?>" ></td>
<td><input type="text" name="time[]" value="<?php echo $sql_result['time']; ?>"</td>
<td><input type="text" name="credit[]" value="<?php echo $sql_result['credit']; ?>"></td>

And while using them in php you need to loop them and run insert query

foreach ($_POST['cus_id'] as $i => $value) {
    $cus_id = $_POST['cus_id'][$i];
    $mobile = $_POST['mobile'][$i];
    $date = $_POST['date'][$i];
    $time = $_POST['time'][$i];
    $credit  = $_POST['credit'][$i];

    // Yourinsert query
}

Upvotes: 3

Related Questions