gpt4 book ai didi

PHP - 在循环的帮助下从表单插入mysql数据库,可能吗?

转载 作者:行者123 更新时间:2023-11-30 00:24:51 25 4
gpt4 key购买 nike

好吧,所以我坐在这里摆弄我的一些旧代码。

下面是我运行的循环示例,用于打印产品和当前库存。每当我想手动更改此值时,我都会打开它并输入新的库存值。但我陷入困境,由于某种原因,我无法将其插入数据库中正确的 ProdId。

// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $oneLiter )) {
echo '<input type="hidden" name="id" value="'<?php echo $row['ProdId']; ?>'"/>'
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['Prodnamn'] . '<div class="min_lager">' . $row['minLager'] . '</div></td>';
echo '<td><input class="lager" type="number" name="prod['. $row['ProdId'] .']" value="' . $row['Lagerstatus'] . '"></td>';
echo "</tr>";
}

// close table>
echo "</table>";

这是我尝试插入已更改的值时的以下代码。

// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit']))
{

// get form data, making sure it is valid
foreach($_POST['prod'] as $item) {
$stock = mysql_real_escape_string(htmlspecialchars($item));
// check that prodname/color fields are both filled in
if ($stock == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';

//error, display form
renderForm($id, $prodname, $color, $size, $packing, $minstock, $stock, $error);
}
else
{
// save the data to the database
mysql_query("UPDATE produkter_sthlm SET Lagerstatus='$stock' WHERE ProdId='$item'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: lagerstatus_stockholm.php");
}
}
}
else
// if the form hasn't been submitted, get the data from the db and display the form
{

// query db
$result = mysql_query("SELECT * FROM produkter_sthlm")
or die(mysql_error());
$row = mysql_fetch_array($result);

// check that the 'id' matches up with a row in the databse
if($row)
{

// get data from db
$id = $row['ProdId'];
$prodname = $row['Prodnamn'];
$color = $row['Farg'];
$size = $row['Storlek'];
$packing = $row['antalPack'];
$minstock = $row['minLager'];
$stock = $row['Lagerstatus'];

// show form
renderForm($id, $prodname, $color, $size, $packing, $minstock, $stock, '');
}
else
// if no match, display result
{
echo "No results!";
}
}

我知道 mysql_query("UPDATE produkter_sthlm SET Lagerstatus='$stock' WHERE ProdId='$item'") 是错误的,但我无法弄清楚如何从$_POST

如果有任何帮助,我可以发布带有架构副本等的完整文件。

刚刚编辑了整个文件。

<?php
session_start();
if(!isset($_SESSION['myusername'])){
header("location:login.php");
}
// connect to the database
include '/include/config.php';

function renderForm($id, $prodname, $color, $size, $packing, $minstock, $stock, $error)
{
$oneLiter = mysql_query("SELECT * FROM produkter_sthlm WHERE `Storlek`=1 AND `Active`=1") or die(mysql_error());
$fourLiter = mysql_query("SELECT * FROM produkter_sthlm WHERE `Storlek`=4 AND `Active`=1") or die(mysql_error());
$fiveLiter = mysql_query("SELECT * FROM produkter_sthlm WHERE `Storlek`=5 AND `Active`=1") or die(mysql_error());
$tenLiter = mysql_query("SELECT * FROM produkter_sthlm WHERE `Storlek`=10 AND `Active`=1") or die(mysql_error());
$twentyLiter = mysql_query("SELECT * FROM produkter_sthlm WHERE `Storlek`=20 AND `Active`=1") or die(mysql_error());
?>
<!DOCTYPE html>
<html>

<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Lagerstatus Stockholm - Petrolia Lagersystem</title>

<!-- Core CSS - Include with every page -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="font-awesome/css/font-awesome.css" rel="stylesheet">
<link href="css/custom-style.css" rel="stylesheet">

<!-- Page-Level Plugin CSS - Blank -->

<!-- SB Admin CSS - Include with every page -->
<link href="css/sb-admin.css" rel="stylesheet">

</head>

<body>

<div id="wrapper">

<?php include "top_menu.php"; ?>
<?php include "side_menu.php"; ?>

<div id="page-wrapper">
<div class="row">
<h1 class="page-header">Lagerstatus - Stockholm <?php print(Date("F j, Y H:i:s")); ?></h1>
<form action="" method="post">
<div class="button-container">
<button type="submit" name="submit" class="btn btn-primary"><i class="fa fa-save fa-fw"></i>Spara ändring</button>
</div>
<div class="col-md-6 well">
<h2 class="horizontal">1 Liter</h2>
<?php echo "<table border='1' cellpadding='10'>";
echo "<tr><th>Typ</th> <th>Antal</th></tr>";

// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $oneLiter )) {
echo '<input type="hidden" name="id" value="'.$row['ProdId'].'"/>';
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['Prodnamn'] . '<div class="min_lager">' . $row['minLager'] . '</div></td>';
echo '<td><input class="lager" type="number" name="prod['. $row['ProdId'] .']" value="' . $row['Lagerstatus'] . '"></td>';
echo "</tr>";
}

// close table>
echo "</table>";
?>

<h2 class="horizontal">4 Liter</h2>
<?php echo "<table border='1' cellpadding='10'>";
echo "<tr><th>Typ</th> <th>Antal</th></tr>";

// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $fourLiter )) {

// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['Prodnamn'] . '<div class="min_lager">' . $row['minLager'] . '</div></td>';
echo '<td><input class="lager" type="number" name="prod['. $row['ProdId'] .']" value="' . $row['Lagerstatus'] . '"></td>';
echo "</tr>";
}

// close table>
echo "</table>";
?>
</div>
<!-- /.col-lg-6 -->
<div class="col-md-6 well">
<h2 class="horizontal">5 Liter</h2>
<?php echo "<table border='1' cellpadding='10'>";
echo "<tr><th>Typ</th> <th>Antal</th></tr>";

// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $fiveLiter )) {

// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['Prodnamn'] . '<div class="min_lager">' . $row['minLager'] . '</div></td>';
echo '<td><input class="lager" type="number" name="prod['. $row['ProdId'] .']" value="' . $row['Lagerstatus'] . '"></td>';
echo "</tr>";
}

// close table>
echo "</table>";
?>

<h2 class="horizontal">10 Liter</h2>
<?php echo "<table border='1' cellpadding='10'>";
echo "<tr><th>Typ</th> <th>Antal</th></tr>";

// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $tenLiter )) {

// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['Prodnamn'] . '<div class="min_lager">' . $row['minLager'] . '</div></td>';
echo '<td><input class="lager" type="number" name="prod['. $row['ProdId'] .']" value="' . $row['Lagerstatus'] . '"></td>';
echo "</tr>";
}

// close table>
echo "</table>";
?>

<h2 class="horizontal">20 Liter</h2>
<?php echo "<table border='1' cellpadding='10'>";
echo "<tr><th>Typ</th> <th>Antal</th></tr>";

// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $twentyLiter )) {

// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['Prodnamn'] . '<div class="min_lager">' . $row['minLager'] . '</div></td>';
echo '<td><input class="lager" type="number" name="prod['. $row['ProdId'] .']" value="' . $row['Lagerstatus'] . '"></td>';
echo "</tr>";
}

// close table>
echo "</table>";
?>
</div>
<!-- /.col-lg-6 -->
</form>
</div>
<!-- /.row -->
</div>
<!-- /#page-wrapper -->

</div>
<!-- /#wrapper -->

<!-- Core Scripts - Include with every page -->
<script src="js/jquery-1.10.2.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/plugins/metisMenu/jquery.metisMenu.js"></script>

<!-- Page-Level Plugin Scripts - Blank -->

<!-- SB Admin Scripts - Include with every page -->
<script src="js/sb-admin.js"></script>

<!-- Page-Level Demo Scripts - Blank - Use for reference -->

</body>

</html>
<?php
}


// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit']))
{

// get form data, making sure it is valid
foreach($_POST['prod'] as $item) {
$stock = mysql_real_escape_string(htmlspecialchars($item));
// check that prodname/color fields are both filled in
if ($stock == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';

//error, display form
renderForm($id, $prodname, $color, $size, $packing, $minstock, $stock, $error);
}
else
{
echo "lol" . $_POST['prod'];
// save the data to the database
mysql_query("UPDATE produkter_sthlm SET Lagerstatus='$stock' WHERE ProdId='$item'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: lagerstatus_stockholm.php");
}
}
}
else
// if the form hasn't been submitted, get the data from the db and display the form
{

// query db
$result = mysql_query("SELECT * FROM produkter_sthlm")
or die(mysql_error());
$row = mysql_fetch_array($result);

// check that the 'id' matches up with a row in the databse
if($row)
{

// get data from db
$id = $row['ProdId'];
$prodname = $row['Prodnamn'];
$color = $row['Farg'];
$size = $row['Storlek'];
$packing = $row['antalPack'];
$minstock = $row['minLager'];
$stock = $row['Lagerstatus'];

// show form
renderForm($id, $prodname, $color, $size, $packing, $minstock, $stock, '');
}
else
// if no match, display result
{
echo "No results!";
}
}
?>

最佳答案

第一个脚本的第三行(包括注释):

echo '<input type="hidden" name="id" value="'<?php echo $row['ProdId']; ?>'"/>'

末尾缺少分号。它也会针对 while 循环的每次迭代进行回显,这将导致 $_POST['id'] 始终包含页面上最后一个“隐藏”输入标记的值。

我的建议是使用生成的键创建两个输入数据数组:

// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $oneLiter )) {
echo '<input type="hidden" name="id[]" value="'<?php echo $row['ProdId']; ?>'"/>';
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['Prodnamn'] . '<div class="min_lager">' . $row['minLager'] . '</div></td>';
echo '<td><input class="lager" type="number" name="status[]" value="' . $row['Lagerstatus'] . '"></td>';
echo "</tr>";
}

// close table>
echo "</table>";

然后您可以按如下方式循环遍历结果。由于隐藏输入和数字输入成对呈现,因此相应 POST 数组的键将匹配:

foreach ($_POST['id'] as $key => $prodId) {
// As both seem to be integers, SQL injections can easily be filtered out by explicitly casting it to an integer
$prodId = (int) $prodId;

if ($prodId > 0) {
$prodStatus = (int) $_POST['status'][$key];

// do your processing here
}
}

关于PHP - 在循环的帮助下从表单插入mysql数据库,可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22969788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com