无法从Form保存数据库中的其他数据

时间:2014-09-24 14:59:47

标签: javascript php html

我试图找到答案,但没有办法。我做了一个表格,接受产品名称,价格,类别,子类别,细节和图像等数据。此数据将保存在数据库中。因为我保存它没有错误的方法,我检查我的数据库是否已保存,但价格和详细信息不是。我该怎么办?请帮帮我。

这是PHP的代码

 <?php 
 session_start();
 if(!isset($_SESSION["manager"])){
     header("location:../storeadmin/admin_login.php");
     exit();
 }

 $managerID = preg_replace('#[^0-9]#i',"",$_SESSION["id"]);
 $manager = preg_replace('#[^A-Za-z0-9]#i',"",$_SESSION["manager"]);
 $password = preg_replace('#[^A-Za-z0-9]#i',"",$_SESSION["password"]);

 include "../storeconnections/connect_to_mysql.php";
 $sql = mysql_query("SELECT * FROM admin WHERE id='$managerID' AND username='$manager' AND password='$password' LIMIT 1");

 $existCount = mysql_num_rows($sql);
 if($existCount == 0){

     echo 'Your login session data is not on record in the database.';
     exit();    
 }
 ?>
 <?php
 //Script Error Reporting
 error_reporting(E_ALL);
 ini_set('display_errors','1')
 ?>
 <?php
  //Delete Item Question to Admin and delete product if they choose
 if(isset($_GET['deleteid'])){
    echo'Do you really want to delete product '.$_GET['deleteid'].'?<a href="inventory_list.php?yesdelete='.$_GET['deleteid'].'">Yes</a> | <a href="inventory_list.php">No</a>';
     exit();    
 }
 if(isset($_GET['yesdelete'])){
 //remove item from system and delete its pic
 //delete from database
 $id_to_delete = $_GET['yesdelete'];
 $sql = mysql_query("DELETE FROM products WHERE id='$id_to_delete' LIMIT 1") or die(mysql_error());
 //unlink from server
 //remove picture
 $pictodelete = ("../inventory_images/$id_to_delete.png");
 if(file_exists($pictodelete)){
     unlink($pictodelete);  
     }
     header("location:inventory_list.php");
     exit();
 }
 ?>
 <?php
 //Parse the form data and add inventory item to the system
 if(isset($_POST['product_name'])){
     $product_name = mysql_real_escape_string($_POST['product_name']);
     $price = mysql_real_escape_string($_POST['price']);
     $category = mysql_real_escape_string($_POST['category']);
     $subcategory = mysql_real_escape_string($_POST['subcategory']);
     $details = mysql_real_escape_string($_POST['details']);

     $sql = mysql_query("SELECT id FROM products WHERE product_name='$product_name' LIMIT 1");
     $productMatch = mysql_num_rows($sql);
     if($productMatch > 0){
         echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a href="inventory_list.php">Click here.</a>';
         exit();
      }

         $sql = mysql_query("INSERT INTO products(product_name, price, details, category, subcategory, date_added)
    VALUES('$product_name','$price','$details','$category','$subcategory',now())") or die(mysql_error());
         $pid = mysql_insert_id();

         $newname = "$pid.png";
         move_uploaded_file($_FILES['fileField']['tmp_name'],"../inventory_images/$newname");
         header("location:inventory_list.php");
         exit();
 }
 ?>
 <?php
 //This block grabs the whole list for viewing
 $product_list = "";
 $sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC");
 $productCount = mysql_num_rows($sql);
 if($productCount > 0){
     while($row = mysql_fetch_array($sql)){
         $id = $row['id'];
         $product_name = $row['product_name'];
         $price = $row['price'];
         $date_added = strftime("%b %d, %Y", strtotime($row['date_added']));

         $product_list .= "#$id | <strong> $product_name</strong> | Php.$price | <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 product listed in your store yet";
     }
 ?>

以下是HTML中的Javasccript代码

 <script type="text/javascript" language="javascript">

 function validateForm(){
     var isValid = true;
      if(document.myForm.product_name.value == ""){
         alert("Please speify product name.");
         isValid = false;
     }else if(document.myForm.textfield.value == ""){
         alert("Please specify price.");
         isValid = false;
     }else if(document.myForm.category.value == ""){
         alert("Please specify category.");
         isValid = false;
     }else if(document.myForm.subcategory.value == ""){
         alert("Please specify subcategory.");
         isValid = false;
     }else if(document.myForm.textarea.value == ""){
         alert("Please specify product details.");
         isValid = false;
     }else if(document.myForm.fileField.value == ""){
         alert("Please specify product image.");
         isValid = false;
     }
         return isValid;
 }
 </script>

以下是HTML格式的代码

 <a name="inventoryForm" id="inventoryForm"></a>
      <h3>
      Add New Item Form
      </h3>
      <form onSubmit="return validateForm()" action="inventory_list.php" enctype="multipart/form-data" name="myForm" id="myForm" method="post">
      <table width="90%" border="0" cellspacing="0" cellpadding="6">
      <tr>
      <td align="right" width="35%">Product Name</td>
      <td width="65%"><label>
      <input name="product_name" type="text" id="product_name" size="64"/>
      </label></td>
      </tr>

      <tr>
      <td align="right">Product Price</td>
      <td><label>
      Php.
      <input name="textfield" type="text" id="textfield" size="12"/>
      </label></td>
      </tr>

      <tr>
      <td align="right">Category</td>
      <td>
      <select name="category" id="category">
      <option value=""></option>
      <option value="Shirts">Shirts</option>
      <option value="Tote Bags">Tote Bags</option>
      <option value="Mugs">Mugs</option>
      <option value="Pillow Case">Pillow Case</option>
      </select></td>
      </tr>

      <tr>
      <td align="right">Subcategory</td>
      <td><select name="subcategory" id="subcategory">
      <option value=""></option>
      <option value="Silk Screen">Silk Screen</option>
      <option value="Sublimation Print">Sublimation Print</option>
      <option value="Vinyl Heat Transfer">Vinyl Heat Transfer</option>
      <option value="Water slide">Water Slide</option>
      <option value="Dark Color Heat Transfer">Dark Color Heat Transfer</option>
      <option value="Light Color Heat Transfer">Dark Color Heat Transfer</option>
      </select></td>
      </tr>

      <tr>
      <td align="right">Product Details</td>
      <td><label>
      <textarea name="textarea" id="textarea" cols="64" rows="5"></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>&nbsp;</td>
      <td><label>
      <input type="submit" name="button" id="button" value="Add Item"/>
      </label></td>
      </tr>
      </table>
      </form>
      <br>
      <br>
      <br>
 </div>

1 个答案:

答案 0 :(得分:1)

您将输入字段命名为textareatextfield

<input name="textfield" type="text" id="textfield" size="12"/>
<textarea name="textarea" id="textarea" cols="64" rows="5"></textarea>

但您尝试从pricedetails抓取发布的数据:

$price = mysql_real_escape_string($_POST['price']);
$details = mysql_real_escape_string($_POST['details']);

确保您为输入字段指定的名称具有描述性和准确性,并确保它们在客户端和服务器上保持一致。


此外,请勿对输入字段使用保留元素名称。在这种情况下,button是现有保留html元素的名称。 submit也不能用作元素的名称。

<input type="submit" name="button" id="button" value="Add Item"/>