从数据库中将信息显示在表单上

时间:2014-04-21 23:03:37

标签: php mysql database forms

解决

我正在尝试制作一个编辑表单,以便自动将数据库中的信息显示在表单上。

目前,只有在针对不同产品进行编辑时才会更改产品详细信息,并且似乎只想显示数据库中的第一行,例如,id' 1'而不是id' 3'当选择第3个id时。此外,图像似乎没有像它应该的那样重命名。

不太确定我哪里出错了,我认为这是从数据库中获取的简单但似乎不是。

edit.php

<?php 
// Parse the form data and add inventory item to the system
if (isset($_POST['product_name'])) {

    $pid = mysql_real_escape_string($_POST['thisID']);
    $product_name = mysql_real_escape_string($_POST['product_name']);
    $price = mysql_real_escape_string($_POST['price']);
    $category = mysql_real_escape_string($_POST['category']);

    $details = mysql_real_escape_string($_POST['details']);
    $stock = mysql_real_escape_string($_POST['stock']);
    // See if that product name is an identical match to another product in the system
    $sql = mysqli_query($link,"UPDATE products SET product_name='$product_name', price='$price', details='$details', category='$category', stock='$stock' WHERE id='$pid'");
    if ($_FILES['fileField']['tmp_name'] != "") {
        // Place image in the folder 
        $newname = "$pid.jpg";
        move_uploaded_file($_FILES['fileField']['tmp_name'], "inventory_images/$newname");
    }
    header("location: inventory_list.php"); 
    exit();
}
?>
<?php 
// Gather this product's full information for inserting automatically into the edit form below on page
if (isset($_GET['pid'])) {
    $targetID = $_GET['pid'];
    $sql = mysqli_query($link,"SELECT * FROM products WHERE id='$targetID' LIMIT 1");
    $productCount = mysqli_num_rows($sql); // count the output amount
    if ($productCount > 0) {
        while($row = mysqli_fetch_array($sql)){ 

             $product_name = $row["product_name"];
             $price = $row["price"];
             $category = $row["category"];
             $details = $row["details"];
             $stock = $row["stock"];
             $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
        }
    } else {
        echo "Sorry it does not exist.";
        exit();
    }
}
?>
<?php 
// This block grabs the whole list for viewing
$product_list = "";
$sql = mysqli_query($link,"SELECT * FROM products ORDER BY date_added DESC");
$productCount = mysqli_num_rows($sql); // count the output amount
if ($productCount > 0) {
    while($row = mysqli_fetch_array($sql)){ 
             $id = $row["id"];
             $product_name = $row["product_name"];
             $price = $row["price"];
             $stock = $row["stock"];
             $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
             $product_list .= "Product ID: $id - <strong>$product_name</strong> - &pound;$price - Stock: $stock -<em>Added $date_added</em> &nbsp; &nbsp; &nbsp; <a href='inventory_edit.php?pid=$id'>edit</a> &bull; <a href='inventory_list.php?deleteid=$id'>delete</a><br />";

    }
} else {
    $product_list = "You have no products listed in your store yet";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Stock List</title>
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen" />
</head>

<body>
<div align="center" id="mainWrapper">
  <?php include_once("../customer/template_header.php");?>
  <div id="pageContent"><br />

<div class="center">
<div id="content">
  <div id="content_top"></div>

  <div id="content_main">
    <center>

    <div align="right" style="margin-right:32px;"><a href="inventory_list.php#inventoryForm">Add New Stock</a></div>
<div align="left" style="margin-left:24px;">
      <h2>Stock list</h2>
      <?php echo $product_list; ?>
    </div>
    <hr />
    <a name="inventoryForm" id="inventoryForm"></a>
    <h3>
     Edit Stock Form
    </h3>
    <form action="inventory_edit.php" enctype="multipart/form-data" name="myForm" id="myform" method="post">
    <table width="90%" border="0" cellspacing="0" cellpadding="6">
      <tr>
        <td width="20%" align="right">Product Name</td>
        <td width="80%"><label>
<input name="product_name" type="text" id="product_name" size="64" value='<?php echo $product_name; ?>' />
        </label></td>
      </tr>
      <tr>
        <td align="right">Product Price</td>
        <td><label>
          &pound;
          <input name="price" type="text" id="price" size="12" value='<?php echo $price ?>' />
        </label></td>
      </tr>
      <tr>
        <td align="right">Category</td>
        <td><label>
          <select name="category" id="category">

<?php 
$sql = mysql_query("SELECT category_Name FROM category");
while ($row = mysql_fetch_array($sql)){
echo '<option>' . $row['category_Name'] . "</option>";

}

?>
</select>
        </label></td>
      </tr>

      <tr>
        <td align="right">Product Details</td>
        <td><label>
          <textarea name="details" id="details" cols="64" rows="5"><?php echo $details; ?></textarea>
        </label></td>
      </tr>
      <tr>
        <td align="right">Product Image</td>
        <td><label>
          <input type="file" name="fileField" id="fileField" />
        </label></td>
      </tr>    
    <tr>
        <td width="20%" align="right">Number of Stock:</td>
        <td width="80%"><label>
          <input name="stock" type="text" id="stock" size="12" value="<?php echo $stock; ?>" />
        </label></td>
      </tr>       
      <tr>
        <td>&nbsp;</td>
        <td><label>
          <input name="thisID" type="hidden" value="<?php echo $targetID; ?>" />
          <input type="submit" name="button" id="button" value="Make Changes" />
        </label></td>
      </tr>
    </table>
    </form>
    <br />
  <br />

</center>

      </div>
  <div id="content_bottom"></div>
  <br />
</div>
</div>


  </div>
  <?php include_once("../customer/template_footer.php");?>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

我认为有一个问题,您首先使用当前编辑过的产品填充$id$product_name等变量,但是然后在代码部分再次填充它,列表。