0

I'm trying to show an error while entering duplicates using php and mysql, but i'm not getting how to complete, please give an solution........

this is my code:

mysql_query(
"INSERT INTO productcost (product, productCategory, model, purchasePrice, mrp, customerPrice, marginCustomer, dealerPrice, marginDealer) 
VALUES ('" . $_POST["product"] . "','" . $_POST["productCategory"] . "','" . $_POST["model"] . "','" . $_POST["purchasePrice"] . "','" . $_POST["mrp"] . "','" . $_POST["customerPrice"] . "','" . $_POST["marginCustomer"] . "','" . $_POST["dealerPrice"] . "', '" . $_POST["marginDealer"] . "')");

$current_id = mysql_insert_id();

if(!empty($current_id)) {
$message = "New Product Added Successfully";
}

}

Sravan Kumar
  • 27
  • 1
  • 4

2 Answers2

-1

You have to create unique key in productcost table , using unique fields like (product, productCategory, model). Now execute insert query, if there is a recode in the table return error . now you can handle error and give message.

    try{ 
    mysql_query("INSERT INTO productcost (product_key_id,product, productCategory,model,purchasePrice, mrp, customerPrice, marginCustomer, dealerPrice, marginDealer) 
VALUES 
('" . $_POST["created_product_id"] . "','" . $_POST["product"] . "','".$_POST["productCategory"] . "','" . $_POST["model"] . "','".$_POST["purchasePrice"] . "','" . $_POST["mrp"] . "','".$_POST["customerPrice"] . "','" . $_POST["marginCustomer"] . "','".$_POST["dealerPrice"] . "', '" . $_POST["marginDealer"] . "')");

    return TRUE;
    }
    catch(Exception $e){
      return FALSE;
    }

or you can check is there a recode in table before insert

select count(*) as cc from doc_upload where product_key_id = $_POST["created_product_id"];
Tharanga
  • 81
  • 6
  • i'm completely new to coding, so is it require to add product_key_id into the database?? and i want to show the error message so how to show it please suggest.......... – Sravan Kumar Sep 24 '15 at 06:37
  • Yes Sravan, you have to add new column for product_key_id. you can create value for product_key_id using your product id, productCategory id and model id like this (product id = 10,productCategory id = 251 and model id = Q0111 now your product_key_id = '10251Q0111') . catch(Exception $e){ $errors = $e->mysql_error(); } – Tharanga Sep 24 '15 at 06:44
  • catch(Exception $e){ $errors = $e->mysql_error(); return $errors; } $errors is an array . – Tharanga Sep 24 '15 at 06:50
  • and what my concern is product name may be same for different models, if i enter same product twice with different model it should be stored in database......so i'm not getting correct solution for this, please suggest anyone.....can i use (mysql_errno()==1062) for this?? and how?? – Sravan Kumar Sep 24 '15 at 07:15
-2

To show an error message while entering duplicates:

// First check there are same data available or not using a query by counting the row
$sqlCheck = "SELECT COUNT(`id`) WHERE product = '" . $_POST["product"] . "' AND productCategory = '" . $_POST["productCategory"] . "' AND model = '" . $_POST["model"] . "'"; // You have to add mroe thing in where clause
$CheckQuery = mysql_query($sqlCheck);

// if there is no duplicate data
// 
if ($CheckQuery > 0) {
    # code...

mysql_query(
"INSERT INTO productcost (product, productCategory, model, purchasePrice, mrp, customerPrice, marginCustomer, dealerPrice, marginDealer) 
VALUES ('" . $_POST["product"] . "','" . $_POST["productCategory"] . "','" . $_POST["model"] . "','" . $_POST["purchasePrice"] . "','" . $_POST["mrp"] . "','" . $_POST["customerPrice"] . "','" . $_POST["marginCustomer"] . "','" . $_POST["dealerPrice"] . "', '" . $_POST["marginDealer"] . "')");

$current_id = mysql_insert_id();

if(!empty($current_id)) {

    $message = "New Product Added Successfully";

}

} else {

        $message = "Data is Duplicated";
}

Note : I'm Giving you an Example . this is how you have to check duplicate data

Aniruddha Chakraborty
  • 1,738
  • 1
  • 17
  • 29
  • You're doing 2 round trips to the database and two primary key look-ups here. A more standard way of doing this would be to attempt the INSERT and if there's an error catch the error and display the error messag.e – Ben Sep 23 '15 at 12:13
  • Thank you anirud............but Ben is suggesting to try and catch blocks......can you please suggest on this...... – Sravan Kumar Sep 24 '15 at 06:38