简体   繁体   中英

Adding new table row and inserting value in database php/mysql/jquery

I am trying to do a trial balance page,when i add new row to input values and hit save it does not take all the rows in the table instead it saves the first one.

PHP

 <tr class="item-row" style="font-size:12px;">
     <td class="item-name" style="margin-right: 5em;background:#A7d6F1;">
          <div class="delete-wpr">
              <textarea onkeypress="return tabE(this,event)" style="font-family: Tahoma, sans-serif;" rows="2" cols="43" maxlength="6"class="code" name="acc_code[]" id="acc_code[]"></textarea>
              <textarea onkeypress="return tabE(this,event)" readonly style="text-align:center;margin-top:-34px;margin-left:20em;font-family: Tahoma, sans-serif;" rows="2" cols="10" maxlength="6"class="type" name="type[]" id="type[]"></textarea>
              <a class="delete" href="javascript:;" title="Remove row">X</a>
          </div>
     </td>            
     <td class="item-name" align="center">
          <textarea onkeypress="return tabE(this,event)" autocomplete="off" style="font-family: Tahoma, sans-serif;" rows="2" cols="8" maxlength="6" name="int_code[]" id="int_code[]" class="intCode"></textarea>
     </td>
     <td >
          <textarea onkeypress="return tabE(this,event)" autocomplete="off" style="font-family: Tahoma, sans-serif;"rows="2" cols="15" maxlength="11" name="debit_mvt[]" id="debit_mvt[]" class="cost"></textarea>
     </td>
     <td style="">
          <textarea onkeypress="return tabE(this,event)" autocomplete="off" style="font-family: Tahoma, sans-serif;" rows="2" cols="15" maxlength="11" name="credit_mvt[]" id="credit_mvt[]" class="qty"></textarea>
     </td>
     <td>
          <textarea style="font-family: Tahoma, sans-serif;" rows="2" cols="15" maxlength="10" autocomplete="off" name="balance[]" id="balance[]" class="price" readonly></textarea>
     </td>
     <td style="background:#A7D6F1;font-family: Tahoma, sans-serif;">
          <textarea style="font-family: Tahoma, sans-serif;border:none;background-color:white"rows="2" cols="15" maxlength="10" class="deb"name="debitOld[]" id="debitOld[]" ></textarea>
     </td>
     <td style="background:#A7D6F1;font-family: Tahoma, sans-serif;">
          <textarea style="font-family: Tahoma, sans-serif;border:none;background-color:white"rows="2" cols="15" maxlength="10" class="cred"name="creditOld[]" id="creditOld[]" ></textarea>
     </td>  

Add a row

Jquery

$("#addrow").click(function(){

$(".item-row:last").after('<tr class="item-row" style="font-size:12px;"><td class="item-name" style="background:#A7d6F1;"><div class="delete-wpr"><textarea  style="font-family: Tahoma, sans-serif;" rows="2" cols="43" maxlength="6"colspan="3" class="code" name="acc_code[]" id="acc_code[]"></textarea><textarea onkeypress="return tabE(this,event)" onkeyup="return check_type()"readonly style="text-align:center;margin-top:-34px;margin-left:20em;font-family: Tahoma, sans-serif;" rows="2" cols="10" maxlength="6"class="type" name="type[]" id="type[]"></textarea><a class="delete" href="javascript:;" title="Remove row">X</a></div></td><td ><textarea  style="font-family: Tahoma, sans-serif;" rows="2" cols="8" maxlength="6" autocomplete="off" name="int_Code[]" id="int_Code[]" class="intCode"></textarea></td><td><textarea onkeypress="return tabE(this,event)"style="font-family: Tahoma, sans-serif;"rows="2" cols="15" maxlength="10" name="acc_deb[]" id="acc_deb[]" autocomplete="off" class="cost"></textarea></td><td style=""><textarea onkeypress="return tabE(this,event)"style="font-family: Tahoma, sans-serif;" rows="2" cols="15" maxlength="9" autocomplete="off" name="acc_cred[]" id="acc_cred[]" class="qty"></textarea></td><td><textarea style="font-family: Tahoma, sans-serif;" rows="2" cols="15" maxlength="10" name="balance[]" id="balance[]" class="price" readonly></textarea></td><td style="background:#A7D6F1;font-family: Tahoma, sans-serif;"><textarea style="font-family: Tahoma, sans-serif;border:none;background-color:white"rows="2" cols="15" maxlength="10" class="deb"name="debitOld[]" id="debitOld[]" ></textarea></td><td style="background:#A7D6F1;font-family: Tahoma, sans-serif;"><textarea style="font-family: Tahoma, sans-serif;border:none;background-color:white"rows="2" cols="15" maxlength="10" class="cred" name="creditOld[]" id="creditOld[]" ></textarea></td></tr>');

PHP code that inserts data into mysql:

    $sql="INSERT INTO trialb ( acc_code,type,int_code,debit_mvt,credit_mvt,balance,debitOld,creditOld,date1,date2,comp_name,user_name,year,status,dt1,dt2 )
VALUES";
for ($i = 0; $i < count($_POST['acc_code']); $i++) {

$sql .= " ('{$_POST['acc_code'][$i]}', '{$_POST['type'][$i]}', '{$_POST['int_code'][$i]}', '{$_POST['debit_mvt'][$i]}','{$_POST['credit_mvt'][$i]}','{$_POST['balance'][$i]}','{$_POST['debitOld'][$i]}','{$_POST['creditOld'][$i]}','{$_SESSION["date1"]}','{$_SESSION["date2"]}','{$_SESSION["name"]}','{$_SESSION["user_name"]}',Now(),'opened','{$_SESSION["date1"]}','{$_SESSION["date2"]}'),";


}
$sql = substr($sql, 0, -1);

Why isn't it inserting all rows???

First of all, your code is not very readable like this, please use proper indentation.

Second, never EVER insert directly from $_POST (or any other input) without any input validation.

Third, what is the value of count($_POST['acc_code']) what is the produced query $sql ? You need information, you need to debug. Use echo's and print_r's to get the debug data, in case of an AJAX call, try Firebug or use error_log to place debug data in you php error log.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM