Help needed with inserting rows

I need to add more rows and then insert them into my preferred table.in my program adding more row is working but i can’t insert them only first row is insert

<?php
error_reporting(0);
include('config.php');
$invno=$_POST['invno'];
$indate=$_POST['indate'];
$cid=$_POST['cid'];
$iid=$_POST['iid'];
$rate=$_POST['rate'];
$qty=$_POST['qty'];
$no=$_POST['no'];
$box=$_POST['box'];
$cover=$_POST['cover'];
$nos=$_POST['nos'];
$disc=$_POST['disc'];
$xat=$_POST['xat'];
$tot=$_POST['tot'];
?>
<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8" />
	<link rel="apple-touch-icon" sizes="76x76" href="../assets/img/apple-icon.png" />
	<link rel="icon" type="image/png" href="../assets/img/favicon.png" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

	<title>FORM HOUSE</title>

	<meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' name='viewport' />
    <meta name="viewport" content="width=device-width" />

    <!-- Bootstrap core CSS     -->
    <link href="../assets/css/bootstrap.min.css" rel="stylesheet" />

    <!--  Material Dashboard CSS    -->
    <link href="../assets/css/material-dashboard.css" rel="stylesheet"/>

    <!--  CSS for Demo Purpose, don't include it in your project     -->
    <link href="../assets/css/demo.css" rel="stylesheet" />

    <!--     Fonts and icons     -->
    <link href="http://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css" rel="stylesheet">
    <link href='http://fonts.googleapis.com/css?family=Roboto:400,700,300|Material+Icons' rel='stylesheet' type='text/css'>
	<style>	
	label
	{
		
		font-size:15px;
		font-weight:bold;
		color:#000;
	}
	</style>
	</head>

