I am working on a web based erp kinda platform but stuck at this situation where I want to save all the data from some textbox and select tags than save them all to the database.
This is the a simple table. After the page finishes loading by default there is one row for inserting data. But if (and only if) the buyer wants to buy some more material than I want the user to insert more row and save all the data to the db.
<form method="post" style="border:1px solid #ccc">
<div class="container"><label><b>Billing details</b></label><table border='1' id="myTable" class="myTable">`<form method="post" style="border:1px solid #ccc">
<div class="container">
<label><b>Billing details</b></label>
<table border='1' id="myTable" class="myTable">
<thead>
<tr>
<th>Sr. No</th>
<th>Particulars</th>
<th>QTY</th>
<th>RATE</th>
<th>DISCOUNT</th>
<th id="gt">AMOUNT</th>
</tr>
</thead>
<tbody>
<tr id="content">
<td></td>
<td><?php echo $select; ?></td>
<td><input type="text" name="qty" id="qty" class="qty" onKeyPress="sum()"/></td>
<td><input type="text" name="rates" id="rates" class="rates" onKeyPress="sum()"/></td>
<td><input type="text" name="dis" placeholder="0" class="dis" id="dis" onKeyPress="sum()"/></td>
<td><input type="text" name="amount" readonly class="amount" id="amount"/></td>
</tr>
</tbody>
</table>
<div><b>Total Amount</b><b><input type="text" name="total" id="gTotal" class="gTotal" readonly/></b></div><br/>
<div><b>Paid Amount</b><input type="text" name="paidamount" class="paidamount" id="paidamount"/></div><br/>
<div><b>Balance Amount</b><input readonly type="text" name="balance" class="balance" id="balance"/></div><br/>
<div id="addrow" style="width:50px;float: left;" onClick="addRow()">+</div>
<div id="remrow" style="width:50px;float: left;" onClick="remRow()">-</div>
<div class="clearfix">
<input class="signupbtn" type="submit" name="submit" value="Register"/>
</div>
</div>
</form>
The PHP Part
include "dbconnection.php";
$sql=mysqli_query($connection,"SELECT id,name FROM materials");
if(mysqli_num_rows($sql)){
$select= '<select class="mater" name="mate">';
while($rs=mysqli_fetch_array($sql)){
$select.='<option value='.$rs['name'].'>'.$rs['name'].'</option>';
}
}
$select.='</select>';
mysqli_close($connection);
if(isset($_POST['submit'])){
print_r($_POST['mate']);
echo count($_POST['mate']);
foreach($_POST as $name => $item){
echo count($_POST['mate']);
if($name != 'submit'){
for($m=0; $m < sizeof($item); $m++){
echo ($name.' '.$item[$m].'<br>');
//Insert something in db
}
}
}
}
The print_r($_POST['mate']);
prints the value selected in the last row ( for more than one row) or value in the first row (if user does not add additional row). This is my table structure
id name address mobile material qty discount total balance
The sizeof($item)
or count()
returns 1 even after adding one more row.
I have tried messing up with something like [0], but got confused. I am not well with php and this is my first project related to the dbs.
What I want is when user clicks on register, all the data available in the text box and the select element gets save in the db. Each table row should be saved to one separate row. I have searched a lot but could not get it working.
</div>