我有一个问题,我插入记录,它也插入电子邮件部分工作,但当确认页面显示它只显示数组中的最后一条记录3次。我想把它放在for循环中显示所有记录。我不确定我做错了什么会继续搞清楚这个问题。
process_insert.php
<html>
<head>
<title></title>
</head>
<body>
<?php
ini_set('display_errors', 1);
error_reporting(~0);
$serverName = "localhost";
$userName = "root";
$userPassword = "";
$dbName = "blog_samples";
$conn = mysqli_connect($serverName,$userName,$userPassword,$dbName);
$rows_count = count($_POST["name"]);
$message = '';
for($i=0;$i<$rows_count;$i++){
// PREVENTING SQL INJECTION !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
$employee_name = mysqli_real_escape_string($conn,$_POST["employee_name"][$i]);
$name = mysqli_real_escape_string($conn,$_POST["name"][$i]);
$code = mysqli_real_escape_string($conn,$_POST["code"][$i]);
$quantity = intval($_POST["quantity"][$i]);
$price = mysqli_real_escape_string($conn,$_POST["price"][$i]);
$sql = "INSERT INTO order_table ( employee_name, name, code, quantity, price)
VALUES ('$employee_name', '$name', '$code', '$quantity', '$price')";
$query = mysqli_query($conn,$sql);
if(mysqli_affected_rows($conn)>0) {
$message .=
"employee_name: " . $employee_name . "
" ."name: ". $name ."
". "code: " . $code . "
" ."quantity: ". $quantity . "
". "price: " . $price . "";
}
}
if ( ! empty($message)) {
$to = "xgrh@gmail.com";
$subject = "Supplies";
$headers = "From: user@gmail.com";
mail($to,$subject,$message,$headers);
}
?>
<h1 align="center">Supply Request Confirmation</h1>
<p align="center">Thank you, <?php echo $employee_name; ?><br><br>
Your request has been sent.
Please print this page out for your copy.</p>
<div align="center">
<h2>Request Information</h2>
</div>
<table style="width: 45%" align="center">
<tr>
<td class="style">Date Request: <?php $date = new DateTime();
echo $date->format('m/d/Y H:i:s') . "\n"; ?></td>
</tr>
<?php for($i=0;$i<$rows_count;$i++){?>
<tr>
<td class="style">name: <?php echo $name; ?></td>
</tr>
<tr>
<td class="style"> code: <?php echo $code; ?></td>
</tr>
<tr>
<td class="style"> Quantity: <?php echo $quantity; ?></td>
</tr>
<tr>
<td class="style"> price: <?php echo $price; ?></td>
</tr>
<?php } ?>
</table>
<div align="center"><button onClick="window.print()">Print this page</button></div>
</body>
</html>
答案 0 :(得分:1)
这是因为变量$ name,$ code,$ amount和$ price具有$ _POST的最后一个值。您可以使用上一个循环的$ _POST值或使用此值创建变量。
请改为尝试:
<html>
<head>
<title></title>
</head>
<body>
<?php
ini_set('display_errors', 1);
error_reporting(~0);
$serverName = "localhost";
$userName = "root";
$userPassword = "";
$dbName = "blog_samples";
$conn = mysqli_connect($serverName,$userName,$userPassword,$dbName);
$rows_count = count($_POST["name"]);
$message = '';
$data = array();
for($i=0;$i<$rows_count;$i++){
// PREVENTING SQL INJECTION !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
$employee_name = mysqli_real_escape_string($conn,$_POST["employee_name"][$i]);
$name = mysqli_real_escape_string($conn,$_POST["name"][$i]);
$code = mysqli_real_escape_string($conn,$_POST["code"][$i]);
$quantity = intval($_POST["quantity"][$i]);
$price = mysqli_real_escape_string($conn,$_POST["price"][$i]);
array_push($data, array(
'employee_name' => $employee_name,
'name' => $name,
'code' => $code,
'quantity' => $quantity,
'price' => $price,
));
$sql = "INSERT INTO order_table ( employee_name, name, code, quantity, price)
VALUES ('$employee_name', '$name', '$code', '$quantity', '$price')";
$query = mysqli_query($conn,$sql);
if(mysqli_affected_rows($conn)>0) {
$message .=
"employee_name: " . $employee_name . "
" ."name: ". $name ."
". "code: " . $code . "
" ."quantity: ". $quantity . "
". "price: " . $price . "";
}
}
if ( ! empty($message)) {
$to = "xgrh@gmail.com";
$subject = "Supplies";
$headers = "From: user@gmail.com";
mail($to,$subject,$message,$headers);
}
?>
<h1 align="center">Supply Request Confirmation</h1>
<p align="center">Thank you, <?php echo $employee_name; ?><br><br>
Your request has been sent.
Please print this page out for your copy.</p>
<div align="center">
<h2>Request Information</h2>
</div>
<table style="width: 45%" align="center">
<tr>
<td class="style">Date Request: <?php $date = new DateTime();
echo $date->format('m/d/Y H:i:s') . "\n"; ?></td>
</tr>
<?php foreach ($data as $value) {?>
<tr>
<td class="style">name: <?php echo $value['name']; ?></td>
</tr>
<tr>
<td class="style"> code: <?php echo $value['code']; ?></td>
</tr>
<tr>
<td class="style"> Quantity: <?php echo $value['quantity']; ?></td>
</tr>
<tr>
<td class="style"> price: <?php echo $value['price']; ?></td>
</tr>
<?php } ?>
</table>
<div align="center"><button onClick="window.print()">Print this page</button></div>
</body>
</html>