0

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.

2
  • "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" you need to divide your problem in 2 separate steps, 1 manage the client part and successfully send the data to the server (check user8836893 answer) , 2 take the data from the PHP variables and save them to your database Commented Nov 2, 2017 at 11:23
  • Thanks @Accountant It worked well. Commented Nov 2, 2017 at 11:38

1 Answer 1

1

You can give name like mate[] for each select element inclusding dynamically created element. So in post data you will get array named mate.

<select class="mater" name="mate[]">
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I was searching on the net for over two days but did not thought about this. Now i am able to insert all the data from all the row to the db but as you can see in the html table there is a column for discount. Which is used to give discount in int. How can i set the default value to it (say 0) for this kind of error. Notice: Uninitialized string offset: 0
Searched and got it.

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.