我正在尝试做一个简单的订购系统。其中服务已经在数据库中,但是如果手动输入了执行该服务的人员的名称。我能够成功保存订单但是参加客户的人的姓名,但是我遇到麻烦,如果我在一个约会中有多个服务,那么只保存我输入名字后的第一个名字,而不是保存到我的名字中数据库。这是我正在研究的功能
function ConfirmOrder(){
var orders = {};
if($('.food').length>0){
var checkbox = 0;
$('tr.item').each(function(){
var order_id = $(this).attr('id');
var quantity1 = $(this).find('.quantity').text();
var name = $('#name').val();
orders[order_id] = quantity1;
})
console.log(orders);
if ($('#senior-checkbox').prop('checked')) {
checkbox = 1;
}
else{
checkbox = 0;
}
}
else{
alert("No orders taken");
}
$.ajax({
url: "confirm_order.php",
type: "POST",
async: true,
cache: false,
data: {orders:orders,checkbox:checkbox,name},
success: function(html){
}
})
这是我如何选择服务并输入名称
<?php
include 'dbconnection.php';
$menu_id = $_POST['menu_id'];
$query = mysql_query("SELECT * FROM menu WHERE menu_id=$menu_id");
echo "SELECT * FROM menu WHERE menu_id=$menu_id";
while ($row = mysql_fetch_array($query)){
?>
<tr id="<?php echo $row['menu_id'];?>" class="item">
<td class="food noprint" id="<?php echo $row['menu_id'];?>">
<input name="checkbox[]" type="checkbox" value="<?php echo $row['menu_id'];?>">
</td>
<td><?php echo $row['dish'];?></td>
<td class="food_price_column" id="<?php echo $row['menu_id'];?>">
<span>
<span class="btn btn-success noprint" onclick="minusQuantity(<?php echo $row['menu_id'];?>, <?php echo $row['price']; ?>)"><i class="fa fa-minus"></i></span>
<span class="quantity order-quantity<?php echo $row['menu_id'];?>">1</span>
<span class="btn btn-success noprint" onclick="addQuantity(<?php echo $row['menu_id'];?>, <?php echo $row['price']; ?>)"><i class="fa fa-plus"></i></span></span>
</td>
<td><?php echo $row['price'];?></td>
<td class="price" id="<?php echo $row['menu_id'];?>"><?php echo $row['price'];?></td>
<td><input type="text" name="name" id="name"></td>
</td></tr>
<?php } ?>
这就是我将它保存到数据库的方式
<?php
include 'dbconnection.php';
session_start();
$user_id = $_SESSION['user_id'];
if(isset($_POST['orders'])){
$checkbox = $_POST['checkbox'];
$order_id = $_POST['orders'];
$fullname= "";
$last_ids = [];
$name = $_POST['name'];
//alert($name);
//Query the cashier's name to be inserted to sales table
$user_query = mysql_query("SELECT full_name FROM account WHERE acct_id = $user_id");
while($row = mysql_fetch_array($user_query)){
$fullname = $row['full_name'];
}
//insert orders into orders table
$insert = mysql_query("INSERT INTO sales VALUES ('','$fullname',now(),$checkbox)");
if($insert){
$last_id = mysql_insert_id();
}
foreach($order_id as $key => $value) { //$key = dish_id and $value = quantity
$dish_query = mysql_query("SELECT dish FROM menu WHERE menu_id = $key");
while($dish = mysql_fetch_array($dish_query)){
$dish = $dish['dish'];
/*$name = $_POST['name'];*/
$order = mysql_query("INSERT INTO orders VALUES ('', '$dish','$value','$last_id','$name')");
$query1 = mysql_query("INSERT into a_logs(id,name,activity,a_date) values(0,'$fullname','Added $dish',NOW())");
$recent_order_id = mysql_insert_id();
}
}
}
?>
答案 0 :(得分:0)
这是您需要检查的代码部分:
</td>
<td><?php echo $row['price'];?></td>
<td class="price" id="<?php echo $row['menu_id'];?>"><?php echo $row['price'];?></td>
<td><input type="text" name="name" id="name"></td>
</td>
</tr>
当您使用while循环创建输入标记时,每个标记都将具有相同的名称和ID。因此,当您使用id获取数据时,每次只会给您一个值。
您可以使用以下内容:创建值数组并访问它们
while(-->condition<--){
</td>
<td><?php echo $row['price'];?></td>
<td class="price" id="<?php echo $row['menu_id'];?>"><?php echo $row['price'];?></td>
<td><input type="text" name="name[]" ></td>
</td>
</tr>
}
使用php访问值:
$name = $_POST['name'];
foreach( $name as $v ) {
print $v;
}