gpt4 book ai didi

php - 如何使用编辑按钮更新 mysql,该按钮会将当前数据从我制作的简单 CMS 发送到表单中

转载 作者:行者123 更新时间:2023-11-29 23:59:12 25 4
gpt4 key购买 nike

我正在尝试使用编辑按钮更新数据库中的值。但是,我不知道该怎么做。我不知道该怎么做,

我的目标是,当我按下 inventory.php 中所选产品的“编辑”按钮时,它将重定向到我在 update_inv.php 中创建的表单,当然填充了来自数据库的当前值,然后我可以编辑表单或上传图片,然后单击“保存”。

每个产品都有 id,但我仍然不知道如何使用它们来编辑所选产品。

例如产品“Apple”是 prod_id = 1,我需要使用我创建的表单编辑数据库中 prod_id = 1 的所有值。

我希望我能够很好地解释我的情况,以便各位专家能够理解我的担忧。非常感谢您的帮助和花时间理解我的问题。

这里是 Inventory.php

<?php
session_start();
$conn = @mysql_connect("localhost","root","12148qx3er");
$db = @mysql_select_db("buybranded");

$qry = "SELECT * FROM inventory";
mysql_set_charset("UTF8");
$result = @mysql_query($qry);
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
echo "<center>";
echo "<table style='width:800px'>
<tr>

<th>Product ID</th>
<th>Product Brand</th>
<th>Product Category</th>
<th>Product Name</th>
<th>Quantity</th>
<th>Action</th>
<tr>"
;while ($row=mysql_fetch_array($result)){

echo "<tr>";
echo "<td>".$row['prod_id'];
echo "<td>".$row['prod_brand'];
echo "<td>".$row['prod_category'];
echo "<td>".$row['prod_name'];
echo "<td>".$row['prod_quantity'];
echo "<td> <form method='post' action='update_inv.php'><button type='submit'>Edit</button>"; //PROVIDE LINKS FOR QUICK AND FULL EDIT
echo "</tr>";
}
echo "</table>";
?>

这是 update.php

<div id="page-wrap">
<h1>Edit a Product</h1>
<form action="updating_inv.php" enctype="multipart/form-data" method="post">

<label for="prod_brand">Product Brand:</label><br />
<input name="prod_brand" id="prod_brand" type="text" maxlength="150" class="col1" placeholder="Auto Complete Form" required/>
<div class="clear"></div>

<label for="prod_name">Product name:</label><br />
<input name="prod_name" id="prod_name" type="text" maxlength="150" class="col1" required/>
<div class="clear"></div>

<label for="prod_category">Category:</label><br />
<select name="prod_category" class="col1">
<option <?php if (isset($prod_category) && $prod_category==" ") ?>>Select Category</option>
<option <?php if (isset($prod_category) && $prod_category=="Compression") ?>>Compression</option>
<option <?php if (isset($prod_category) && $prod_category=="Headwear") ?>>Headwear</option>
<option <?php if (isset($prod_category) && $prod_category=="Shorts") ?>>Shorts</option>
<option <?php if (isset($prod_category) && $prod_category=="Yoga") ?>>Yoga</option>
<option <?php if (isset($prod_category) && $prod_category=="Swimming") ?>>Swimming</option>
<option <?php if (isset($prod_category) && $prod_category=="Fitness Training") ?>>Fitness Training</option>
<option <?php if (isset($prod_category) && $prod_category=="Water Bottles") ?>>Water Bottles</option>
<option <?php if (isset($prod_category) && $prod_category=="Sports Socks") ?>>Sports Socks</option>
<option <?php if (isset($prod_category) && $prod_category=="Sports Bags") ?>>Sports Bags</option>
<option <?php if (isset($prod_category) && $prod_category=="Protein Shaker") ?>>Protein Shaker</option>
<option <?php if (isset($prod_category) && $prod_category=="Gloves") ?>>Gloves</option>
<option <?php if (isset($prod_category) && $prod_category=="Splash Guards") ?>>Splash Guards</option>
<option <?php if (isset($prod_category) && $prod_category=="Massagers") ?>>Massagers</option>
<option <?php if (isset($prod_category) && $prod_category=="Sun Blocks") ?>>Sun Blocks</option>
<option <?php if (isset($prod_category) && $prod_category=="Sports Cleaners") ?>>Sports Cleaners</option>
</select>
<div class="clear"></div><br>

