我正在为我的项目创建发票。共有3页。 我是数据库和php的新手(只有1个月的时间通过这2个主题崩溃)所以请原谅我,如果我的代码/或数据库逻辑不足。
对于所有3个页面,它们已经连接到数据库,我取出了文件名,以免混乱代码块。
第一页是发票的表格。用户键入他/她的名字,然后键入描述,金额,数量和折扣变量。单击“提交”将转到第2页。
如果在page1中输入了多行输入,则第二页更多是循环变量的后端处理。然后它将所有这些输入解析为一个名为invoicesub的表。它还将客户名称编码到底部重定向链接的URL中。
第3页是我想要显示所有这些变量的地方,例如客户名称,描述,数量,金额,总额,表格中的折扣,还要在表格invoicesub中输入总变量的总和。我还想将打印变量的所有变量重新插入到名为invoicefinal的同一数据库中的另一个表中。
第一页(表格)
<script type="text/javascript">
var count = 0;
function addTextArea(){
count= count+1;
var div = document.getElementById('name');
div.innerHTML += "<div> <input type='text' name='name[]' value='' "+"id=name"+count+"> </div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('quantity');
div.innerHTML += "<div><input type='text' name='quantity[]' value ='' "+"id=quantity"+count+"></div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('amount');
div.innerHTML += "<div><input type='text' name='amount[]' value ='' "+"id=amount"+count+"></div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('discount');
div.innerHTML += "<div><input type='text' name='discount[]' value ='' "+"id=discount"+count+"></div>";
div.innerHTML += "\n<br />";
}
function removeTextArea(){
document.getElementById("name"+count).remove();
document.getElementById("quantity"+count).remove();
document.getElementById("amount"+count).remove();
document.getElementById("discount"+count).remove();
count = count-1;
}
</script>
</head>
<body>
<form action="invoicesubmit.php" method="POST">
<?php
echo "<table border='2'>\n";
echo "<tr>\n";
echo "<th>Description</th>\n";
echo "<th>Quantity</th>\n";
echo "<th>Amount($)</th>\n";
echo "<th>Discount(%)</th>\n";
echo "</tr>";
echo "<tr>";
echo "<td>"?><input type='text' size="50" name='name[]' value='Examination and Consultation' readonly/><?php "</td>";
echo "<td>"?><input type='text' size="50" name='quantity[]' value='' /><?php "</td>";
echo "<td>"?><input type='text' size="50" name='amount[]' value='' /><?php "</td>";
echo "<td>"?><input type='text' size="50" name='discount[]' value='' /><?php "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>"?><div id="name"></div> <?php "</td>";
echo "<td>"?><div id="quantity"></div> <?php "</td>";
echo "<td>"?><div id="amount"></div> <?php "</td>";
echo "<td>"?><div id="discount"></div> <?php "</td>";
echo "</tr>";
?>
Customer Name:
<br />
<input type="text" name="cust_name" value="" />
<br />
<input type="button" value="Add Description" onClick="addTextArea();">
<input type="button" value="Remove Description" onClick="removeTextArea();">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
第二页(将变量插入表invoicesub)
if (isset($_POST['submit'])){ // Process the form
$name_array = $_POST['name'];
$quantity_array = $_POST['quantity'];
$amount_array = $_POST['amount'];
$discount_array = $_POST['discount'];
$cust_name_array = mysql_prep( $_POST['cust_name']);
for ($i = 0; $i < count($name_array); $i++){
$cust_name = $cust_name_array;
$name = $name_array[$i];
$quantity = $quantity_array[$i];
$amount = $amount_array[$i];
$discount = $discount_array[$i];
$total_amt = ($amount - ($amount * ($discount / 100))) * $quantity;
global $connection;
$query = "INSERT INTO invoicesub (";
$query.= " cust_name, description, quantity, amount, discount, total";
$query.= ") VALUES (";
$query.= " '{$cust_name}', '{$name}', {$quantity}, {$amount}, {$discount}, {$total_amt}";
$query.= ")";
$result = mysqli_query($connection, $query);
}
redirect_to("invoicesubmitfinal.php?cname=".urlencode($cust_name));
}
第3页(试图总变量)
<?php
echo "<table border='1'>\n";
echo "<tr>\n";
echo "<th>Description</th>\n";
echo "<th>Quantity</th>\n";
echo "<th>Amount($)</th>\n";
echo "<th>Discount(%)</th>\n";
echo "<th>Total_amt</th>\n";
echo "</tr>";
global $connection;
$sql1="SELECT sum(amount) as amount, sum(quantity) as quantity, sum(total) as total FROM invoicesub where cust_name=$name";
$result2 = mysqli_query($connection, $sql1) or die(mysqli_error());
while ($rows = mysql_fetch_array($result2)){
echo "<tr>";
echo "<td>" . $rows['quantity'] . "</td>";
echo "<td>" . $rows['amount'] . "</td>";
echo "<td>" . $rows['discount']. "%" . "</td>";
echo "<td>" ."$". $rows['total'] . "</td>";
echo "<td>" . "$" . $total_amt . "</td>";
echo "</tr>";
echo $rows['amount'];
echo $rows['quantity'];
echo $rows['total'];
}
?>
答案 0 :(得分:0)
您需要使用返回每一行的查询。您可以在打印详细记录时计算总计:
$det_sql = "SELECT description, quantity, amount, discount, total
FROM invoicesub
WHERE cust_name = ?";
$det_stmt = mysqli_prepare($connection, $det_sql);
mysqli_stmt_bind_param($det_stmt, "s", $name);
mysqli_stmt_execute($det_stmt);
mysqli_stmt_bind_result($det_stmt, $description, $quantity, $amount, $discount, $total);
$total_quantity = $total_amount = $total_total = 0;
while (mysqli_stmt_fetch($det_stmt)) {
echo "<tr><td>$description</td><td>$quantity</td><td>\$$amount</td><td>$discount%</td><td>\$$total</td></tr>";
$total_quantity += $quantity;
$total_amount += $amount;
$total_total += $total;
}
echo "<tr><td>Totals:</td><td>$total_quantity</td><td>\$$total_amount</td><td></td><td>\$$total_total</td></tr>";