Fix code for adding multiple data rows inserting incorrect data

  mysql, php

An insert form is part populated from a database with the salesperson name. The other fields are completed manually. Which works fine. My first attempt to insert the data in the database only inserted one record. After some research I added a row counter, but it still inserts just one row of data and the data is incorrect. I would appreciate it if someone could nudge me in the right direction to solve the issue as I have spent a few days on this.

Form Code:

<form class="weeklySales" action="insertWeeklySales.php" method="post">
    <?php     
   $sql="SELECT salesperson FROM activity";
    $result = mysqli_query($conn, $sql);
    
    while($row = mysqli_fetch_array($result)){
        
        echo '<input required  type = "text" name = "salesperson" value="' . $row['salesperson'] . '" />'.'<input required  type = "text" class="smalltb" name = "appointments" placeholder="Appointments'  . '" />'.'<input required  type = "text" class="smalltb" name = "demos" placeholder="Demos'  . '" />'.'<input required  type = "text" class="smalltb" name = "orders" placeholder="Orders'  . '" />'.'<input required  type = "text" class="smalltb" name = "units" placeholder="Units'  . '" />'.'<input required  type = "text" class="smalltb" name = "profit" placeholder="Profit'  . '" />'."<br> ";
    }
    ?>
    <button type="submit" name="submit">Send Report</button>
   </form>

First attempt at code to insert the data, inputs just one line of data:

<?php 
    include_once'dbh.php';

$salesperson = mysqli_real_escape_string($conn,$_POST['salesperson']);
$appraisals = mysqli_real_escape_string($conn,$_POST['appointments']);
$demos =mysqli_real_escape_string($conn, $_POST['demos']);
$orders =mysqli_real_escape_string($conn, $_POST['orders']);
$units =mysqli_real_escape_string($conn, $_POST['units']);
$profit =mysqli_real_escape_string($conn, $_POST['profit']);
if (!empty($_POST)) {     
   $sql = "INSERT INTO weeklyreport(salesperson, appointments, demos, orders, units, profit) VALUES('$salesperson', '$ appointments ', '$demos','$orders','$units','$profit');";   
mysqli_query($conn, $sql);  
}
header("Location: prePopTest.php?insertData=success");
?>

2nd attempt at code to insert the data with a row counter: Inserts just one line of incorrect data. The incorrect data, is the first initial of the last salespersons name in the row of names (read J for Joe Smith) The other data columns are numerical data, they are inserted but to just one decimal place (read 2 for 23 or 1 for 12) :

<?php 
    include_once'dbh.php';
$count = count($_POST['salesperson']);
   for ($i=0; $i <$count; $i++) {  
$sql = "INSERT INTO weeklyreport(salesperson, appointments, demos, orders, units, profit) VALUES('{$_POST['salesperson'][$i]}', '{$_POST[' appointments '][$i]}', '{$_POST['demos'][$i]}','{$_POST['orders'][$i]}','{$_POST['units'][$i]}','{$_POST['profit'][$i]}')";   
mysqli_query($conn, $sql);  
   }
?>

Source: Ask PHP

LEAVE A COMMENT