1 Replies - 587 Views - Last Post: 29 May 2012 - 10:01 AM Rate Topic: -----

#1 nozon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-December 11

Add new category to the system

Posted 29 May 2012 - 09:03 AM

Good evening,

I want to create php code to add new categories to the system, which allow th admin to manage the categories.
I try the following way but does not work with me, I cannot find where is the error in the code

In the database I have two tables (User,Category), which the User_ID is FK

My php code is as the following:

<?php
session_start();
error_reporting(0);
echo "Welcome!<br><a href ='logout.php'>Logout</a></br>";  
?>

<?php 
if (isset($_POST['Category_Name'])){
$Category_Name = strip_tags($_POST['Category_Name']);
$Category_Quantity = strip_tags($_POST['Category_Quantity']);


//see if that product name is an identical match to another product in the system
$connect = mysql_connect("localhost","root","") or die("Cannot connect");
mysql_select_db("poly_database") or die ("Could not find database");

$sql = mysql_query("SELECT Category_Name From Category WHERE Category_Name ='$Category_Name'");
$categoryMatch = mysql_num_rows($sql);//count the output amount
if($categoryMatch != 0){
	echo 'Sorry you tried to place a duplicate "Category_Name" into the system <a href="inventory_categories.php">Click here</a>';
	
}

if($Category_Name&&$Category_Quantity){

// Insert tha data in the database
$sql1 = mysql_query("INSERT INTO Category(Category_Name, Category_Quantity) VALUES ('$Category_Name','$Category_Quantity'");
$result = mysql_query($sql1,$connect);
$Category_ID = mysql_insert_id($connect);
		  mysql_free_result($result);
}

else
echo"The category not added to the system";
}
?>
<?php 
//This block grables the whole list of viewing  
$Category_list = "";
$sql = mysql_query("SELECT * FROM Category");
$CategoryCount = mysql_num_rows($sql); //Count the output amount
if ($CategoryCount > 0){
	while($row = mysql_fetch_array($sql)){
		$Category_ID = $row["Category_ID"];
		$Category_Name = $row["Category_Name"];
		$Category_list = "$Category_ID - $Category_Name &nbsp;&nbsp;<a href='#'>Edit</a>&bull; <a href='#'>Delete</a><br/>";
	}
	
}else{
$product_list ="You have no products listed in your store yet";
}
?>



The HTML form for adding new Category

<!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>Inventory</title>
<link href="Style/style.css" rel="stylesheet" type="text/css" />
</head>

<body>

<div align="center" id="mainWarapper">
<?php include_once("Header.php");?>
  <div id="PageContent"><br/>
  
  <form>
      <table width="983" height="538" border="1">
      <tr>
    <th height="532" valign="top" scope="col">
    <div align="right" style="margin-right:32px"><a href="inventory_categories.php#categoriesForm">+Add New Categories</a></div>
    <div align="left" style="margin-left:24px">
    <h3>Inventory List </h3>
   <?php echo  $Category_list; ?>
   </div>
   <a name="categoriesForm" id="categoriesForm"></a>
    <h4><br/>
      <br/>
      Categories From</h4>
      <form action="inventory_categories.php" enctype="multipart/form-data" name="categoriesForm" id="categoriesForm" method="post">
    <table width="648" height="273" border="1" align="center" dir="ltr">
      <tr>
        <td width="175">Category Name</td>
        <td width="457"><label for="Category_Name"></label>
          <input name="Category_Name" type="text" id="Category_Name" value="" align="left" size="50px" /></td>
      </tr>
      <tr>
      <td width="175">Category Quantity</td>
        <td width="457"><label for="Category_Quantity"></label>
          <input name="Category_Quantity" type="text" id="Category_Quantity" value="" align="left" size="50px" /></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td><input type="submit" name="submit" id="Add" value="Add" /></td>
      </tr>
    </table>
    <p><br/>
    </p>
    </tr>
  
  </form>
  </form>

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



I would be most grateful fo helping me to find a solution for my proplem

Thank you in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Add new category to the system

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3402
  • View blog
  • Posts: 9,617
  • Joined: 08-June 10

Re: Add new category to the system

Posted 29 May 2012 - 10:01 AM

if there is a MySQL error, you can get that by mysql_error(). it is impossible for us to resolve any FK errors without knowing the table definitions, that set up the FK.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1