<body>
<div class="wrapper">
		<div class="sidebar" data-color="green" data-image="../assets/img/sidebar-1.jpg">
			

			<div class="logo">
				<a href="#" class="simple-text">
					FORM HOUSE
				</a>
			</div>

	    	<div class="sidebar-wrapper">
	            <ul class="nav">
	                <li class="dropdown">
	                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">
	                        <i class="material-icons ">dashboard</i>
	                        <p>MASTER</p>
	                    </a>
						<ul class="dropdown-menu">
									<li><a href="Add_category.php">Category</a></li>
									<li><a href="Sub_category.php">SubCategory</a></li>
									<li><a href="unit.php">Unit</a></li>
									<li><a href="item.php">ItemMaster</a></li>
									<li><a href="supplier.php">AddSupplier</a></li>
									<li><a href="customer.php">AddCustomer</a></li>
									<li><a href="employee.php">AddEmployee</a></li>
								</ul>
	                </li>
	                <li class="active dropdown">
	                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">
	                        <i class="material-icons">person</i>
	                        <p>INVENTORY</p>
	                    </a>
						<ul class="dropdown-menu">
									<li><a href="stock.php">OpeningStock</a></li>
									<li><a href="purchase.php">Purchase</a></li>
									<li><a href="sales.php">Sales</a></li>
									<li><a href="#">purchaseReturn</a></li>
									<li><a href="#">SalesReturn</a></li>
								</ul>
	                </li>
	                <li class="dropdown">
	                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">
	                        <i class="material-icons">content_paste</i>
	                        <p>ACCOUNTS</p>
	                    </a>
						<ul class="dropdown-menu">
									<li><a href="#">Expenses</a></li>
									<li><a href="#">PaymentVoucher</a></li>
									<li><a href="#">ReceiptVoucher</a></li>
								</ul>
	                </li>
	                <li class="dropdown" >
	                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">
	                        <i class="material-icons">library_books</i>
	                        <p>REPORTS</p>
	                    </a>
						<ul class="dropdown-menu">
									<li><a href="#">SalesReport</a></li>
									<li><a href="#">PurchaseReport</a></li>
									<li><a href="#">OutstandingReport</a></li>
									<li><a href="#">ExpensesReport</a></li>
									<li><a href="#">StockReport</a></li>
									<li><a href="#">ProfitandLossReport</a></li>
								</ul>
	                </li>
	            </ul>
	    	</div>
			 </div>
				<div class="main-panel">
			<nav class="navbar navbar-transparent navbar-absolute">
				<div class="container-fluid">
					<div class="navbar-header">
						<button type="button" class="navbar-toggle" data-toggle="collapse">
							<span class="sr-only">Toggle navigation</span>
							<span class="icon-bar"></span>
							<span class="icon-bar"></span>
							<span class="icon-bar"></span>
						</button>
						<a class="navbar-brand" href="#">Inventory Dashboard</a>
					</div>
					<div class="collapse navbar-collapse">
						<ul class="nav navbar-nav navbar-right">
							<li class="dropdown">
								<a href="#" class="dropdown-toggle" data-toggle="dropdown">
									<p>MENU</p>
								</a>
								<ul class="dropdown-menu">
									<li><a href="#">MASTER</a></li>
									<li><a href="#">INVENTORY</a></li>
									<li><a href="#">ACCOUNTS</a></li>
									<li><a href="#">REPORTS</a></li>
								</ul>
							</li>	
						</ul>
					</div>
				</div>
			</nav>
			
			<div class="content">
				<div class="container-fluid">
					<div class="row">
						
						<div class="col-md-11">
							<div class="form">
							<form action="" method="post" name="sales_1_fm">
								<div class="col-md-3">
								<label>Inv No</label>
								<input type="text" name="invno" class="form-control" id="inno" value="<?php 
								if(isset($invno))
								{
									echo $invno;
								}
								?>">
								</div>
								<script>
								document.getElementByClass('inno').value="<?php if(isset($invno)){ echo $invno; } ?>";
								</script>
								<div class="col-md-3">
								<label>Inv Date</label>
								<input type="date" name="indate" class="form-control" id="idate" value="<?php 
								if(isset($indate))
								{
									echo $indate;
								}
								?>">
								</div>
								<script>
								document.getElementByClass('idate').value="<?php if(isset($indate)){ echo $indate; } ?>";
								</script>
								<div class="col-md-3">
								<label>Customer Id</label>
								<input type="text" name="cid" class="form-control" id="cust" onchange="javascript:document.sales_1_fm.submit();" 
								value="<?php 
								if(isset($cid))
								{
									echo $cid;
								}
								?>">
								</div>
								<script>
								document.getElementByClass('cust').value="<?php if(isset($cid)){ echo $cid; } ?>";
								</script>
								<div class="col-md-3">
								<label>Customer Name</label>
								<input type="text" name="cname" class="form-control" value="<?php 
								$sql=mysqli_query($con,"select * from add_customer where id='".$cid."' ");
									if($res=mysqli_fetch_array($sql))
									{
										echo $res['cname'];
									}
								?>">
								</div>
								<div class="col-md-3">
								<label>Mobile</label>
								<input type="text" name="mob" class="form-control" value="<?php 
								$sql=mysqli_query($con,"select * from add_customer where id='".$cid."' ");
									if($res=mysqli_fetch_array($sql))
									{
										echo $res['mobile'];
									}
								?>">
								</div>
								<div class="col-md-3">
								<label>Tin No</label>
								<input type="text" name="tin" class="form-control"
								value="<?php 
								$sql=mysqli_query($con,"select * from add_customer where id='".$cid."' ");
									if($res=mysqli_fetch_array($sql))
									{
										echo $res['tinno'];
									}
								?>">
								</div>
								
						<div class="card card-stats">
							<div class="card-header"  data-background-color="green">
							<p>Sales Entry</p>
							</div>
							<div class="table table-responsive">
							<table class="table  table-bordered" id="tab_logic">
								<tr class="success">
									<th><label>No</label></th>
									<th><label>Item Code</label></th>
									<th><label>Item Name</label></th>
									<th><label>Rate</label></th>
									<th><label>Qty</label></th>
									<th><label>Nos</label></th>
									<th><label>Total</label></th>
									<th><label>Box</label></th>
									<th><label>Cover</label></th>
									<th><label>Add</label></th>
								</tr>
								<tr id='addr0'>
								<td><input type="text" name="no" class="form-control" class="bno"
							value="<?php 
							if($no){ 
							echo $no; 
							} 
							?>"><script>
							document.getElementByClass('bno').value="<?php if(isset($no)){echo $no;}?>";
							</script></td>
							<td><input type="text" name="iid" class="supp" class="form-control" onchange="javascript:document.sales_1_fm.submit();"
							value="<?php 
							if($iid){ 
							echo $iid; 
							} 
							?>">
							<script>
							document.getElementByClass('supp').value="<?php if(isset($iid)){echo $iid;}?>";
							</script>
							</td>
							<td>
							<input type="text" name="iname" class="form-control" value="<?php 
							$sqli=mysqli_query($con,"select * from item_master where itemcode='".$iid."'") or die(mysqli_error($con));
							if($row=mysqli_fetch_array($sqli))
							{
								echo $row['itemname'];
							}
							?>">
							</td>
							<td><input type="text" name="rate" class="amt" class="form-control" 
							value="<?php
							if(isset($rate)){
								echo $rate;
							}
							?>">
							<script>
							document.getElementByClass('amt').value="<?php if(isset($rate)){echo $rate;}?>";
							</script>
							</td>
							<td><input type="text" name="qty" class="form-control" class="qual" 
							value="<?php
							if(isset($qty)){
								echo $qty;
							}
							?>">
							<script>
							document.getElementByClass('qual').value="<?php if(isset($qty)){echo $qty;}?>";
							</script>
							</td>
							<td><input type="text" name="nos" class="coun" class="form-control"  onchange="javascript:document.sales_1_fm.submit();"
							value="<?php
							if(isset($nos)){
								echo $nos;
							}
							?>">
							<script>
							document.getElementByClass('coun').value="<?php if(isset($nos)){echo $nos;}?>";
							</script>
							</td>
							<td><input type="text" name="tot" class="form-control" class="subt"
							value="<?php if(isset($tot)){ 
							$tot = $rate * $nos; 
							echo $tot;
							}?>">
							<script>
							document.getElementByClass('subt').value="<?php if(isset($tot)){echo $tot;}?>";
							</script></td>
							<td><input type="text" name="box" class="form-control" class="boc" value="<?php
							if(isset($box))
							{
								echo $box;
							}
							?>"></td>
							<script>
							document.getElementByClass('boc').value="<?php if(isset($box)){echo $box;}?>";
							</script>
							<td><input type="text" name="cover" class="form-control" class="cov" value="<?php
							if(isset($cover))
							{
								echo $cover;
							}
							?>"></td>
							<script>
							document.getElementByClass('cov').value="<?php if(isset($cover)){echo $cover;}?>";
							</script>
							<td><input type="button" name="sentr" value="Add" class="btn btn-success" id="add_row"></td>
							</tr>
							<tr id='addr1'></tr></table>
							<table class="table table-responsive table-bordered">
							<tr>
							<td colspan="7"></td>
							<td><b>RateTotal:</b>
							<?php
							$rates+=$rate;
							echo $rates;
							?>
							</td>
							<td><b>TotalQty:</b>
							<?php
							$quty+=$qty;
							echo $quty;
							?>
							</td>
							<td><b>TotalNos:</b>
							<?php
							$num+=$nos;
							echo $num;
							?>
							</td>
							<td colspan='4'><b>SubTotal:</b>
							<?php
							$total+=$tot;
							echo $total;
							?>
							</td>
							</tr>
							<tr>
							<td colspan="7"></td>
							<td><b>Discount</b></td>
							<td colspan='4'><input type="text" name="disc" class="dcount" class="form-control" placeholder="Type your discount...." value="<?php
							if(isset($disc)){
								echo $disc;
							}
							?>">
							<script>
							document.getElementByClass('dcount').value="<?php if(isset($disc)){echo $disc;}?>";
							</script>
							</td>
							</tr>
							<tr>
							<td colspan="7"></td>
							<td><b>Tax</b></td>
							<td colspan='4'><input type="text" name="xat" class="taxx" class="form-control" placeholder="Type your tax...."  onchange="javascript:document.sales_1_fm.submit();"
							value="<?php
							if(isset($xat)){
								echo $xat;
							}
							?>">
							<script>
							document.getElementByClass('taxx').value="<?php if(isset($xat)){echo $xat;}?>";
							</script>
							</td>
							</tr>
							<tr>
							<td colspan="7"></td>
							<td><b>RoundOff</b>
							<td colspan='4'><input type="text" name="roff" class="form-control" value="<?php 
							$cp= ($disc/100) * $total;
							$cp=$total-$cp;
							$fp=($xat/100) * $cp;
							$fp+=$cp;
							echo round($fp);
							?>"></td>
							</tr>
							<tr>
							<td colspan="7"></td>
							<td><b>GrandTotal</b></td>
							<td colspan="4"><input type="text" name="gtot" class="form-control" 
							value="<?php 
							$cp= ($disc/100) * $total;
							$cp=$total-$cp;
							$fp=($xat/100) * $cp;
							$fp+=$cp;
							echo round($fp);
							?>"></td>
							</tr>
							<tr>
							<td colspan="6"></td>
							<td><a href="#" class="btn btn-success btn-md">Print</a></td>
							<td><input type="submit" name="sav" value="save" class="btn btn-success btn-md"></td>
							<td><a href="sales.php" class="btn btn-md btn-success">Clear</a></td>
							</tr>
							</table>
							
							</div>
						</div>
									
							</form>							
							</div>
						</div>
						<?php
							 if(isset($_POST['sav']))
							{
								$cname=$_POST['cname'];
								$tinno=$_POST['tin'];
								$tot=$_POST['tot'];
								$iname=$_POST['iname'];
								$mob=$_POST['mob']; 
								$gtot=$_POST['gtot'];

								$ins=mysqli_query($con,"insert into sales_entry(invno,invdate,cid,cname,mobile,tinno,sno,icode,iname,rate,qty,nos,total,box,cover,Discount,Tax,G_Total,rate_total,total_qty,total_nos,sub_total) 
								values('$invno','$indate','$cid','$cname','$mob','$tinno','$no','$iid','$iname','$rate','$qty',$nos,'$tot','$box','$cover','$disc','$xat','$gtot','$rates','$quty','$num','$total')") or die(mysqli_error($con));
							
								header('location:sales.php');
							} 
							?>
							
			
			</div>
			</div>
			</div>
			
			
		</div>
	   
	</div>
</body>
<!--   Core JS Files   -->
	<script src="../assets/js/jquery-3.1.0.min.js" type="text/javascript"></script>
	<script src="../assets/js/bootstrap.min.js" type="text/javascript"></script>
	<script src="../assets/js/material.min.js" type="text/javascript"></script>

	<!--  Charts Plugin -->
	<script src="../assets/js/chartist.min.js"></script>

	<!--  Notifications Plugin    -->
	<script src="../assets/js/bootstrap-notify.js"></script>

	<!--  Google Maps Plugin    -->
	<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js"></script>

	<!-- Material Dashboard javascript methods -->
	<script src="../assets/js/material-dashboard.js"></script>

	<!-- Material Dashboard DEMO methods, don't include it in your project! -->
	<script src="../assets/js/demo.js"></script>
	<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
	<script type="text/javascript">
    	$(document).ready(function(){

			// Javascript method's body can be found in assets/js/demos.js
        	demo.initDashboardPageCharts();

    	});
	</script>
	
	<script>
	$(document).ready(function() {
  var i = 1;
  $("#add_row").click(function() {
 $('#addr'+(i+1)).find('input').attr('disabled',false);
 $('#addr' + i).html("<form action='' method='post'><td><input type='text' name='no" + i + "' class='form-control input-md bno'/></td><td><input type='text' name='iid" + i + "'  class='form-control input-md supp'/></td><td><input type='text' name='iname" + i + "'  class='form-control input-md '/></td><td><input type='text' name='rate" + i + "'  class='form-control input-md amt'/></td><td><input type='text' name='qty" + i + "' class='form-control input-md qual'/></td><td><input type='text' name='nos" + i + "' class='form-control input-md coun'/></td><td><input type='text' name='tot" + i + "' class='form-control input-md subt'/><td><input type='text' name='box" + i + "' class='form-control input-md boc'/><td><input type='text' name='cover" + i + "' class='form-control input-md cov'/><td></form>");

    $('#tab_logic').append('<tr id="addr' + (i + 1) + '"></tr>');
    i++;
  });
});
</script>
</html>

Can you expand on this? In particular, you say “adding more row is working”, but then you say that inserting a row only works for the first one.

Specifically, what happens when you try to insert the second and subsequent rows? Is there an error message? Does the data make it to the database but not update the screen? Can you clarify you are talking about inserting rows into the database table, not the html table?

For the inserts that fail, are all the variables correct as you split them out in your PHP code prior to running the query? If you run the query manually in phpmyadmin with the same values, does it work?

I think I’d move the section of code where you run the insert to the top of the file, just after you’ve retrieved all the $_POST variables. Apart from anything else, the header redirect isn’t going to work because you’ve output all that html before it. You should be getting a “headers already sent” error there.

i am adding more rows for billing at that many no of products will be added to the rows.After finishing billing
i need insert all products and store in database including new rows also

I’m still not clear on whether you mean database table rows, or html table rows. Also on all the other questions I asked about whether you get error messages, and whether the various form variables have the values you expect, and whether if you run the query with those values, does it work?

i mean html table rows

Might be something to try in the JavaScript area of the forum, too. It would probably be a help if you could narrow down specifically which part of the code is causing the problem - I created a very small HTML table and used your “add row” code, and was able to make it add rows into the html table.

This part is causing problem from inserting values

You’re definitely going to get a Headers already sent error right here if you have errors enabled and error logs turned on. In HTTP, you cannot have any outputs before the headers. In your case, you’ve outputted 100+ lines of HTML before the header was sent.

You don’t close the opening <td> tags on two of those lines, could that be the problem? tot and box open a new <td> each, but never close it. Failing that perhaps you could expand on exactly what is happening that doesn’t work as expected.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.