1

I want to add some static row and multiple row same time into mysql as well as invoice. I can add row into mysql. But when I add multiple row then insert first row all field fully but not insert all field from other row , only insert dynamic field into mysql. For example specification ,qty insert well but pur ,appb,date,appn not insert from second row. Could any one help me. My code are below...

<form action="ps.php"  method="POST"> 
      <table id="customers">
      <tr>
      <td>Project/Purpose</td><td><input type="text" name="pur[]" required="required"  /></td>
      <td>Approved Budget:</td><td><input type="text" name="appb[]" required="required"  /></td>
      </tr>
       <tr>
      <td>Expected Delivery Date:</td><td><input type="date" name="expdate[]"  required="required" /></td>
       </tr>

      <td>Approve by:</td><td><input type="text" name="appn[]"  required="required" /></td>
      </tr>
      </table>

      <fieldset style="background-color:#00FFFF"><h2>Purchase Information</h2></fieldset>


   <div id="page-wrap">
  <table id="customers">

          <tr>
              <th>Goods/Services(include description and specifications)</th>
              <th>Qty Required(Goods only)</th>
              <th>Qty on Hand(Goods only)</th>            
              <th>Qty to Order</th>
              <th>Est. Unit Price</th>
              <th>Total Estimated Cost</th>
          </tr>

          <!-- add remove row dynamically -->  
          <tr class="item-row">
              <td class="item-name"><div class="delete-wpr"><a class="delete" href="javascript:;" title="Remove row">X</a><textarea class="noscrollbars" name="specification[]" onkeyup="autoGrow(this);"></textarea></div></td>
              <td ><input type="number" name="" /></td>
              <td ><input type="number" name="" /></td>
              <td><input type="number" class="qty" name="qty[]" /></td>
              <td><input type="number" class="cost" name="esp[]" /> </td>

              <td><span class="price">0.00</span></td>
          </tr>

           <!-- add remove row dynamically -->

           <tr class="item-row">
              <td class="item-name"><div class="delete-wpr"><a class="delete" href="javascript:;" title="Remove row">X</a><textarea class="noscrollbars" name="specification[]" onkeyup="autoGrow(this);"></textarea></div></td>
              <td ><input type="number" name="" /></td>
              <td ><input type="number" name="" /></td>
              <td><input type="number" class="qty" name="qty[]" /></td>
              <td><input type="number" class="cost" name="esp[]" /> </td>
              <input type="hidden" name="empid[]" value='<?php echo $empid;?>' />
              <td><span class="price">0.00</span></td>

          </tr>

          <tr id="hiderow">
            <td colspan="6"><a id="addrow" href="javascript:;" title="Add a row">Add a row</a></td>
          </tr>

          <tr>
              <td colspan="4" class="blank"> </td>
              <td colspan="1" class="total-line">Total</td>
              <td class="total-value"><div id="total">0.00</div></td>
          </tr>
          <tr>
              <td colspan="4" class="blank"> </td>
              <td colspan="1" class="total-line"><input type="submit" name="submit" value="Submit" /></td>

              <td class="blank"><textarea id="paid" style="display:none;" ></textarea></td>
          </tr>

      </table>



    </div>



  </form>

ps.php

if(isset($_POST)==true && empty($_POST)==false): 
                $pur = $_POST['pur'];           
                $appb=$_POST['appb'];
                $expdate=$_POST['expdate'];         

                $appn=$_POST['appn'];
                $specification=$_POST['specification'];                 
                $qty=$_POST['qty'];
                $esp=$_POST['esp'];                             
                $empid=$_POST['empid']; 

                foreach($qty as $a => $b){                   



                    $result = mysqli_query($mysqli, "INSERT INTO `pr`(`pur`, `empid`, `appb`, `expdate`, `appn`, `specification`, `qty`, `esp`) VALUES ('$pur[$a]','$empid[$a]','$appb[$a]','$expdate[$a]','$appn[$a]','$specification[$a]','$qty[$a]','$esp[$a]')");       

                    } 

                    echo "Your entry has been successfully recorded, Thank you!";       
            else:  echo "Some things went wrong please try again";  
            endif;
Ohidul Islam
  • 45
  • 1
  • 9
  • 1
    learn about prepared Statements to prevent SQL injection – Jens Aug 16 '17 at 11:00
  • 1
    For one thing, your code is wide open to SQL injection so you could be trying to execute just about anything in that SQL statement. Which leads to the point... When the query fails, what is the query? And what is the error from MySQL? You're currently not checking either of these things. – David Aug 16 '17 at 11:02

2 Answers2

1

Try This:

$qry  = "INSERT INTO `pr`(`pur`, `empid`, `appb`, `expdate`, `appn`, `specification`, `qty`, `esp`) VALUES ";
$i = 0;
foreach($qty as $a => $b) {                   

    $qry .= "('$pur[$a]','$empid[$a]','$appb[$a]','$expdate[$a]','$appn[$a]','$specification[$a]','$qty[$a]','$esp[$a]')";
    if($i == count($qty)) {
        $qry .= ";";
    } else {
        $qry .= ",";
    }

$i++;
} 

$result = mysqli_query($mysqli, $qry);

If you have large data then use insert query once, It will increase efficiency.

er.irfankhan11
  • 1,280
  • 2
  • 16
  • 29
  • This unstable/insecure querying technique should no be used by anyone. A prepared statement with placeholders should be used. – mickmackusa Aug 15 '20 at 13:38
0

Just make the with the field name and use implode function while inserting data.

For example:

    $data['name'] = 'abc';
    $data['description'] = 'description';
    $data['created'] = date("Y-m-d H:i:s");

Query:

     $fields = array_keys($data);
        $sql = "INSERT INTO table_name . (`" . implode('`,`', $fields) . "`) 
                VALUES('" . implode("','", $data) . "')";
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Priyank
  • 470
  • 2
  • 11
  • This unstable/insecure querying technique should no be used by anyone. A prepared statement with placeholders should be used. – mickmackusa Aug 15 '20 at 13:38