2017-05-21 1 views
0

Ich habe Formular, wenn ich auf Senden klicken, zeigen Sie alle Daten Zeile für Zeile , die alle funktionieren gut, aber mein Problem ist, wie kann ich diese alle Wert zur mysql Datenbank hinzufügen, wenn klicken Sie auf "submit_to_database " Taste. Bitte kann mir helfen, dies ist mein vollständiger CodeEinfügen von Daten in MySQL-Datenbank

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> 
 
<link rel="stylesheet" href="custom.css" type="text/css"> 
 

 
<html lang="en"> 
 
<head> 
 
\t <meta charset="utf-8"> 
 
\t <title>Add Sales</title> 
 

 
\t <script type="text/javascript"> 
 
\t function add_values(){ 
 
    if(document.getElementById('edit_guid').value==""){ 
 
    if(document.getElementById('stk').value!=""){ 
 
     
 
    if(document.getElementById('stk').value!=0){ 
 
        
 
    sell=document.getElementById('pric').value; 
 
    disc=document.getElementById('stk').value; 
 
    item=document.getElementById('guid').value; 
 
    roll=parseInt(document.getElementById('roll_no').value); 
 
\t $('<tr id='+item+'><td><lable id='+item+'roll >'+roll+'</label></td><td><input type=text readonly="readonly" value='+sell+'></td><td><input type=text readonly="readonly" value='+disc+' ></td></tr>').fadeIn("slow").appendTo('#item_copy_final'); 
 
    document.getElementById('stk').value=""; 
 
    document.getElementById('pric').value=""; 
 
    document.getElementById('roll_no').value=roll+1; 
 
    document.getElementById('guid').value=""; \t 
 
} 
 
}else{ 
 
    alert('Please Select An Item'); 
 
    }}} 
 
     </script> 
 
     <body> 
 
\t \t \t \t <form name="form1" method="post" id="form1" action="">     
 
        <input type="hidden" id="roll_no" value="1" > \t 
 
       <div align="center"> 
 
        <input type="hidden" id="guid"> 
 
        <input type="hidden" id="edit_guid"> 
 
         
 
        <table class="form" > 
 
\t \t \t \t \t <tr> 
 
          <td>price</td> \t \t \t \t \t \t 
 
          <td>stk</td> 
 
         </tr> 
 
\t \t \t <tr> 
 
\t \t \t <td><input type='text' class='form-control' id="pric" name="pric"></td> 
 
\t \t \t <td><input type='text' class='form-control' id="stk" name="stk"></td> 
 
\t \t \t <td><input type="button" onclick="add_values()" id="add_new_code" value="submit" class="round"></div></form></td></tr> 
 
      </table> 
 
\t \t \t \t <div style="overflow:auto ;max-height:300px; "> 
 
        <table class="form" id="item_copy_final" style="margin-left:45px "></table> 
 
        </div> 
 
        </div> 
 
     <div class="mytable_row "> 
 
\t \t <form> 
 
\t \t <div align="center"> 
 
\t \t <table> 
 
\t \t <td><input type="button" value="submit_to_database" class="round"></td> 
 
\t \t </table> 
 
\t \t </div> 
 
\t \t </form> 
 
    
 
</body> 
 
</html>

Antwort

0

Aktualisieren Sie Ihr Code -

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> 
<link rel="stylesheet" href="custom.css" type="text/css"> 

<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title>Add Sales</title> 

    <script type="text/javascript"> 
    function add_values(){ 
     if(document.getElementById('edit_guid').value==""){ 
     if(document.getElementById('stk').value!=""){ 

      if(document.getElementById('stk').value!=0){ 

      sell=document.getElementById('pric').value; 
      disc=document.getElementById('stk').value; 
      item=document.getElementById('guid').value; 
      roll=parseInt(document.getElementById('roll_no').value); 
      $('<tr id='+item+'><td><lable id='+item+'roll >'+roll+'</label></td><td><input type=text name="price[]" readonly="readonly" value='+sell+'></td><td><input type=text name="stk[]" readonly="readonly" value='+disc+' ></td></tr>').fadeIn("slow").appendTo('#item_copy_final'); 
      document.getElementById('stk').value=""; 
      document.getElementById('pric').value=""; 
      document.getElementById('roll_no').value=roll+1; 
      document.getElementById('guid').value=""; 
      $("#pric").focus(); 
      } 
     } 
     else{ 
      alert('Please Select An Item'); 
     } 
     } 
    } 
    </script> 
</head> 
<body> 
    <form name="form1" method="post" id="form1" action="">     
    <input type="hidden" id="roll_no" value="1" >  
    <div align="center"> 
     <input type="hidden" id="guid"> 
     <input type="hidden" id="edit_guid"> 

     <table class="form" > 
     <tr><td>price</td><td>stk</td><td>&nbsp;</td></tr> 
     <tr> 
      <td><input type='text' class='form-control' id="pric" name="pric"></td> 
      <td><input type='text' class='form-control' id="stk" name="stk"></td> 
      <td><input type="button" onclick="add_values()" id="add_new_code" value="submit" class="round"></td> 
     </tr> 
     </table> 
    </div> 
    </form> 
    <div style="overflow:auto ;max-height:300px;" align="center"> 
    <form method="post" action="test.php"> 
     <table class="form" id="item_copy_final" style="margin-left:45px "></table> 
     <table> 
     <tr><td><input type="submit" name="submit_to_database" value="submit_to_database" class="round"></td></tr> 
     </table> 
    </form> 
    </div> 

</body> 
</html> 

eine PHP-Skript Seite Schreiben, in dem Sie Ihre Daten veröffentlichen und fügen Sie die Wert in der MySQL-Datenbank.

"test.php"

<?php 
mysql_connect("hostname", "database_user", "database_password") or die("Could not connect: " . mysql_error()); 
mysql_select_db("database_name"); 

if(isset($_POST["submit_to_database"])){ 
    for($i=0; $i<count($_POST["price"]); $i++){ 
    mysql_query("INSERT INTO `your_table` SET `your_price_field` = '".$_POST["price"][$i]."', `your_stk_field` = '".$_POST["stk"][$i]."'"); 
    } 
} 
?> 
+0

Lassen Sie uns nicht lehren/propagieren schlampig und gefährlichen Praktiken Codierung. Wenn Sie eine Antwort ohne vorbereitete Anweisungen posten [möchten Sie dies möglicherweise vor dem Posten berücksichtigen] (http://meta.stackoverflow.com/q/344703/). Zusätzlich [eine wertvollere Antwort kommt von der OP die richtige Methode] (https://meta.stackoverflow.com/a/290789/1011527) –

+0

Vielen Dank sir.it's Arbeit für mich..danke Ihnen –