Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working on a small project in php which I want do bulk insert into mysql table when I click on select all checkbox. Below code works fine select all. But when I select I select or check any the checkbox in the middle of the table nothing will insert. Please I have spent one whole week trying to figure something out, but nothing seems to work for me. I am begging for help. Please please please.

I have add my html table to code. Please I am begging for help. Thank you

What I have tried:

<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
									<thead style="background:#F4582D;color:#fff;">
											<tr>
											<th><input type="checkbox" id="selectall" onClick="china_toggle(this)"><font color="#fff">Select All</font></th>
												<th><font color="#fff">Product</font></th>
												<th class="hidden-xs"><font color="#fff">Unit Cost</font></th>
												<th><font color="#fff">Sale Price</font></th>
                                                <th><font color="#fff">Total Pieces</font></th>
												<th><font color="#fff">Qty to Move</font></th>
												<th><font color="#fff">Cost Value</font></th>
												<th><font color="#fff">Sales Value</font></th>
                                                <th><font color="#fff">Profit Margin</font></th>
												
											</tr>
										</thead>
										<tfoot style="background:#F4582D;color:#fff;">
                                            <tr>
											<th></th>
												<th><font color="#fff">Product</font></th>
												<th class="hidden-xs"><font color="#fff">Unit Cost</font></th>
												<th><font color="#fff">Sale Price</font></th>
                                                <th><font color="#fff">Total Pieces</font></th>
												<th><font color="#fff">Qty to Move</font></th>
												<th><font color="#fff">Cost Value</font></th>
												<th><font color="#fff">Sales Value</font></th>
                                                <th><font color="#fff">Profit Margin</font></th>
                                            </tr>
                                        </tfoot>
										
	                                   <tbody>
											
											
<?php
										
									
	if (isset($_GET['page_no']) && $_GET['page_no']!="") {
	$page_no = $_GET['page_no'];
	} else {
		$page_no = 1;
        }

	$total_records_per_page = 100;
    $offset = ($page_no-1) * $total_records_per_page;
	$previous_page = $page_no - 1;
	$next_page = $page_no + 1;
	$adjacents = "2"; 

	$result_count = mysqli_query($con,"SELECT COUNT(*) As total_records FROM `low_stock_v`");
	$total_records = mysqli_fetch_array($result_count);
	$total_records = $total_records['total_records'];
    $total_no_of_pages = ceil($total_records / $total_records_per_page);
	$second_last = $total_no_of_pages - 1; // total page minus 1																				
														
$sql=mysqli_query($con,"select * from low_stock_v LIMIT $offset, $total_records_per_page");
while($row=mysqli_fetch_array($sql))
{
?>

											<tr>
											<form method="post" action="">
											    <td><input type="checkbox" class="china[]" name="china[]"  id="china[]" value="<?php echo $row['pro_id'] ?>" ></td>
												<td><input type="text" class="prod[]" name="prod[]" id="prod[]" value="<?php echo $row['pro_descrip'];?>" style="width:100%;border:none;" readonly="true" border="none" /></td>
												<td><?php echo $row['unit_cost'];?></td>
												<td><?php echo $row['sales_price'];?></td>
                                                <td><?php echo $row['totpcs'];?></td>
												<td><input type="text" class="qtyout[]" name="qtyout[]" id="qtyout[]" value="<?php echo isset($_POST['qtyout[]']) ? $_POST['qtyout[]'] : '' ?>" size="4" /></td>
												<td><?php echo $row['p_cost'];?></td>
                                                <td><?php echo $row['Sales_Value'];?></td>
												<td><?php echo $row['Profit_Margin'];?></td>
												<input type="hidden" class="proqty" name="proqty" id="proqty" value="0"/>
											</tr>
											
											<?php 
											 }?>
											
											
										</tbody>
									</table>
									<p align="center"><button type="submit" class="btn btn-success" name="save">Move Selected Row(s) to Store</button></p>
            </form>




if(isset($_POST['save'])){
    for($i=0;$i<count($_POST['china']);$i++){
        $chk = $_POST['china'][$i];
        $prod_name = $_POST['prod'][$i];
            $qty = $_POST['qtyout'][$i];
            if($chk!=='' && $prod_name!=='' && $qty!==''){
            mysqli_query($con,"insert into bulk_insert_t (pro_id,pro_descrip,qty)VALUES('$chk','$prod_name','$qty')");
            
                //echo '<div class="alert alert-success" role="alert">Submitted Successfully</div>';
            }
            else{
                
                echo "<script type='text/javascript'>";
            echo "alert('Error Submitting in Data')";
            echo "</script>";
echo "<script>window.location.href ='product-list.php'</script>";

            }
        }
        echo "<script type='text/javascript'>";
            echo "alert('Submitted successfully')";
            echo "</script>";
echo "<script>window.location.href ='product-list.php'</script>";

    }
Posted
Updated 2-Feb-21 2:30am
v2
Comments
Richard Deeming 2-Feb-21 6:25am    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
PHP: SQL Injection - Manual[^]
oLiontas 2-Feb-21 7:04am    
You don't declare $con parameter in your code.You can add an if statement for $con eg
If $con{code here}else{echo "fail";}
Mathiudi 2-Feb-21 7:08am    
So please what is the best practice. Any help?
Mathiudi 2-Feb-21 7:09am    
Okay Thanks
oLiontas 2-Feb-21 7:15am    
Also post any error if you have. It helps us a lot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900