在单个mysql列中插入动态字段输入

时间:2016-12-06 17:51:33

标签: php jquery html mysql

以下代码的概念是允许用户通过收集每个输入并将它们发布到各个列中来将几个输入字段输入到数据库中,如下面的代码段所示。

我遇到的问题是当我尝试插入代码时它只在mysql表中插入“数组”,因此为什么我尝试使用foreach($BX_NAME as $a => $b){ ?>现在它只输入最后一个输入而不是完整数组。在谷歌搜索和研究后,我发现有一个名为serialize()的函数,但问题是它会将整个数组发布到单个列中,这有效..但我相信如果我单独插入每个列会更好。

非常感谢任何帮助疑难解答。     

     $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 个答案:

答案 0 :(得分:2)

要将每个人放入数据库,请尝试将PHP mysql命令放在foreach循环中,如下所示:

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);

}