Insert and Display form Data Using JOIN in PHP

Create table ‘category’


CREATE TABLE category
(
c_id int AUTO_INCREMENT,
c_name varchar(255),
b_name varchar(255),
PRIMARY KEY (c_id)
);

Create table ‘product’


CREATE TABLE product
(
p_id INT(11) AUTO_INCREMENT,
c_id INT(11),    
p_name varchar(255),
p_price INT(11),
p_color varchar(255),
PRIMARY KEY (p_id)
);

Create “category.php”


<?PHP include 'connect.php'; ?>

<form method="post">
  Product Category
  <input type="text" name="category">
  </br>
  </br>
  Brand Name
  <input type="text" name="bname">
  </br>
  </br>
  <input type="submit" name="add_category" value="Add Category" />
</form>
<?php 
if(isset($_POST['add_category'])){
	$product_category = $_POST['category'];
	$brand_name = $_POST['bname'];
	$query2 = "INSERT INTO `category` (`c_name`,`b_name`) VALUES ('".$product_category."','".$brand_name."')";
	$result2 = mysql_query($query2) or die(mysql_error());
}

?>

Create “product.php”


<?php include 'connect.php'; ?>

<form method="post">
  Category Name
  <?php 
  $category = "SELECT c_name, c_id FROM category";
	$cat_result = mysql_query($category);
	?>
  <select name="category">
    <?php
	while($cat_row = mysql_fetch_array($cat_result)) { ?>
    <option value="<?php echo $cat_row['c_id']; ?>"><?php echo $cat_row['c_name']; ?></option>
    <?php }	?>
  </select>
  </br>
  </br>
  Product Name
  <input type="text" name="pname">
  </br>
  </br>
  Product Price
  <input type="text" name="price">
  </br>
  </br>
  Product Color
  <input type="checkbox" value="red" name="color[]" />
  Red,
  <input type="checkbox" value="blue" name="color[]" />
  Blue,
  <input type="checkbox" value="black" name="color[]" />
  Black</br>
  </br>
  <input type="submit" name="add_product" value="Add Product" />
</form>
<?php
if(isset($_POST['add_product'])){
	$c_id = $_POST['category'];
	$product_name = $_POST['pname'];
	$product_price = $_POST['price'];
	$product_color = implode(', ',$_POST['color']);
	
	$query1 = "INSERT INTO `product` (`c_id`,`p_name`,p_price,p_color) VALUES ('".$c_id."','".$product_name."','".$product_price."','".$product_color."')";
	$result1 = mysql_query($query1) or die(mysql_error());
}

?>

Create “result.php”


<?php
include 'connect.php';
$query = "SELECT product.*, category.* 
		  FROM product 
		  JOIN category 
		  ON product.c_id=category.c_id";	  

$result = mysql_query($query);

?>

<table border="1">
<tr>
  <td>Product ID</td>
  <td>Product Name</td>
  <td>Product Price</td>
  <td>Product Color</td>
  <td>Product Category ID</td>
  <td>Product Category </td>
  <td>Brand Name</td>
</tr>
<?php while($row = mysql_fetch_array($result)) { ?>
<tr>
  <td><?php echo $row['p_id']; ?></td>
  <td><?php echo $row['p_name']; ?></td>
  <td><?php echo $row['p_price']; ?></td>
  <td><?php echo $row['p_color']; ?></td>
  <td><?php echo $row['c_id']; ?></td>
  <td><?php echo $row['c_name']; ?></td>
  <td><?php echo $row['b_name']; ?></td>
</tr>
<?php } ?>

Create “connect.php”


<?php

$dbname = 'php';
$dbuser = 'root';
$dbpass = '';
$dbhost = 'localhost';

$con = mysql_connect($dbhost,$dbuser,$dbpass);
$db =  mysql_select_db($dbname, $con) or die(mysql_error());
 
?>

Advertisements

Tagged:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: