3

the concept of the code below is to allow a user to input several input fields into the database by collecting each input and posting them in individual columns as seen in the snippet below.

The issue I'm having is when i try to insert the code it only inserts "array" in the mysql table, hence why I tried using foreach($BX_NAME as $a => $b){ ?> Now it only inputs the last inputs rather than the full array. After googling and researching I figured out there is a function called serialize() but the issue is it will post the entire array into a single column, that works.. but I believe it would be better if I inserted each column individually.

Any help troubleshooting will be greatly appreciated.

     $error = false;

     if ( isset($_POST['btn-signup']) ) {

      // clean user inputs to prevent sql injections


      $company_invoice = trim($_POST['company_invoice']);
      $company_invoice = strip_tags($company_invoice);
      $company_invoice = htmlspecialchars($company_invoice);

      $contact_name = trim($_POST['contact_name']);
      $payment_type = strip_tags($contact_name);
      $payment_type = htmlspecialchars($contact_name);

      $added_by = trim($_POST['added_by']);
      $added_by = strip_tags($added_by);
      $added_by = htmlspecialchars($added_by);

            $chkbox = $_POST['chk'];
            $BX_NAME=$_POST['BX_NAME'];
            $BX_PRICE_POST['BX_price'];     

       foreach($BX_NAME as $a => $b){ ?>

                    <?php echo $BX_NAME[$a]; ?>
                  <?php echo $BX_price[$a]; 
                }

      // if there's no error, continue to Registeration
      if( !$error ) {

       $query = "INSERT INTO company_invoices(company_name,contact_name,BX_NAME,BX_price,added_by,date_added) VALUES('$company_name','$contact_name','$BX_NAME[$a]','$BX_price[$a]', ' $added_by',now())";
       $res = mysql_query($query);

function addRow(tableID) {
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	if(rowCount < 5){							// limit the user from creating fields more than your limits
		var row = table.insertRow(rowCount);
		var colCount = table.rows[0].cells.length;
		for(var i=0; i<colCount; i++) {
			var newcell = row.insertCell(i);
			newcell.innerHTML = table.rows[0].cells[i].innerHTML;
		}
	}else{
		 alert("Maximum Passenger per ticket is 5.");
			   
	}
}

function deleteRow(tableID) {
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	for(var i=0; i<rowCount; i++) {
		var row = table.rows[i];
		var chkbox = row.cells[0].childNodes[0];
		if(null != chkbox && true == chkbox.checked) {
			if(rowCount <= 1) { 						// limit the user from removing all the fields
				alert("Cannot Remove all the Passenger.");
				break;
			}
			table.deleteRow(i);
			rowCount--;
			i--;
		}
	}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
   <legend>Invoice Details</legend>
        <p> 
          <input type="button" value="Add Row" onClick="addRow('dataTable')" /> 
          <input type="button" value="Remove Row" onClick="deleteRow('dataTable')"  /> 
          <p>(All acions apply only to entries with check marked check boxes only.)</p>
        </p>
               <table id="dataTable" class="form" border="0">
                  <tbody>
                    <tr>
                      <p>
            <td><input type="checkbox" required="required" name="chk[]" checked="checked" /></td>
            <td>
              <label>Item Name</label>
              <input type="text" required="required" name="BX_NAME[]">
             </td>
             <td>
              <label for="BX_price">Price</label>
              <input type="text" required="required"   name="BX_price[]">
               </td>
             
              </p>
                    </tr>
                    </tbody>
                </table>

1 Answer 1

2

To put each one into the database, try putting the PHP mysql command inside the foreach loop, like this:

foreach($BX_NAME as $a => $b){
    echo $BX_NAME[$a]; 
    echo $BX_price[$a]; 

    $query = "INSERT INTO company_invoices(company_name,contact_name,BX_NAME,BX_price,added_by,date_added) VALUES('$company_name','$contact_name','$BX_NAME[$a]','$BX_price[$a]', ' $added_by',now())";
   $res = mysql_query($query);

}
Sign up to request clarification or add additional context in comments.

Comments

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.