Web Analytics Made Easy -
StatCounter Sending Dynamic Form Data To Mysql via PHP - CodingForum

Announcement

Collapse
No announcement yet.

Sending Dynamic Form Data To Mysql via PHP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Sending Dynamic Form Data To Mysql via PHP

    I have created a dynamic form and i am trying to store the form data to mysql with php. But data doesnt get saved to database. I am posting my all codes below. I want to know how can i solve this problem, or where are my faults and how can i have an accurate form.. I am new to this and out of ideas.

    Here is my form code:

    Code:
    <form name="newbillform" method="POST" action="save_purchase_details.php">
       <table style=" border:1px solid black" cellpadding="5px" cellspacing="0px"
       align="center" border="0">
       <tr>
       <td colspan="4" style="background:#0066FF; color:#FFFFFF; fontsize:
       20px" align="center">ADD NEW PURCHASE RECORD</td></tr>
       <tr>
       <td>Date:</td>
       <td><input type="date" name="p_date"/></td></tr>
       <tr>
       <td>Invoice Number:</td>
       <td><input type="text" name="invoice_no" size="50"></td></tr>
       <tr>
       <td>Balance:</td>
       <td><input type="text" name="balance" size="50"></td></tr>
       </table>
    <h2 style="padding-left:10px;">Enter Product Details Below:-</h2>
    <table id="product_details" style="margin-top:8px;" align='center' border='1' width="900px">
       <tr id="row1">
       <td><input type="text" name="qty[]" placeholder="Quantity" size="6"></td>
       <td><input type="text" name="pack[]" placeholder="Pack" size="6"></td>
       <td><input type="text" name="item_name[]" placeholder="Item Name" size="16"></td>
       <td><input type="text" name="batch[]" placeholder="Batch" size="6"></td>
       <td><input type="text" name="expiry[]" placeholder="Expiry" size="6"></td>
       <td><input type="text" name="mrp[]" placeholder="M.R.P" size="6"></td>
       <td><input type="text" name="rate[]" placeholder="Rate" size="6"></td>
       <td><input type="text" name="vat[]" placeholder="VAT" size="6"></td>
       <td><input type="text" name="discount[]" placeholder="Discount" size="6"></td>
       <td><input type="button" class="button-add-row" onClick="add_row();" value="ADD ROW" size="8"></td>
       </tr>
       </table>
        <center>
          <input type="submit" name="submit_row" value="SUBMIT">
       </center>
      </form>
    Here is the javascript code...

    Code:
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
    function add_row()
    {
    $rowno=$("#product_details tr").length;
     $rowno=$rowno+1;
     $("#product_details tr:last").after("<tr id='row"+$rowno+"'><td><input type='text' name='qty[]' placeholder='Quantity' size='6'></td><td><input type='text' name='pack[]' placeholder='Pack' size='6'></td><td><input type='text' placeholder='Item Name' name='item_name[]' size='16'></td><td><input type='text' name='batch[]' placeholder='Batch' size='6'></td><td><input type='text' name='expiry[]' placeholder='Expiry' size='6'></td><td><input type='text' name='mrp[]' placeholder='M.R.P' size='6'></td><td><input type='text' name='rate[]' placeholder='Rate' size='6'></td><td><input type='text' name='vat[]' placeholder='VAT' size='6'></td><td><input type='text' name='discount[]' placeholder='Discount' size='6'></td><td><input type='button' class='button-add-row' value='DELETE' onclick=delete_row('row"+$rowno+"')></td></tr>");
    }
    function delete_row(rowno)
    {
     $('#'+rowno).remove();
    }
    </script>
    Here is the PHP code...

    Code:
    [PHP]
    <?php
    $connect = mysql_connect("localhost","root","") or die(mysql_error());
    mysql_select_db("store_records",$connect) or die(mysql_error());
    
    if(isset($_POST['submit_row']))
    {
    
    for($i=0;$i<count($_POST['item_name']);$i++)
    {
    $amount;
    $grand_total;
    $p_date=$_POST['p_date'];
    $invoice_no=$_POST['invoice_no'];
    $balance=$_POST['balance'];
    $qty=$_POST['qty'][$i];
    $pack=$_POST['pack'][$i];
    $item_name=$_POST['item_name'][$i];
    $batch=$_POST['batch'][$i];
    $expiry=$_POST['expiry'][$i];
    $mrp=$_POST['mrp'][$i];
    $rate=$_POST['rate'][$i];
    $vat=$_POST['vat'][$i];
    $discount=$_POST['discount'][$i];
    $amount=$balance+($qty*$rate)-$discount;
    $grand_total=$amount+(($amount*$vat)/100);
    
     $query =mysql_query("insert into bill_records values('', '$p_date', '$invoice_no', '$balance', '$qty','$pack','$item_name', '$batch', '$expiry', '$mrp', '$rate', '$vat', '$discount', '$amount', '$grand_total')");	 
     }
    }
    
    ?>[/PHP]
    It would be of great help.. Thank you!

  • #2
    Why put your code in php tags and then put that in code tags? - You've just made it pretty hard to read your php code.
    "Tango says double quotes with a single ( ' ) quote in the middle"
    '$Name says single quotes with a double ( " ) quote in the middle'
    "Tango says double quotes ( \" ) must escape a double quote"
    '$Name single quotes ( \' ) must escape a single quote'

    Comment


    • #3
      After the mysql_query add
      var_dump(mysql_errror());
      That will give you the mysql error.

      Is this a hobby project? If not then you have some serious security holes in there.
      Don't use old mysql library
      The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets
      When a white horse is not a horse

      Comment

      Working...
      X