I have the following query:
select * from shoppertrack;
on column SELECTEDITEM, the result is:
g45, LDR01, TYU
I want to do some validation before adding a new item so there wont be any duplicates. I have hyperlinks that pass the variable to the page where the item is added. How would I create a script that echoes the message “Item already selected” when for example, someone selects g45?
This is what I have so far but doesn’t work:
$sessionid = session_id();
//PREVENT DUPLICATION
$checkSession = "SELECT sessionid FROM SHOPPERTRACK";
if(!$checkSession){ exit("<p class='alert'>Could not get database information during duplicate prevention.</p>"); }
$checkSessionResult = mysql_query($checkSession) or die("<p class='alert'>Could not get database information during duplicate prevention.</p>");
while($rowSession = mysql_fetch_array($checkSessionResult))
{
$rowSessionid = $rowSession["sessionid"];
//PREVENT DUPLICATION
$check = "SELECT selecteditem FROM SHOPPERTRACK WHERE sessionid = '$sessionid'";
if(!$check){ exit("<p class='alert'>Could not get database information during duplicate prevention.</p>"); }
$checkResult = mysql_query($check) or die("<p class='alert'>Could not get database information during duplicate prevention.</p>");
while($row = mysql_fetch_array($checkResult))
{
$productidCheck = $row["selecteditem"];
if($productidCheck == $productid AND $sessionid == $rowSessionid)
{
$productidCheck = strtoupper($productidCheck);
exit("<p class='alert'>Product: " . $productidCheck . " has already been added to shopping cart. Please go back and continue.</p>");
} }
}
$checkSession = "SELECT * FROM SHOPPERTRACK";
if(!$checkSession){ exit("<p class='alert'>Could not get database information during duplicate prevention.</p>"); }
$checkSessionResult = mysql_query($checkSession) or die("<p class='alert'>Could not get database information during duplicate prevention.</p>");
$number_of_rows = mysql_num_rows($checkSessionResult);
while($rowSession = mysql_fetch_array($checkSessionResult))
{
$rowSessionid = $rowSession["sessionid"];
if($sessionid == $rowSessionid)
{
$selecteditemRow = $rowSession["selecteditem"];
$quantityRow = $rowSession["quantity"];
$priceRow = $rowSession["PRICE"];
echo "quantityRow: " . $quantityRow . "<p>";
$quantity = 1;
$updateSelecteditem = $selecteditemRow . ", " . $productid;
$updateQuantity = $quantityRow . ", " . $quantity;
$updatePrice = $priceRow . ", " . $price;
echo "item: " . $updateSelecteditem . "<BR>";
echo "quantity: " . $updateQuantity . "<BR>";
echo "price: " . $updatePrice . "<BR>";
$sql = "UPDATE shoppertrack SET " .
"selecteditem = '$updateSelecteditem', " .
"PRICE = '$updatePrice', " .
"quantity = '$updateQuantity', " .
"timeadded = CURTIME(), " .
"dateadded = CURDATE() " .
"WHERE sessionid = '$sessionid'";
$result = mysql_db_query('magalys', $sql);
if($result)
{ echo("<p class='success'>62: Item successfully added to database.</p><p><a href='showCart.php?customerid=" .
$customerid . "'>Click here to view your shopping cart.</a></p>"); }
else { echo("<p class='alert'>Error. Please go back and try again. " . __line__ . "</p>"); }
}
else
{
$number_of_rows++;
echo "Number of rows: " . $number_of_rows . "<p>";
if($number_of_rows == 1)
{
echo("<p>INSERT</p>");
$sql = "INSERT INTO shoppertrack SET " .
"sessionid = '$sessionid', " .
"selecteditem = '$productid', " .
"customerid = '$customerid', " .
"quantity = 1, " .
"price = '$price', " .
"dateadded = curdate(), " .
"timeadded = curtime()" ;
$result = mysql_db_query('magalys', $sql);
if($result)
{ echo("<p class='success'>101: Item successfully added to database.</p><p><a href='showCart.php?customerid=" .
$customerid . "'>Click here to view your shopping cart.</a></p>"); }
else { echo("<p class='alert'>Error. Please go back and try again.103</p>"); }
}
} // END WHILE
}