duplicate key Duplicate entry '9549' for key 'PRIMARY'

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

duplicate key Duplicate entry '9549' for key 'PRIMARY'

Post by jonnyfortis »

getting an error on a shopping cart.

i need to write a statement to check if the order exists already so if it does i dont run the INSERT query it just goes to the gateway

what i have so far does not work, im still getting the Duplicate error

Code: Select all

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

//check if an order already exists
$checkOrderID = $_SESSION['OrderID'];

mysql_select_db($database_beauSS15, $beauSS15);
$query_rsOrder = "SELECT OrderID FROM beau_orders WHERE OrderID = %s LIMIT 1";
$query_rsOrder = sprintf($query_rsOrder, $checkOrderID);
$results = mysql_query($query_rsOrder);
if(mysql_num_rows($results) > 0)
{
	$row = mysql_fetch_array($results);
	$checkOrderID = $row_rsOrder['OrderID'];
	}

else

{
//end check if an order already exists
}
//additional bracket for check if an order already exists

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO beau_orders (OrderID, CustomerID, OrderDate, Shipping, Discount, Tax, Total, TransactResult, imx) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['OrderID'], "text"),
                       GetSQLValueString($_POST['CustomerID'], "int"),
                       GetSQLValueString($_POST['OrderDate'], "date"),
                       GetSQLValueString($_POST['Shipping'], "double"),
                       GetSQLValueString($_POST['Discount'], "double"),
                       GetSQLValueString($_POST['Tax'], "double"),
					   GetSQLValueString($_POST['XC_Amount'], "double"),
					   GetSQLValueString($_POST['TransactResult'], "text"),
					   GetSQLValueString($_POST['imx'], "int"));

  mysql_select_db($database_beauSS15, $beauSS15);
  $Result1 = mysql_query($insertSQL, $beauSS15) or die(mysql_error());
}
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: duplicate key Duplicate entry '9549' for key 'PRIMARY'

Post by Celauran »

You've got two separate blocks of code that don't actually have anything to do with each other.

Code: Select all

//check if an order already exists
$checkOrderID = $_SESSION['OrderID'];

mysql_select_db($database_beauSS15, $beauSS15);
$query_rsOrder = "SELECT OrderID FROM beau_orders WHERE OrderID = %s LIMIT 1";
$query_rsOrder = sprintf($query_rsOrder, $checkOrderID);
$results = mysql_query($query_rsOrder);
if(mysql_num_rows($results) > 0)
{
        $row = mysql_fetch_array($results);
        $checkOrderID = $row_rsOrder['OrderID']; // $row_rsOrder is not defined, $checkOrderID is not used again
}
else
{
//end check if an order already exists
}
This next bit has nothing at all to do with the first:

Code: Select all

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { // None of the variables above are checked AND you're querying a different table
  $insertSQL = sprintf("INSERT INTO beau_orders (OrderID, CustomerID, OrderDate, Shipping, Discount, Tax, Total, TransactResult, imx) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['OrderID'], "text"),
                       GetSQLValueString($_POST['CustomerID'], "int"),
                       GetSQLValueString($_POST['OrderDate'], "date"),
                       GetSQLValueString($_POST['Shipping'], "double"),
                       GetSQLValueString($_POST['Discount'], "double"),
                       GetSQLValueString($_POST['Tax'], "double"),
                                           GetSQLValueString($_POST['XC_Amount'], "double"),
                                           GetSQLValueString($_POST['TransactResult'], "text"),
                                           GetSQLValueString($_POST['imx'], "int"));
// $_POST everywhere. What purpose is $checkOrderID serving?
  mysql_select_db($database_beauSS15, $beauSS15);
  $Result1 = mysql_query($insertSQL, $beauSS15) or die(mysql_error());
}
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: duplicate key Duplicate entry '9549' for key 'PRIMARY'

Post by jonnyfortis »

Have i put the brackets in the wrong place

should it be

Code: Select all

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

//check if an order already exists
$checkOrderID = $_SESSION['OrderID'];

mysql_select_db($database_beauSS15, $beauSS15);
$query_rsOrder = "SELECT OrderID FROM beau_orders WHERE OrderID = %s LIMIT 1";
$query_rsOrder = sprintf($query_rsOrder, $checkOrderID);
$results = mysql_query($query_rsOrder);
if(mysql_num_rows($results) > 0)
{
	$row = mysql_fetch_array($results);
	$checkOrderID = $row_rsOrder['OrderID'];
	}

else

{
//end check if an order already exists

//additional bracket for check if an order already exists

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO beauloves_orders (OrderID, CustomerID, OrderDate, Shipping, Discount, Tax, Total, TransactResult, imx) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['OrderID'], "text"),
                       GetSQLValueString($_POST['CustomerID'], "int"),
                       GetSQLValueString($_POST['OrderDate'], "date"),
                       GetSQLValueString($_POST['Shipping'], "double"),
                       GetSQLValueString($_POST['Discount'], "double"),
                       GetSQLValueString($_POST['Tax'], "double"),
					   GetSQLValueString($_POST['XC_Amount'], "double"),
					   GetSQLValueString($_POST['TransactResult'], "text"),
					   GetSQLValueString($_POST['imx'], "int"));

  mysql_select_db($database_beauSS15, $beauSS15);
  $Result1 = mysql_query($insertSQL, $beauSS15) or die(mysql_error());
}
}
Celauran wrote:You've got two separate blocks of code that don't actually have anything to do with each other.

