DeniseD.
DeniseD.

Reputation: 1

Can't save other data in database from Form

I've tried to find answers to this but nothing will do. I made a form which accept data like product name, price, category, subcategory, details and image. This data will be save on a database. As I save it no error approach, I check my database if it's been save but the PRICE and DETAILS are not. What should I do? Please help me.

Here's the code for 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";
     }
 ?>

Here's the code for Javasccript in HTML

 <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>

Here's the code for FORM in 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>

Upvotes: 0

Views: 94

Answers (1)

000
000

Reputation: 27227

You named your input fields textarea and textfield:

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

But you try to grab the posted data from price and details:

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

Make sure the names you give your input fields are descriptive and accurate, and make sure they are consistent across the client and server.


Further, do not use reserved element names for your input fields. In this case, button is the name of an existing reserved html element. submit also can not be used as a name for an element.

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

Upvotes: 1

Related Questions