0

I'm creating a form using HTML and PHP. I have created a form which I want to submit and save that data in database.

I'm trying to submit a form with data that comes from a while loop. All input values are getting generated by while loop.

The code looks like this.

<table width="1348" border="0" class="table table-striped" >
        <tr>
          <td width="106">&nbsp;</td>

          <td width="332"><strong>Product Code</strong></td>
          <td width="375"><strong>Product Name</strong></td>
          <td width="211"><strong>QTY</strong></td>


      </tr>
                <?php
    $i = 0;
    $rowset = mysql_query("select * from product_detail where productID='".$data['productCode']."'");
    while($stuff = mysql_fetch_array($rowset)){
    ?>
    <tr>

        <td><input type="text" name="code[<?php echo $i?>]" value="<?php enter code hereecho $stuff['code'];?>"/></td>
        <td><input type="text" name="name[<?php echo $i?>]" value="<?php echo $stuff['name'];?>" size="50"/></td>
        <td><input type="text" name="qty[<?php echo $i?>]"  value="<?php echo $stuff['qty'];?>" size="10"/></td>

    </tr>
    <?php $i++; }?>
    <tr id="last">
</table>
<input type="submit" name="save id="save" class="btn btn-primary btn-lg"/>

This is the code to add the data to database.

$code=$_POST['code'.$i];
$name=$_POST['name'.$i];
$qty=$_POST['qty'.$i];
$query = mysqli_query($con,"insert into stock(productCode, productName, qty) values ('".$code."', '".$name."','".$qty."')") or die(mysqli_error($con));
6
  • do you need submit your data from the table ? Commented Apr 24, 2018 at 15:15
  • Look at your $_POST array. Also you are open to SQL injections. Parameterize. Also use mysqli everywhere, no more mysql_*. Commented Apr 24, 2018 at 15:16
  • yes i need to submit my data from a table @diego got it chirs85 Commented Apr 24, 2018 at 15:19
  • Why are you using removed mysql extension to fetch table and use mysqli extension for insert? Commented Apr 24, 2018 at 15:31
  • and what is $lagu vs $stuff? Commented Apr 24, 2018 at 15:38

2 Answers 2

2

First, use prepared statement with bind_param as your script is totally exposed to sql injection.

Second, you can add input type hidden for the number of rows

<form action="" method="POST">
    <table width="1348" border="0" class="table table-striped" >
                <tr>
                        <td width="106">&nbsp;</td>
                        <td width="332"><strong>Product Code</strong></td>
                        <td width="375"><strong>Product Name</strong></td>
                        <td width="211"><strong>QTY</strong></td>
                </tr>
<?php
    $data['productCode'] = "1"; // sample data
    $stmt = $con->prepare("SELECT * FROM product_detail WHERE productID = ?");
    $stmt->bind_param("i", $data['productCode']);
    $stmt->execute();
    $result = $stmt->get_result();
    $i = 0;
    while($stuff = $result->fetch_assoc()) {
?>
            <tr>
                    <td></td>
                    <td><input type="text" name="code[<?php echo $i; ?>]" value="<?php echo $stuff['code'];?>"/></td>
                    <td><input type="text" name="name[<?php echo $i; ?>]" value="<?php echo $stuff['name']; ?>" size="50" /></td>
                    <td><input type="text" name="qty[<?php echo $i; ?>]"  value="<?php echo $stuff['qty']; ?>" size="10" /></td>

            </tr>
<?php
        $i++;
    }
?>
                    <input type="hidden" name="count" value="<?php echo $i; ?>" />
            <tr id="last">
    </table>
<input type="submit" name="save" id="save" class="btn btn-primary btn-lg"/>
</form>

post count with the form

<?php
if (isset($_POST['save'])) {
    $count = $_POST['count'];
    for ($i = 0; $i < $count; $i++) {
        $code = $_POST['code'][$i]; // check empty and check if interger
        $name = $_POST['name'][$i]; // check empty and strip tags
        $qty = $_POST['qty'][$i]; // check empty and check if interger

        $stmt = $con->prepare("INSERT INTO stock (productCode, productName, qty) VALUES (?, ?, ?)");
        $stmt->bind_param("iss",$code,$name,$qty);
        $stmt->execute();
    }
}
?>

You may also want to check if post values are empty with other necessary validation before insert

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

Comments

0

Since the table is dynamically filled, you need to use an array as the name attribute

<table>
        <tr>
            <th>Name</th>
            <th>Present</th>
            <th>Excused</th>
            <th>Unexcused</th>
            <th>Ext</th>
        </tr>
        <?php         
        $query = "select * from TbCard";
        $sql = mysqli_query($connect, $query);
        $count = 0;
            while ($data = mysqli_fetch_array($sql)) {
        ?>
                <tr>
                <td>
                    <input name="tableRow[<?php echo $count; ?>]['dataName']" id='name' type='text' value="<?php echo $data['Name'];?>" readonly style='border:none;width:350px'></input>
                </td>
                <td>
                    <input name="tableRow[<?php echo $count; ?>]['status']" type="radio" value="Present"> Present
                </td>
                <td>
                    <input name="tableRow[<?php echo $count; ?>]['status']" type="radio" value="Excused"> Excused
                </td>
                <td>
                    <input name="tableRow[<?php echo $count; ?>]['status']" type="radio" value="Unexcused"> Unexcused
                </td>
                </tr>;
        <?php
             $count++;
            }
        ?>
    </table>

The php would be something like this, assuming that the data has values in it:

$tableRow = $_POST['tableRow'];
foreach($tableRow as $row){
   /* here insert data from post */
    echo $row['dataName'].' '.$row['status'].'<br/>';
}

To see the content of the array, use print_r($tableRow) in this case i use a name tableRow

1 Comment

ok, i will try it, thanks a lot for the help and i will let u know the result

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.