2015-02-09 20 views
-2

我在我的在线订购系统中有我的产品清单,我想更新所选产品的数据。当我点击编辑链接时,会发生什么情况,它会将数据库中的值发布到其他页面(编辑页面),即使我已经更改了数据,数据库也不会更新。如何更新所选产品的数据?

admin.php的(页,所有的产品都列出)

<a href=addprod.php?id='.$row['ID'].'>EDIT</a> 

ADDPROD.PHP(页面,管理员可以添加/更新产品)

echo'<form method="post" action="saveprod.php" class="product" style="margin-top:500px;" enctype="multipart/form-data">'; 
if (isset($_GET['id'])) { 
include('db.php'); 
$id=$_GET['id']; 
$result = mysql_query("SELECT * FROM products WHERE ID = $id"); 
echo'<input type="hidden" name="hiddenId" value="'.$id.'"> 
<table border="1" cellpadding="8px" width="100%">'; 

while($row3 = mysql_fetch_array($result)) { 
$ID = $row3['ID']; 
$Image = $row3['Image']; 
$Product = $row3['Product']; 
$Description = $row3['Description']; 
$PricePack = $row3['PricePack']; 
$PriceBox = $row3['PriceBox']; 
$Discount = $row3['Discount']; 
$Category = $row3['Category']; 
} 
echo' 
<tr><td align="right">Image</td><td><input type="text" id="img" name="img" value="'.$Image.'"/> </td></tr> 
<tr><td align="right"></td><td><input type="file" id="img" name="img" /></td </tr> 
<tr><td align="right">Product</td><td><input type="text" id="prod" name="prod" value="'.$Product.'"/></td></tr> 
<tr><td align="right">Description</td><td><textarea id="desc" name="desc" style="resize:none; height:100px; width:200px; ">'.$Description.'</textarea></td></tr> 
<tr><td align="right">Price Pack</td><td><input type="text" id="pck" name="pck" value="'.$PricePack.'"/></td></tr> 
<tr><td align="right">Price Box</td><td><input type="text" id="box" name="box" value="'.$PriceBox.'"/></td></tr> 
<tr><td align="right">Discount</td><td><input type="text" id="disc" name="disc" value="'.$Discount.'"/></td></tr> 
<tr><td align="right">Category</td><td><input type="text" id="cat" name="cat" value="'.$Category.'"/></td></tr> 
<tr><td align="right"></td><td><input type="submit" value="Save"/></a> <input type="reset" value="Clear"/></td></tr>'; 
} 
echo' </table> </form>'; 

SAVEPROD.PHP

<?php 
include('db.php'); 
$id = $_POST['ID']; 
$Image = $_POST['Image']; 
$Product = $_POST['Product']; 
$Description = $_POST['Description']; 
$PricePack = $_POST['PricePack']; 
$PriceBox = $_POST['PriceBox']; 
$Discount = $_POST['Discount']; 
$Category = $_POST['Category']; 
mysql_query("UPDATE products SET Image='$Image', Product='$Product', Description='$Description', PricePack='$PricePack', PriceBox='$PriceBox', Discount='$Discount', Category='$Category' WHERE ID='$id'"); 

header("location: admin.php"); 
    exit(); 
?> 
+2

您试图更新您的数据?我在这里没有看到类似的东西。用UPDATE或''_POST'''不需要查询你需要显示你的saveprod.php文件,如果你有问题 – KyleMassacre 2015-02-09 04:30:18

+0

Hi @KyleMassacre我已经更新了我的问题。对不起,我无法粘贴查询saveprod。谢谢。希望你能帮忙。 – fashionavenew 2015-02-09 05:05:18

回答

1

注意:

  • 您发布的内容名称不准确。
  • 确保您提供的列名,表名是正确的。对此感到非常敏感。
  • 您应该至少考虑mysqli_* prepared statement以防止SQL injections

你savepro.php应该是这样的:

<?php 
include('db.php'); 
/* CHANGED THE WAY YOU CALL THE POST DATA BASED FROM YOUR HTML FORM */ 
$id = $_POST['hiddenId']; 
$Image = $_POST['img']; 
$Product = $_POST['prod']; 
$Description = $_POST['desc']; 
$PricePack = $_POST['pck']; 
$PriceBox = $_POST['box']; 
$Discount = $_POST['disc']; 
$Category = $_POST['cat']; 
mysql_query("UPDATE products SET Image='$Image', Product='$Product', Description='$Description', PricePack='$PricePack', PriceBox='$PriceBox', Discount='$Discount', Category='$Category' WHERE ID='$id'"); 

header("location: admin.php"); 
exit(); 
?> 

如果您在准备好的声明中做到了,它看起来像下面的一个。所以你不会担心SQL注入。只是一个简单的示例:

$stmt = $YourConnection->prepare("UPDATE products SET Image=?, Product=?, Description=?, PricePack=?, PriceBox=?, Discount=?, Category=? WHERE ID=?"); 

$stmt->bind_param('sssssssi', $_POST["img"], $_POST["prod"], $_POST["desc"], $_POST["pck"], $_POST["box"], $_POST["disc"], $_POST["cat"], $_POST["hiddenId"]); 

$stmt->execute(); 
1

在你的saveprod.php上,你试图获取一个不存在的元素的值,检查你的每个输入字段的名字[R addprod.php,应该对你在你的saveprod.php

这里发出请求相对应的是,你在做什么的例子:

ADDPROD.PHP 

    <input type="text" id="img" name="img" value="'.$Image.'"/> 

SAVEPROD.PHP 

    $Image = $_POST['Image']; 

应该是这样的:

ADDPROD.PHP 

    <input type="text" id="img" name="img" value="'.$Image.'"/> 

SAVEPROD.PHP 

    $Image = $_POST['img']; 
相关问题