<label for="prod_price">Price:</label><br />
<input name="prod_price" id="prod_price" type="text" maxlength="10" class="col1" required placeholder="ex. 330"/>
<div class="clear"></div>

<label for="prod_desc">Description: (HTML Codes are allowed)</label><br />
<textarea name="prod_desc" id="prod_desc" class="col2" required></textarea>
<div class="clear"></div>

<label for="prod_pic">Upload Image:</label><br />
<input type="file" name="prod_pic" class="col2" required/>
<div class="clear"></div><br>

<label for="prod_quantity">Quantity: (Editable in the Invetory Page)</label><br />
<input name="prod_quantity" id="prod_quantity" type="text" maxlength="10" class="col2" required placeholder="ex. 10"/>
<div class="clear"></div>
<input type="submit" value="Update This Entry!" />
</form>
</div>

更新_inv.php

<?php
session_start();
$con=@mysql_connect("localhost","root","12148qx3er");
$dbcheck = mysql_select_db("buybranded");

if (!$dbcheck) {
echo mysql_error();
}


$image = addslashes(file_get_contents($_FILES['prod_pic']['tmp_name']));
$sql="UPDATE INTO `inventory` (`prod_brand`,`prod_name`,`prod_category`,`prod_price`,`prod_desc`,`prod_quantity`,`prod_pic`)
VALUES
('$_POST[prod_brand]','$_POST[prod_name]','$_POST[prod_category]','$_POST[prod_price]','$_POST[prod_desc]','$_POST[prod_quantity]','{$image}')";




if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error($con));
}

header('refresh: 0');
$message = "Product Successfully Updated";
echo("<script type='text/javascript'>alert('$message');</script>");
?>

如果需要,这里是我的 CMS.php 的代码

<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Add a Product</title>
<link rel="stylesheet" type="text/css" href="cms.css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.autocomplete.js"></script>
<script>
$(document).ready(function(){
$("#prod_brand").autocomplete("prod_brand_auto_complete.php", {
selectFirst: true
});
});
</script>
</head>
<body>
<div id="page-wrap">
<h1>Add a Product</h1>
<form action="add_prod.php" enctype="multipart/form-data" method="post">

<label for="prod_brand">Product Brand:</label><br />
<input name="prod_brand" id="prod_brand" type="text" maxlength="150" class="col1" placeholder="Auto Complete Form" required/>
<div class="clear"></div>

<label for="prod_name">Product name:</label><br />
<input name="prod_name" id="prod_name" type="text" maxlength="150" class="col1" required/>
<div class="clear"></div>

<label for="prod_category">Category:</label><br />
<select name="prod_category" class="col1">
<option <?php if (isset($prod_category) && $prod_category==" ") ?>>Select Category</option>
<option <?php if (isset($prod_category) && $prod_category=="Compression") ?>>Compression</option>
<option <?php if (isset($prod_category) && $prod_category=="Headwear") ?>>Headwear</option>
<option <?php if (isset($prod_category) && $prod_category=="Shorts") ?>>Shorts</option>
<option <?php if (isset($prod_category) && $prod_category=="Yoga") ?>>Yoga</option>
<option <?php if (isset($prod_category) && $prod_category=="Swimming") ?>>Swimming</option>
<option <?php if (isset($prod_category) && $prod_category=="Fitness Training") ?>>Fitness Training</option>
<option <?php if (isset($prod_category) && $prod_category=="Water Bottles") ?>>Water Bottles</option>
<option <?php if (isset($prod_category) && $prod_category=="Sports Socks") ?>>Sports Socks</option>
<option <?php if (isset($prod_category) && $prod_category=="Sports Bags") ?>>Sports Bags</option>
<option <?php if (isset($prod_category) && $prod_category=="Protein Shaker") ?>>Protein Shaker</option>
<option <?php if (isset($prod_category) && $prod_category=="Gloves") ?>>Gloves</option>
<option <?php if (isset($prod_category) && $prod_category=="Splash Guards") ?>>Splash Guards</option>
<option <?php if (isset($prod_category) && $prod_category=="Massagers") ?>>Massagers</option>
<option <?php if (isset($prod_category) && $prod_category=="Sun Blocks") ?>>Sun Blocks</option>
<option <?php if (isset($prod_category) && $prod_category=="Sports Cleaners") ?>>Sports Cleaners</option>
</select>
<div class="clear"></div><br>

