从数据库中获取值并显示在 html 表中,包括 PHP
Posted
技术标签:
【中文标题】从数据库中获取值并显示在 html 表中,包括 PHP【英文标题】:Fetching values from database and display in html table including PHP 【发布时间】:2017-12-05 17:23:35 【问题描述】:我有一个表格,用户在其中输入数据。行是动态创建的。用户给定的值被发送到数据库(Oracle 11g),并基于该值提取其他值并将其插入表中。代码是这样的:
<!DOCTYPE html>
<html>
<body>
<?php
$code = $_POST['code'];
$qty = $_POST['qty'];
foreach ($_POST['code'] as $code => $c)
//print $c . " " . $qty[$code] . "<br>";
$link = oci_connect('hd','hd', 'localhost/mydb');
if(!$link)
$e = oci_error();
exit('Connection error ' . $e['message']);
foreach ($_POST['code'] as $code => $c)
$q1 = "select PROD_NAME, PROD_COST from PRODUCT where PROD_ALIAS = :bv_code";
$q1parse = oci_parse($link, $q1);
oci_bind_by_name($q1parse, ':bv_code', $c);
oci_execute($q1parse);
while($row = oci_fetch_array($q1parse))
PRINT $row['PROD_NAME'] . " " . $row['PROD_COST'] .
" " . $qty[$code] . " " .
($row['PROD_COST']*$qty[$code]) . "<br>";
?>
<script type = "text/javascript" >
function addRow()
var table = document.getElementById('order');
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
cell1.innerHTML = "<input type = 'text' name = 'code[]' /> ";
cell2.innerHTML = "";
cell3.innerHTML = "";
cell4.innerHTML = "<input type = 'text' name = 'qty[]' />";
cell5.innerHTML = "";
function delRow(r)
document.getElementById('order').deleteRow(-1);
</script>
<p><strong> Order Details </strong></p>
<form action = 'm3.php' method = 'POST' >
<table id = "order" border = 1 border_collapse = "collapse" >
<tr>
<th> Item Code </th>
<th> Name </th>
<th> Price </th>
<th> Quantity </th>
<th> Total </th>
</tr>
</table>
<input type = 'submit' name = 'submit' value = 'Submit' />
<input colspan = '2' type = "button" value = "Add Row" onclick = "addRow('order')" />
<input colspan = '2' type = "button" value = "Delete Row" onclick = "delRow('order')" />
</form>
</body>
</html>
用户提供“CODE”和“Quantity”的值。 “代码”被发送到数据库并获取产品的其他值。问题是我想在同一个表中显示获取的值。现在我把它展示在桌子外面。另外,我希望最后有一列显示总账单。如,(价格*数量)的总和。我该怎么做?
【问题讨论】:
【参考方案1】:不要将整个 foreach
循环放置在页面中,而应将其放置在 <table> ... </table>
中,如下所示:
// your code
<table id = "order" border = 1 border_collapse = "collapse" >
<tr>
<th> Item Code </th>
<th> Name </th>
<th> Price </th>
<th> Quantity </th>
<th> Total </th>
</tr>
<?php
$sumTotal = 0;
foreach ($_POST['code'] as $code => $c)
$q1 = "select PROD_NAME, PROD_COST from PRODUCT where PROD_ALIAS = :bv_code";
$q1parse = oci_parse($link, $q1);
oci_bind_by_name($q1parse, ':bv_code', $c);
oci_execute($q1parse);
while($row = oci_fetch_array($q1parse))
?>
<tr>
<td><?php echo $c; ?></td>
<td><?php echo $row['PROD_NAME']; ?></td>
<td><?php echo $row['PROD_COST']; ?></td>
<td><?php echo $qty[$code]; ?></td>
<td><?php echo $row['PROD_COST']*$qty[$code]; ?></td>
</tr>
<?php
$sumTotal += $row['PROD_COST']*$qty[$code];
?>
<tr>
<td>Sum Total</td>
<td colspan="4"><?php echo $sumTotal; ?></td>
</tr>
</table>
// your code
【讨论】:
哦,太好了。它有效,但如何计算总数?和总量一样? @KaranGupta 我已经更新了我的答案以解决您的评论,即适应总额计算。希望这能解决您的问题。 它没有给出想要的结果。我收到一个错误“第 70 行中的未定义变量 sumTotal”,即我们声明“sumTotal”的行。但它也显示了结果,虽然不在表格中 ! ) 注意:未定义变量:sumTotal in E:\xampp\htdocs\myfiles\m3.php on line 70 Call Stack # Time Memory Function Location 1 0.0005 140960 main( ) ...\m3.php:0 我修正了错误,你没有放“;”在声明 sumTotal 时。但它仍然给出了上述错误。以上是关于从数据库中获取值并显示在 html 表中,包括 PHP的主要内容,如果未能解决你的问题,请参考以下文章
如何从下拉列表中获取值并插入 MySQL Workbench?