gpt4 book ai didi

javascript - 如何使用php和javascript在mysql中提交多行

转载 作者:行者123 更新时间:2023-11-30 00:51:33 25 4
gpt4 key购买 nike

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

该页面只向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('','','','','','','','');
}

?>

最佳答案

您应该在输入标签中放置一个带有空括号的名称,例如:

<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 数组一样遍历每个数组

干杯,来自玻利维亚拉巴斯

关于javascript - 如何使用php和javascript在mysql中提交多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20931574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com