<label for="prod_price">Price:</label><br />
<input name="prod_price" id="prod_price" type="text" maxlength="10" class="col1" required placeholder="ex. 330"/>
<div class="clear"></div>

<label for="prod_desc">Description: (HTML Codes are allowed)</label><br />
<textarea name="prod_desc" id="prod_desc" class="col2" required></textarea>
<div class="clear"></div>

<label for="prod_pic">Upload Image:</label><br />
<input type="file" name="prod_pic" class="col2" required/>
<div class="clear"></div><br>

<label for="prod_quantity">Quantity: (Editable in the Invetory Page)</label><br />
<input name="prod_quantity" id="prod_quantity" type="text" maxlength="10" class="col2" required placeholder="ex. 10"/>
<div class="clear"></div>
<input type="submit" value="Create This Entry!" />
</form>
</div>
</body>
</html>

这是 add_prod.php

<?php
session_start();
$con=@mysql_connect("localhost","root","12148qx3er");
$dbcheck = mysql_select_db("buybranded");

if (!$dbcheck) {
echo mysql_error();
}


$image = addslashes(file_get_contents($_FILES['prod_pic']['tmp_name']));
$sql="INSERT INTO `inventory` (`prod_brand`,`prod_name`,`prod_category`,`prod_price`,`prod_desc`,`prod_quantity`,`prod_pic`)
VALUES
('$_POST[prod_brand]','$_POST[prod_name]','$_POST[prod_category]','$_POST[prod_price]','$_POST[prod_desc]','$_POST[prod_quantity]','{$image}')";




if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error($con));
}

header('refresh: 0; url=cms.php');
$message = "Product Successfully Added";
echo("<script type='text/javascript'>alert('$message');</script>");
?>

最佳答案

将 Inventory.php 中的第 31 行更改为:

echo "<td> <form method='post' action='update_inv.php?id=".$row['prod_id']."'><button type='submit'>Edit</button>";

添加到Update.php:

<?php
session_start();
$itemid = $_GET['id'];
$conn = @mysql_connect("localhost","root","password");
$db = @mysql_select_db("buybranded");

$qry = "SELECT * FROM inventory WHERE prod_id=$itemid";
mysql_set_charset("UTF8");
$result = @mysql_query($qry);
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}?>

将 update.php 中的表单操作从 'action="updating_inv.php"' 更改为 'action="updating_inv.php?id=$itemid"'

然后将所有输入值设置为从查询收到的数据:

value="<?php echo mysql_result($result, 0, 'prod_brand');?>"

将updated_inv.php更改为:

$itemid = $_GET['id'];

$sql="UPDATE INTO `inventory` (`prod_brand`,`prod_name`,`prod_category`,`prod_price`,`prod_desc`,`prod_quantity`,`prod_pic`)
VALUES
('$_POST[prod_brand]','$_POST[prod_name]','$_POST[prod_category]','$_POST[prod_price]','$_POST[prod_desc]','$_POST[prod_quantity]','{$image}') WHERE prod_id=$itemid";

我确信有很多更好的方法可以做到这一点,但这就是我所知道的。另外,您应该将所有 mysql 查询更新为 mysqli,因为在 PHP5+ 中不再支持它。

关于php - 如何使用编辑按钮更新 mysql,该按钮会将当前数据从我制作的简单 CMS 发送到表单中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25164385/

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