2014-01-05 20 views
0

如何在MySQL中使用PHP和javascript提交多行?如何在mysql中使用php和javascript提交多行

本頁面僅向mysql提交一行數據,我想提交多行。

Live Demo

這是該頁面的PHP:

<form action="multi.php" method="post"> 
<body ng:app> 
    <h2>Shopping Card Example</h2> 
<div ng:controller="CartForm"> 
    <table class="table"> 
     <tr> 

      <th>Description</th> 
      <th>Qty</th> 
      <th>Cost</th> 
      <th>Total</th> 
      <th></th> 
     </tr> 
     <tr ng:repeat="item in invoice.items"> 
      <td><input type="text" ng:model="item.description" name="description" value="" class="input-small"></td>   
      <td><input type="number" ng:model="item.qty" name="qty" value="" ng:required class="input-mini"></td> 
      <td><input type="number" ng:model="item.cost" name="cost" value="" ng:required class="input-mini"></td> 
      <td>{{item.qty * item.cost | currency}}</td> 
      <td> 
       [<a href ng:click="removeItem($index)">X</a>] 
      </td> 
     </tr> 
     <tr> 
      <td><a href ng:click="addItem()" class="btn btn-small">add item</a></td> 
      <td></td> 
      <td>Total:</td> 
      <td>{{total() | currency}}</td> 
     </tr> 
    </table> 
</div> 
    <div align="center"> 
     <input name="submit" type="submit" class="btn btn-small" value="submit"/> 
     &nbsp; 
     <input name="Reset" type="reset" class="btn btn-small" value="Reset"/> 
     </div> 


</form> 

<?php 

function renderForm($description, $qty, $cost, $error) 
{ 
?> 


<?php 
// if there are any errors, display them 
if ($error != '') 
{ 
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>'; 
} 
?> 



<?php 
} 


// connect to the database 
include('connect-db.php'); 

// check if the form has been submitted. If it has, start to process the form and save it to the database 
if (isset($_POST['submit'])) 
{ 
// get form data, making sure it is valid 

$description = mysql_real_escape_string(htmlspecialchars($_POST['description'])); 
$qty = mysql_real_escape_string(htmlspecialchars($_POST['qty'])); 
$cost = mysql_real_escape_string(htmlspecialchars($_POST['cost'])); 

// check to make sure both fields are entered 
if ($qty == '') 
{ 
// generate error message 
$error = 'ERROR: Please fill in all required fields!'; 

// if either field is blank, display the form again 
renderForm($description, $qty,$cost, $error); 
} 
else 
{ 
// save the data to the database 
    mysql_query("INSERT stock SET description='$description', qty='$qty', cost='$cost'") 
or die(mysql_error()); 

// once saved, redirect back to the view page 

} 
} 
else 
// if the form hasn't been submitted, display the form 
{ 
renderForm('','','','','','','',''); 
} 

     ?> 

回答

0

你應該把名字在輸入標籤的空括號,是這樣的:

<td><input type="text" name="description[]" ...></td>   
<td><input type="number" name="qty[]" ...></td> 
<td><input type="number" name="cost[]" ...></td> 

然後,在PHP,你會得到3 陣列,像這樣:

$descriptions = $_POST['description']; 
$qtys = $_POST['qty']; 
$costs = $_POST['cost']; 

則可以遍歷每個陣列的任何其它的PHP陣列

乾杯,從玻利維亞拉巴斯

+0

我改變它,但現在此錯誤顯示警告:用htmlspecialchars()預計參數1爲串,在第139行 – user3162123

+0

和139行的E:\ xampp \ htdocs \ j \ multi.php給出的數組。 $ qtys = mysql_real_escape_string(htmlspecialchars($ _ POST ['qty'])); $ costs = mysql_real_escape_string(htmlspecialchars($ _ POST ['cost'])); – user3162123

+0

您必須先遍歷數組,然後對每個數組項使用htmlspecialchars和您的邏輯。 –