在 PHP/MySQL 中使用 SELECT SUM WHERE
Using SELECT SUM WHERE in PHP/MySQL
我正在用 MySQL 学习 PHP 并尝试使用 SELECT SUM 得到第 'QuantityChecked' 列的总数,但只有 'Lot' ] 匹配然后显示它。在这种情况下,我提供了一些我正在使用的代码,如果我还需要定义这个问题或代码,请告诉我。我现在遇到的问题是 'Lot' 似乎没有从 qcapproval.php 转移到 qcapproval-exec.php 中,我从以下错误消息
未定义索引:qcapproval 中的批次-exec.php
下面的代码。
qcapproval.php
<form action="qcapproval-exec.php?id=<?php echo $row['id']; echo $row['Lot']; ?>" method="post">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
<input type="hidden" name="Lot" value="<?php echo $row['Lot']; ?>" />
<input type="hidden" name="CheckedBy" value="<?php echo $_SESSION['CheckedBy']; ?>" />
qcapproval-exec.php
$conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('there was a problem connecting to the database' . mysql_error());
session_start();
if (isset($_POST['submit']))
{
$QuantityChecked= $_POST['QuantityChecked'];
$AnodThickness= $_POST['AnodThickness'];
$QtyPassed= $_POST['QtyPassed'];
$QtyFailed= $_POST['QtyFailed'];
$SealsChecked= $_POST['SealsChecked'];
$SealsPassed= $_POST['SealsPassed'];
$SealsFailed= $_POST['SealsFailed'];
$GraphicsChecked= $_POST['GraphicsChecked'];
$GraphicsPassed= $_POST['GraphicsPassed'];
$GraphicsFailed= $_POST['GraphicsFailed'];
$CheckedBy= $_POST['CheckedBy'];
$id = $_GET['id'];
$Lot = $_GET['Lot'];
// OR
// $id = $_POST['id'];
$sql = "UPDATE logs SET QuantityChecked= '$QuantityChecked',
AnodThickness = '$AnodThickness',
QtyPassed = '$QtyPassed',
QtyFailed = '$QtyFailed',
SealsChecked = '$SealsChecked',
SealsPassed = '$SealsPassed',
SealsFailed = '$SealsFailed',
GraphicsChecked = '$GraphicsChecked',
GraphicsPassed = '$GraphicsPassed',
GraphicsFailed = '$GraphicsFailed',
CheckedBy = '$CheckedBy',
DateTimeChecked = now()
WHERE id = $id ";
$conn->query($sql);
$result = mysqli_query($conn, "SELECT SUM(QuantityChecked) AS TotalQuantityChecked FROM logs WHERE Lot = $Lot");
$row= mysqli_fetch_assoc($result);
$sum= $row['TotalQuantityChecked'];
$conn->close();
}
echo $sum;
echo "Record Updated.";
我已进行编辑以反映新代码和对此问题的更改
您正在使用 POST
提交表单,因此隐藏元素的值将在 $_POST
中,而不是 $_GET
。
$Lot = $_POST['Lot'];
我正在用 MySQL 学习 PHP 并尝试使用 SELECT SUM 得到第 'QuantityChecked' 列的总数,但只有 'Lot' ] 匹配然后显示它。在这种情况下,我提供了一些我正在使用的代码,如果我还需要定义这个问题或代码,请告诉我。我现在遇到的问题是 'Lot' 似乎没有从 qcapproval.php 转移到 qcapproval-exec.php 中,我从以下错误消息
未定义索引:qcapproval 中的批次-exec.php
下面的代码。
qcapproval.php
<form action="qcapproval-exec.php?id=<?php echo $row['id']; echo $row['Lot']; ?>" method="post">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
<input type="hidden" name="Lot" value="<?php echo $row['Lot']; ?>" />
<input type="hidden" name="CheckedBy" value="<?php echo $_SESSION['CheckedBy']; ?>" />
qcapproval-exec.php
$conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('there was a problem connecting to the database' . mysql_error());
session_start();
if (isset($_POST['submit']))
{
$QuantityChecked= $_POST['QuantityChecked'];
$AnodThickness= $_POST['AnodThickness'];
$QtyPassed= $_POST['QtyPassed'];
$QtyFailed= $_POST['QtyFailed'];
$SealsChecked= $_POST['SealsChecked'];
$SealsPassed= $_POST['SealsPassed'];
$SealsFailed= $_POST['SealsFailed'];
$GraphicsChecked= $_POST['GraphicsChecked'];
$GraphicsPassed= $_POST['GraphicsPassed'];
$GraphicsFailed= $_POST['GraphicsFailed'];
$CheckedBy= $_POST['CheckedBy'];
$id = $_GET['id'];
$Lot = $_GET['Lot'];
// OR
// $id = $_POST['id'];
$sql = "UPDATE logs SET QuantityChecked= '$QuantityChecked',
AnodThickness = '$AnodThickness',
QtyPassed = '$QtyPassed',
QtyFailed = '$QtyFailed',
SealsChecked = '$SealsChecked',
SealsPassed = '$SealsPassed',
SealsFailed = '$SealsFailed',
GraphicsChecked = '$GraphicsChecked',
GraphicsPassed = '$GraphicsPassed',
GraphicsFailed = '$GraphicsFailed',
CheckedBy = '$CheckedBy',
DateTimeChecked = now()
WHERE id = $id ";
$conn->query($sql);
$result = mysqli_query($conn, "SELECT SUM(QuantityChecked) AS TotalQuantityChecked FROM logs WHERE Lot = $Lot");
$row= mysqli_fetch_assoc($result);
$sum= $row['TotalQuantityChecked'];
$conn->close();
}
echo $sum;
echo "Record Updated.";
我已进行编辑以反映新代码和对此问题的更改
您正在使用 POST
提交表单,因此隐藏元素的值将在 $_POST
中,而不是 $_GET
。
$Lot = $_POST['Lot'];