Posted Date:11-07-2017

In this we will explain mysql_query insert using custom user created function

Stpe 1:Create basic html page with form

<form method="POST" action="" id="addproduct-form">
      
        
         <label for="hd-desc">Select Category</label>
          <select  name="cat_id" id="cat-id">
           <option value="" selected="">Select Category</option>
            <option value="8">mobile</option>
            <option value="9">Tv</option>
            <option value="10">XYZ</option>
            <option value="11">mobile2</option>
            <option value="12">Tv2</option>
            <option value="13">Tv 3</option>
            </select>
         
            <label for="hd-desc">Product Name</label>
            <input  id="product-name" name="product_name" placeholder="Enter Category" value="" type="text">
         
            <label for="hd-desc">Product Price</label>
            <input  id="product-price" name="product_price" placeholder="Enter Price" value="" type="text">
         
          	<!-- 	<input type="submit" name="save"> -->
              <button type="submit" name="save" class="btn btn-success" value="save" id="add-save">Save</button>
         
      
    </form>

 

Step 2:Create php connection string,query insert function and form submit action

<?php
  define("DB_HOST", "localhost");
 	define("DB_USER", "root");
 	define("DB_PSSWD", "");
 	define("DB_NAME", "test");
  $conn = mysql_connect(DB_HOST,DB_USER,DB_PSSWD);
 	mysql_select_db(DB_NAME, $conn);
function qry_insert($table, $data)
 {

      $fields = array_keys( $data );  
      $values = array_map( "mysql_real_escape_string", array_values( $data ) );
      return mysql_query( "INSERT INTO $table(".implode(",",$fields).") VALUES ('".implode("','", $values )."');") or die( mysql_error() );

  }
if(isset($_POST['save']))
{
   $data = array(
            'cat_id'     =>  $_POST['cat_id'],
            'pname'     =>   $_POST['product_name'],
            'price'  =>  $_POST['product_price']
        );
   $qry = qry_insert('product',$data);
}
?>

Step 3: Create table name as product using following code

CREATE TABLE `product` (  `pid` int(11) NOT NULL AUTO_INCREMENT,  `pname` varchar(100) NOT NULL,  `price` int(11) NOT NULL,  `pimg` varchar(100) NOT NULL,  `cat_id` int(11) NOT NULL,  PRIMARY KEY (`pid`)) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=latin1

Step 4: If you following above step you get final code like this

<?php
  define("DB_HOST", "localhost");
 	define("DB_USER", "root");
 	define("DB_PSSWD", "");
 	define("DB_NAME", "karthik");
  $conn = mysql_connect(DB_HOST,DB_USER,DB_PSSWD);
 	mysql_select_db(DB_NAME, $conn);
function qry_insert($table, $data)
 {

      $fields = array_keys( $data );  
      $values = array_map( "mysql_real_escape_string", array_values( $data ) );
      return mysql_query( "INSERT INTO $table(".implode(",",$fields).") VALUES ('".implode("','", $values )."');") or die( mysql_error() );

  }
if(isset($_POST['save']))
{
   $data = array(
            'cat_id'     =>  $_POST['cat_id'],
            'pname'     =>   $_POST['product_name'],
            'price'  =>  $_POST['product_price']
        );
   $qry = qry_insert('product',$data);
}
?>
<!DOCTYPE html>
<html>
<head>
  <title>Insert function</title>
</head>
<body>
<form method="POST" action="" id="addproduct-form">
      
        
         <label for="hd-desc">Select Category</label>
          <select  name="cat_id" id="cat-id">
           <option value="" selected="">Select Category</option>
            <option value="8">mobile</option>
            <option value="9">Tv</option>
            <option value="10">XYZ</option>
            <option value="11">mobile2</option>
            <option value="12">Tv2</option>
            <option value="13">Tv 3</option>
            </select>
            <label for="hd-desc">Product Name</label>
            <input  id="product-name" name="product_name" placeholder="Enter Category" value="" type="text">
         
            <label for="hd-desc">Product Price</label>
            <input  id="product-price" name="product_price" placeholder="Enter Price" value="" type="text">
         
          	<!-- 	<input type="submit" name="save"> -->
              <button type="submit" name="save" class="btn btn-success" value="save" id="add-save">Save</button>
         
      
    </form>
</body>
</html>

 

 

 

Leave a Reply