Multiple insert records from same form using same name text input

I want insert multiple names and pay values from one form and table using checkbox.
The main idea is get some unpaid receipt results from 1 mysql query and insert the pay receipts on mysql.

<form name"teste" method="post" action="">
  <table width="200" border="1">
  <tbody>
    <tr>
      <td>
        <input type="text" name="name[]" id="name"></td>
      <td><input name="pay[]" type="checkbox" value="1" id="valor"></td>
    </tr>
    <tr>
      <td>
        <input type="text" name="name[]" id="name"></td>
      <td><input name="pay[]" type="checkbox" value="1"></td>
    </tr>
    <tr>
      <td>
        <input type="text" name="name[]" id="name"></td>
      <td><input name="pay[]"type="checkbox" value="1"></td>
    </tr>
    <tr>
      <td>
        <input type="text" name="name[]" id="name"></td>
      <td><input name= "pay[]" type="checkbox" value="1"></td>
    </tr>
  </tbody>
</table>
		<p>
		  <input type="submit" name="submit" id="submit" value="Enviar">
		</p>	
  </form>

<?php

	if(isset($_POST['name'])){

	$name[]=($_POST['name']);
	$pay[]=($_POST['pay']);
	$arr=array(array($name), array($pay)
	);	
		
	//$value=array($_POST['pay']);
	
	echo'<pre>';
	print_r($arr);
    echo'</pre>';
	
	
		
		foreach($arr as $key=>$p){
		
			// I want echo $name " pay = " $pay
			
			// I´m lost I cant foun solution
		}
	
		



?>

I found same part of solution but I´m having notice error when i not check all checkbox

<?php
	
	
	
		if(isset($_POST['name'])&&isset($_POST['pay'])){
			$pay_array=0;
		$name_array=$_POST['name'];
		$pay_array=$_POST['pay'];
			
		
		
			
		//$value=array($_POST['pay']);
		
		echo '<pre>';
		print_r($name_array);
	    echo'</pre>';
		
		
			
				
			for($i=0; $i< count($name_array);$i++){
			
				if(($pay_array[$i]=='1')&&($pay_array[$i]!="")){
						echo   $name_array[$i] ." pay = ". $pay_array[$i] ."<br>";
						
				// I´m lost I cant foun solution
			}
		}
		}
			
	
	
	
	?>

The problem is the unchecked check box return empty.
If I can return “0” I have solution. But I not know how to do it.

The name[…] array and the pay[…] need to have the same index values. If you are editing existing data from a database table, you would use the data’s id (auto-increment integer) as the index values. If you are instead inserting new data, you would just use a php variable and increment it for each set of form fields.

If there are no checked check-boxes, $_POST[‘pay’] won’t be set. If $_POST[‘pay’] is set, you can use a foreach(){} loop to get the index values, then reference the corresponding $_POST[‘name’][$index] values.

1 Like

Thank you! I need!

<form name"teste" method="post" action="">
  <table width="200" border="1">
  <tbody>
<tr>
  <td>
    <input type="text" name="name[0]" id="name"></td>
  <td><input name="pay[0]" type="checkbox"  value="1" id="valor"></td>
</tr>
<tr>
  <td>
    <input type="text" name="name[1]" id="name"></td>
  <td><input name="pay[1]" type="checkbox" value="1"></td>
</tr>
<tr>
  <td>
    <input type="text" name="name[2]" id="name"></td>
  <td><input name="pay[2]"type="checkbox" value="1"></td>
</tr>
<tr>
  <td>
    <input type="text" name="name[3]" id="name"></td>
  <td><input name= "pay[3]" type="checkbox" value="1"></td>
</tr>
  </tbody>
</table>
		<p>
		  <input type="submit" name="submit" id="submit" value="Enviar">
		</p>

	
  </form>


<?php	
		if(isset($_POST['name'])&&isset($_POST['pay'])){
		
		$name_array=$_POST['name'];
		$pay_array=$_POST['pay'];	
			
		//$value=array($_POST['pay']);
		
		echo '<pre>';
		print_r($name_array);
	    echo'</pre>';
		echo '<pre>';
		print_r($pay_array);
	    echo'</pre>';		
				
					
				foreach($pay_array as $a=>$k){	
						echo "nome: " .$name_array[$a]. " pay = ".  $k ."<br>";				
					}
				}	
	?>
Sponsor our Newsletter | Privacy Policy | Terms of Service