Jump to content

[Help] Problem With Inserting Data


Diether

Recommended Posts

Hi guys Good day, my problem is this. everytime i click the "add this item now" button. the data is not insert in my database. is there anyway to isolate my problem in codes? there is also no error appears. thats why i cant figured out whats the problem. please help me guys. thanks in advance

 

here is my form screenshot:

16987910e81e235ac09107c89dfbf7ed876e2ba2.jpg

 

my codes:

 

<?php
// This block grabs the whole list for viewing
$product_list = "";
$sql = mysql_query("SELECT * FROM product
								 ORDER BY date_added DESC");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
	 while($row = mysql_fetch_array($sql)){
			 $id = $row["id"];
						 $product_name = $row["name"];
						 $price = $row["price"];
						 $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
						 $product_list .= "Product ID: $id - <strong>$product_name</strong> - $$price - <em>Added $date_added</em>
															       <a href='inventory_edit.php?pid=$id'>edit</a> •
																 <a href='inventory_list.php?deleteid=$id'>delete</a><br />";
			 }
					 } else {
							 $product_list = "You have no products listed in your store yet";
					 }


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

 $product_name = mysql_real_escape_string($_POST['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']);
	 // See if that product name is an identical match to another product in the system
	 $sql = mysql_query("SELECT id FROM product WHERE name='$product_name' LIMIT 1");
	 $productMatch = mysql_num_rows($sql); // count the output amount
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();
	 }
	 // Add this product into the database now
	 $sql = mysql_query("INSERT INTO product (name, price,
												 details,date_added)
		 VALUES('$product_name','$price',now())")
							 or die (mysql_error());
 $pid = mysql_insert_id();
	 // Place image in the folder
	 $newname = $pid. '.jpg';
	 move_uploaded_file($_FILES['fileField']['tmp_name'], "../inventory_images/".$newname);
	 header("location: inventory_list.php");
 exit();
}
?>

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

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
 <head><title>Inventory</title></head>
<style type="text/css">
<!--
.style1 {font-size: 10px}
-->
</style></head>
<link rel="stylesheet" href="../stylesheet/style.css" type="text/css" media="screen" />

<body>

<div align = "center"id="wrapper">
	 <div id="content"><br/>
 <div align = "right" style="margin-right:140px;">
 <a href="inventory_list.php#inventory_form">+ Add new inventory item</a>
 </div>
 <div align= "left" style = "margin-left:140px;">
 <h2>Inventory List</h2>
	 <?php echo $product_list; ?>
</div>
	 <div align="left">
		 <blockquote>
		 <blockquote>
		 <blockquote>
		 <blockquote>

		 <p id="inventory_form" name="inventory_form"><a name = "inventory_form" id="inventory_form"></a>			 </p>
		 </blockquote>
		 </blockquote>
		 </blockquote>
		 </blockquote>
	 </div>
		 <h3>Add New Inventory Item Form</h3>
	 <form id="my_form" name="my_form" method="post" action="inventory_list.php" enctype="multipart/form-data">
	 <table width="633" height="248" border="1">
	 <tr>
	 <td width="156"><label>Product Name:</label>
		  </td>
	 <td width="425"><input type="text" name="product_name" id="product_name" /></td>
	 </tr>
	 <tr>
	 <td><label>Product Price:</label></td>
	 <td>$
		 <input type="text" name="price" id="price" /></td>
	 </tr>


	 <tr>
	 <td height="45">Product Details:</td>
	 <td>
				 <textarea name="details" id="details"></textarea></td>
	 </tr>
	 <tr>
	 <td>Product Image:</td>
	 <td>
				 <input type="file" name="fileField" id="fileField" />
	 </td>
	 </tr>
	 <tr>
	 <td> </td>
	 <td>
			 <input type="submit" name="submit" id="submit" value="Add this item now" /></td>
	 </tr>
		 </table>

	 </form>
		 <p> </p>
		 <p> </p>
		 <p> </p>
		 <p><br/>
			 </p>
			 </div>




</div>
</body>
</html>
<?php include("../includes/footer.php"); ?>

Edited by Diether
Link to comment
Share on other sites

I second what Christian F. states above. Also, I would like to add something about your insert query:

$sql = mysql_query("INSERT INTO product (name, price, details, date_added) VALUES('$product_name','$price',now())");

 

You've listed four fields after 'product', but you only list three variables after 'Values'.

Edited by parkerj
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.