php, sql, html 用相应的按钮更新唯一记录
php, sql, html updating a unique record with corresponding button
我是 PHP 和 SQL 的新手,正在尝试弄清楚如何让 HTML 批准(提交)按钮专门与其对应的记录进行交互。当前,当单击“批准”按钮时,每个字段都会更新,但可用的最前面(第一条)记录始终是更新的。我希望用户能够跳过第一条记录并更新不同的记录。非常感谢所有 suggestions/help。
$conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('there was a problem connecting to the database' . mysql_error());
$sql = "SELECT Part, Lot, Qty, AnodTemp, Amp, SealTemp, PerformedBy, DateTimePerformed, FinalAnodThickness, QtyPass, FinalSealCheck, CheckedBy, DateTimeChecked, id FROM logs";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$unapproved = $row['CheckedBy'];
if($unapproved == null)
{
echo "<br><br><br> Part: " . $row['Part']. " / Lot: " . $row['Lot']. " / Qty: " . $row['Qty']. " / AnodTemp: " . $row['AnodTemp']. " / Amp: " . $row['Amp']. " / SealTemp: " . $row['SealTemp']. " / PerformedBy: " . $row['PerformedBy']. " / ID: " . $row['id']; ?>
<form action="adminapproval.php" method="post">
Final Anod Thickness:<br>
<input type="text" name="FinalAnodThickness">
<br><br>
Qty Pass:<br>
<input type="text" name="QtyPass">
<br><br>
Final Seal Check:<br>
<input type="text" name="FinalSealCheck">
<br><br>
<input type="submit" id="submit" value="Approve" name="submit">
<br><br>
</form>
_____________________________________________________________________<br>
<?php
if (isset($_POST['submit']))
{
$FinalAnodThickness= $_POST['FinalAnodThickness'];
$QtyPass= $_POST['QtyPass'];
$FinalSealCheck= $_POST['FinalSealCheck'];
$CheckedBy= $_SESSION['CheckedBy'];
$id = $row['id'];
$sql = "UPDATE logs SET FinalAnodThickness = '$FinalAnodThickness', QtyPass = '$QtyPass', FinalSealCheck = '$FinalSealCheck', CheckedBy = '$CheckedBy', DateTimeChecked = now() WHERE id = $id ";
$conn->query($sql);
break;
$conn->close();
echo "Record Updated.";
header("Location: adminapproval.php");
}
}
}
}
echo "<br><br> No further items need to be approved at this time.";
?>
<?php
$server = "localhost";
$username = "username";
$password = "password";
$dbname = "db";
$con = mysqli_connect($server, $username, $password, $dbname);
if (!$con) {
die("Faild: " . mysqli_connect_error());
}
$sql = "UPDATE xxx SET lastname='Jan' WHERE id=2"; // This is importat
if (mysqli_query($con, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($con);
}
mysqli_close($con);
?>
$CheckedBy= $_SESSION['CheckedBy'];
$id = $row['id'];
行 ID 是否也应该从会话中出来?如果不是,那么它将始终指向行中的第一项。
两个文件
adminapproval.php
<?php
session_start();
$conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('there was a problem connecting to the database' . mysql_error());
$sql = "SELECT Part, Lot, Qty, AnodTemp, Amp, SealTemp, PerformedBy, DateTimePerformed, FinalAnodThickness, QtyPass, FinalSealCheck, CheckedBy, DateTimeChecked, id FROM logs";
$result = $conn->query($sql);
if ($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$unapproved = $row['CheckedBy'];
if($unapproved == null){
echo "<br><br><br> Part: " . $row['Part']. " / Lot: " . $row['Lot']. " / Qty: " . $row['Qty']. " / AnodTemp: " . $row['AnodTemp']. " / Amp: " . $row['Amp']. " / SealTemp: " . $row['SealTemp']. " / PerformedBy: " . $row['PerformedBy']. " / ID: " . $row['id']; ?>
<form action="adminapproval-exec.php?id=<?php echo $row['id']; ?>" method="post">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
<input type="hidden" name="checkedby" value="<?php echo $SESSION['CheckedBy']; ?>" />
Final Anod Thickness:<br>
<input type="text" name="FinalAnodThickness">
<br><br>
Qty Pass:<br>
<input type="text" name="QtyPass">
<br><br>
Final Seal Check:<br>
<input type="text" name="FinalSealCheck">
<br><br>
<input type="submit" id="submit" value="Approve" name="submit">
<br><br>
</form>
<?php
}
}
} else {
echo "<br><br> No further items need to be approved at this time.";
}
?>
adminapproval-exec.php
<?php
session_start();
if (isset($_POST['submit'])){
$FinalAnodThickness= $_POST['FinalAnodThickness'];
$QtyPass= $_POST['QtyPass'];
$FinalSealCheck= $_POST['FinalSealCheck'];
$CheckedBy= $_POST['CheckedBy'];
$id = $_GET['id'];
// OR
// $id = $_POST['id'];
$sql = "UPDATE logs SET FinalAnodThickness = '$FinalAnodThickness', QtyPass = '$QtyPass', FinalSealCheck = '$FinalSealCheck', CheckedBy = '$CheckedBy', DateTimeChecked = now() WHERE id = $id ";
$conn->query($sql);
$conn->close();
// echo "Record Updated.";
header("Location: adminapproval.php");
}
?>
我是 PHP 和 SQL 的新手,正在尝试弄清楚如何让 HTML 批准(提交)按钮专门与其对应的记录进行交互。当前,当单击“批准”按钮时,每个字段都会更新,但可用的最前面(第一条)记录始终是更新的。我希望用户能够跳过第一条记录并更新不同的记录。非常感谢所有 suggestions/help。
$conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('there was a problem connecting to the database' . mysql_error());
$sql = "SELECT Part, Lot, Qty, AnodTemp, Amp, SealTemp, PerformedBy, DateTimePerformed, FinalAnodThickness, QtyPass, FinalSealCheck, CheckedBy, DateTimeChecked, id FROM logs";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$unapproved = $row['CheckedBy'];
if($unapproved == null)
{
echo "<br><br><br> Part: " . $row['Part']. " / Lot: " . $row['Lot']. " / Qty: " . $row['Qty']. " / AnodTemp: " . $row['AnodTemp']. " / Amp: " . $row['Amp']. " / SealTemp: " . $row['SealTemp']. " / PerformedBy: " . $row['PerformedBy']. " / ID: " . $row['id']; ?>
<form action="adminapproval.php" method="post">
Final Anod Thickness:<br>
<input type="text" name="FinalAnodThickness">
<br><br>
Qty Pass:<br>
<input type="text" name="QtyPass">
<br><br>
Final Seal Check:<br>
<input type="text" name="FinalSealCheck">
<br><br>
<input type="submit" id="submit" value="Approve" name="submit">
<br><br>
</form>
_____________________________________________________________________<br>
<?php
if (isset($_POST['submit']))
{
$FinalAnodThickness= $_POST['FinalAnodThickness'];
$QtyPass= $_POST['QtyPass'];
$FinalSealCheck= $_POST['FinalSealCheck'];
$CheckedBy= $_SESSION['CheckedBy'];
$id = $row['id'];
$sql = "UPDATE logs SET FinalAnodThickness = '$FinalAnodThickness', QtyPass = '$QtyPass', FinalSealCheck = '$FinalSealCheck', CheckedBy = '$CheckedBy', DateTimeChecked = now() WHERE id = $id ";
$conn->query($sql);
break;
$conn->close();
echo "Record Updated.";
header("Location: adminapproval.php");
}
}
}
}
echo "<br><br> No further items need to be approved at this time.";
?>
<?php
$server = "localhost";
$username = "username";
$password = "password";
$dbname = "db";
$con = mysqli_connect($server, $username, $password, $dbname);
if (!$con) {
die("Faild: " . mysqli_connect_error());
}
$sql = "UPDATE xxx SET lastname='Jan' WHERE id=2"; // This is importat
if (mysqli_query($con, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($con);
}
mysqli_close($con);
?>
$CheckedBy= $_SESSION['CheckedBy'];
$id = $row['id'];
行 ID 是否也应该从会话中出来?如果不是,那么它将始终指向行中的第一项。
两个文件
adminapproval.php
<?php
session_start();
$conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('there was a problem connecting to the database' . mysql_error());
$sql = "SELECT Part, Lot, Qty, AnodTemp, Amp, SealTemp, PerformedBy, DateTimePerformed, FinalAnodThickness, QtyPass, FinalSealCheck, CheckedBy, DateTimeChecked, id FROM logs";
$result = $conn->query($sql);
if ($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$unapproved = $row['CheckedBy'];
if($unapproved == null){
echo "<br><br><br> Part: " . $row['Part']. " / Lot: " . $row['Lot']. " / Qty: " . $row['Qty']. " / AnodTemp: " . $row['AnodTemp']. " / Amp: " . $row['Amp']. " / SealTemp: " . $row['SealTemp']. " / PerformedBy: " . $row['PerformedBy']. " / ID: " . $row['id']; ?>
<form action="adminapproval-exec.php?id=<?php echo $row['id']; ?>" method="post">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
<input type="hidden" name="checkedby" value="<?php echo $SESSION['CheckedBy']; ?>" />
Final Anod Thickness:<br>
<input type="text" name="FinalAnodThickness">
<br><br>
Qty Pass:<br>
<input type="text" name="QtyPass">
<br><br>
Final Seal Check:<br>
<input type="text" name="FinalSealCheck">
<br><br>
<input type="submit" id="submit" value="Approve" name="submit">
<br><br>
</form>
<?php
}
}
} else {
echo "<br><br> No further items need to be approved at this time.";
}
?>
adminapproval-exec.php
<?php
session_start();
if (isset($_POST['submit'])){
$FinalAnodThickness= $_POST['FinalAnodThickness'];
$QtyPass= $_POST['QtyPass'];
$FinalSealCheck= $_POST['FinalSealCheck'];
$CheckedBy= $_POST['CheckedBy'];
$id = $_GET['id'];
// OR
// $id = $_POST['id'];
$sql = "UPDATE logs SET FinalAnodThickness = '$FinalAnodThickness', QtyPass = '$QtyPass', FinalSealCheck = '$FinalSealCheck', CheckedBy = '$CheckedBy', DateTimeChecked = now() WHERE id = $id ";
$conn->query($sql);
$conn->close();
// echo "Record Updated.";
header("Location: adminapproval.php");
}
?>