Code: Select all

//check if an order already exists
$checkOrderID = $_SESSION['OrderID'];

mysql_select_db($database_beauSS15, $beauSS15);
$query_rsOrder = "SELECT OrderID FROM beau_orders WHERE OrderID = %s LIMIT 1";
$query_rsOrder = sprintf($query_rsOrder, $checkOrderID);
$results = mysql_query($query_rsOrder);
if(mysql_num_rows($results) > 0)
{
        $row = mysql_fetch_array($results);
        $checkOrderID = $row_rsOrder['OrderID']; // $row_rsOrder is not defined, $checkOrderID is not used again
}
else
{
//end check if an order already exists
}
This next bit has nothing at all to do with the first:

Code: Select all

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { // None of the variables above are checked AND you're querying a different table
  $insertSQL = sprintf("INSERT INTO beau_orders (OrderID, CustomerID, OrderDate, Shipping, Discount, Tax, Total, TransactResult, imx) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['OrderID'], "text"),
                       GetSQLValueString($_POST['CustomerID'], "int"),
                       GetSQLValueString($_POST['OrderDate'], "date"),
                       GetSQLValueString($_POST['Shipping'], "double"),
                       GetSQLValueString($_POST['Discount'], "double"),
                       GetSQLValueString($_POST['Tax'], "double"),
                                           GetSQLValueString($_POST['XC_Amount'], "double"),
                                           GetSQLValueString($_POST['TransactResult'], "text"),
                                           GetSQLValueString($_POST['imx'], "int"));
// $_POST everywhere. What purpose is $checkOrderID serving?
  mysql_select_db($database_beauSS15, $beauSS15);
  $Result1 = mysql_query($insertSQL, $beauSS15) or die(mysql_error());
}
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: duplicate key Duplicate entry '9549' for key 'PRIMARY'

Post by Celauran »

This does nothing to change the fact that you're checking a session value against one table and then trying to insert different post values into another table.
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: duplicate key Duplicate entry '9549' for key 'PRIMARY'

Post by jonnyfortis »

Celauran wrote:This does nothing to change the fact that you're checking a session value against one table and then trying to insert different post values into another table.
was looking at the wrong table it should have read

Code: Select all

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

//check if an order already exists
$checkOrderID = $_SESSION['OrderID'];

mysql_select_db($database_beauSS15, $beauSS15);
$query_rsOrder = "SELECT OrderID FROM beau_orders WHERE OrderID = %s LIMIT 1";
$query_rsOrder = sprintf($query_rsOrder, $checkOrderID);
$results = mysql_query($query_rsOrder);
if(mysql_num_rows($results) > 0)
{
	$row = mysql_fetch_array($results);
	$checkOrderID = $row_rsOrder['OrderID'];
	}

else

{
//end check if an order already exists

//additional bracket for check if an order already exists

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO beau_orders (OrderID, CustomerID, OrderDate, Shipping, Discount, Tax, Total, TransactResult, imx) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['OrderID'], "text"),
                       GetSQLValueString($_POST['CustomerID'], "int"),
                       GetSQLValueString($_POST['OrderDate'], "date"),
                       GetSQLValueString($_POST['Shipping'], "double"),
                       GetSQLValueString($_POST['Discount'], "double"),
                       GetSQLValueString($_POST['Tax'], "double"),
					   GetSQLValueString($_POST['XC_Amount'], "double"),
					   GetSQLValueString($_POST['TransactResult'], "text"),
					   GetSQLValueString($_POST['imx'], "int"));

  mysql_select_db($database_beauSS15, $beauSS15);
  $Result1 = mysql_query($insertSQL, $beauSS15) or die(mysql_error());
}
}
i have just seen your comments

// $_POST everywhere. What purpose is $checkOrderID serving?

this is a variable for $_SESSION['OrderID'];
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: duplicate key Duplicate entry '9549' for key 'PRIMARY'

Post by Celauran »

Code: Select all

$checkOrderID = $_SESSION['OrderID'];

mysql_select_db($database_beauSS15, $beauSS15);
$query_rsOrder = "SELECT OrderID FROM beau_orders WHERE OrderID = %s LIMIT 1";
$query_rsOrder = sprintf($query_rsOrder, $checkOrderID);

Code: Select all

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO beau_orders (OrderID, CustomerID, OrderDate, Shipping, Discount, Tax, Total, TransactResult, imx) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['OrderID'], "text"),
I don't even know what else to say. Checking whether a specific session value exists has absolutely no bearing on a separate post value. If you're inserting from $_POST and you want to make sure the entry doesn't already exist, you need to check the same $_POST value.
Post Reply