1

Good day. I just wanna ask how can I insert the data from the forms generated from a while loop. This is what I tried so far. I added a loop where they will have different id or names but when I tried on clicking the button the first form is the only one working. Thank you so much in advance.

<?php
include "../config/dbconfig.php";

$data['productCode'] = "1"; // sample data
$stmt = $conn->prepare("SELECT * FROM tbl_category");
//$stmt->bind_param("i", $data['productCode']);
$stmt->execute();
$result = $stmt->get_result();
$i = 1;
while ($stuff = $result->fetch_assoc()) {
?>
    <div class="col-sm-6" style="margin-top:20px;">
        <div class="card">
            <div class="card-header"><?php echo $stuff['categoryname']; ?>
            </div>
            <div class="card-body outermydiv">
                <div class="myDIV">
                    <form method="POST" name="itemform" action="">
                        <div class="form-row">
                            <div class="col-5">
                                <input type="text" class="form-control" name="name[<?php echo $i; ?>]" id="itemname[<?php echo $i; ?>]" placeholder="Item name" required autocomplete="off">
                            </div>
                            <div class="col">
                                <input type="number" class="form-control" name="cost[<?php echo $i; ?>]" id="itemcost[<?php echo $i; ?>]" placeholder="Cost" required>
                            </div>
                            <div class="col">
                                <input type="number" class="form-control" name="price[<?php echo $i; ?>]" id="itemprice[<?php echo $i; ?>]" placeholder="Price" required>
                                <input type="hidden" class="form-control" name="code[<?php echo $i; ?>]" id="forcatcode[<?php echo $i; ?>]" value="<?php echo $stuff['categorycode'] ?>">
                            </div>
                            <div class="col">
                                <button type="submit" class="btn btn-success" name="btnsaveitem" id="btnsaveitem">Save</button>
                            </div>
                            <?php $i++; ?>
                            <input type="hidden" name="count" value="<?php echo $i; ?>" />
                        </div>
                    </form>

                </div>
                <br>

            </div>
        </div>
    </div>
<?php
}
?>

and here is my insert code

<?php
    if (isset($_POST['btnsaveitem'])) {
      $count = $_POST['count'];
      for ($i = 1; $i < $count; $i++) {
          //$code = $_POST['code'][$i]; // check empty and check if interger
          $foritemname = $_POST['name'][$i]; // check empty and strip tags
          //$qty = $_POST['qty'][$i]; // check empty and check if interger
          $stmt = $conn->prepare("INSERT INTO tbl_items(`itemname`) VALUES ('".$foritemname."')");
          //$stmt->bind_param("iss",$name);
          $stmt->execute();
      }
  }
?>
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
jackfrost
  • 15
  • 6
  • 1
    Your insert query is insecure/unstable because you are not using a prepared statement. Why not include some diagnostic findings in your question? What is the submission data when it misbehaves? – mickmackusa Jul 04 '20 at 10:37
  • thank you for replying sir mickmackusa. When I tried sending the data it just refresh and upon checking on the database it doesn't insert and only the first button works. – jackfrost Jul 04 '20 at 10:45
  • What errors are you seeing in your error logs? Much of this code should be rewritten. – mickmackusa Jul 04 '20 at 10:50
  • I have been refreshing the error logs sir but it doesn't show anything sir. – jackfrost Jul 04 '20 at 10:55

1 Answers1

0

Starting by rewriting your form...

  1. You don't need $i for anything, but I'll leave the declaration in case you want it for something else.
  2. Don't submit array type data, each form will submit its own set of fields.
  3. It probably makes more sense to add $stuff['categorycode'] as the value of each submit to avoid needing the hidden field. I'll leave it your way for now.

Form:

foreach ($stmt->get_result() as $i => $stuff) { ?>
    <div class="col-sm-6" style="margin-top:20px;">
        <div class="card">
            <div class="card-header"><?php echo $stuff['categoryname']; ?></div>
            <div class="card-body outermydiv">
                <div class="myDIV">
                    <form method="POST">
                        <div class="form-row">
                            <div class="col-5">
                                <input type="text" class="form-control" name="name" placeholder="Item name" required autocomplete="off">
                            </div>
                            <div class="col">
                                <input type="number" class="form-control" name="cost" placeholder="Cost" required>
                            </div>
                            <div class="col">
                                <input type="number" class="form-control" name="price" placeholder="Price" required>
                            </div>
                            <div class="col">
                                <button type="submit" class="btn btn-success" name="btnsaveitem">Save</button>
                            </div>
                        </div>
                        <input type="hidden" class="form-control" name="code" value="<?php echo $stuff['categorycode']; ?>">
                    </form>
                </div>
                <br>
            </div>
        </div>
    </div>
    <?php
}        

Receiving script: (extend with additional fields as desired)

if (isset($_POST['btnsaveitem'])) {
    $stmt = $conn->prepare("INSERT INTO tbl_items(`itemname`) VALUES (?)");
    $stmt->bind_param("s",$_POST['name']);
    $stmt->execute();
}

This is all untested code.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • Thank you sir. I tried it and all the buttons sends data now but it appears its not still inserting on the database. I tried changing the name of the textbox but it didn't help. – jackfrost Jul 04 '20 at 12:32
  • Your feedback lacks specificity and diagnostic detail. There could be 10 different reasons why your insert is not working. Please research how to show errors on the screen and print out your `$_POST` variable to confirm that it holds all of the data that you expect. – mickmackusa Jul 04 '20 at 12:52
  • Thank you so much sir! Its working now. I have a typo on the code. Thank you so much sir. – jackfrost Jul 04 '20 at 13:19