Skip to main content
Inspiring
June 20, 2013
Question

php mySQL filtering problem...really desperate

  • June 20, 2013
  • 1 reply
  • 3142 views

Hi

I have a live site that is currently causing me some issues

It is a fashion site that sends the product information that is pulled in from sevaral table to the checkout.. the issue i am having is the incorrect stock id is being sent to the checkout so when it returns from the gateway it is updating the wrong stock levels.

What is happening currently is the stockID from the first size is being sent.

what i have is the following tables

table 1 - beauSS13_products

ProductID

CatID

table 2 - beauSS13_Cat

catID

table3 - beauSS13_Stock

StockID

ID (this was named like this incorrectly) but this joins with ProductID

SizeID

Stock

beauSS13_SizeList

SizeID

Size

the SQL is

$var1_rsProduct = "-1";

if (isset($_GET['ProductID'])) {

  $var1_rsProduct = $_GET['ProductID'];

}

mysql_select_db($database_beau, $beau);

$query_rsProduct = sprintf("SELECT * FROM beauSS13_Cat, beauSS13_products, beauSS13_Stock, beauSS13_SizeList WHERE beauSS13_products.CatID = beauSS13_Cat.catID AND beauSS13_products.ProductID = beauSS13_Stock.ID AND beauSS13_Stock.SizeID = beauSS13_SizeList.SizeID AND beauSS13_products.ProductID = %s AND beauSS13_Stock.Stock != 0 ", GetSQLValueString($var1_rsProduct, "int"));

i think beauSS13_products.ProductID = beauSS13_Stock.ID shouldnt be there ( but am not sure what it should have )

I need to identify the Unique Stock ID from table3 - beauSS13_Stock

basically cant get my head round the logic...

if i remove beauSS13_products.ProductID = beauSS13_Stock.ID

then in the

select list

<select name="SelectSize" id="SelectSize">

        <option value="Select Size">Select Size</option>

        <?php

do { 

?>

        <option value="<?php echo $row_rsProduct['Size']?>"><?php echo $row_rsProduct['Size']?></option>

        <?php

} while ($row_rsProduct = mysql_fetch_assoc($rsProduct));

  $rows = mysql_num_rows($rsProduct);

  if($rows > 0) {

      mysql_data_seek($rsProduct, 0);

            $row_rsProduct = mysql_fetch_assoc($rsProduct);

  }

?>

      </select>

it outputs all sizes from the ProductID and not just from the selected item

i have just tried

      <select name="SelectSize" id="SelectSize">

        <option value="Select Size">Select Size</option>

        <?php

do { 

?>

        <option value="<?php echo $row_rsProduct['Size']?>"><?php echo $row_rsProduct['Size']?><?php echo $row_rsProduct['StockID']; ?></option>

        <?php

} while ($row_rsProduct = mysql_fetch_assoc($rsProduct));

  $rows = mysql_num_rows($rsProduct);

  if($rows > 0) {

      mysql_data_seek($rsProduct, 0);

            $row_rsProduct = mysql_fetch_assoc($rsProduct);

  }

?>

      </select>

and adding <?php echo $row_rsProduct['StockID']; ?>

i can see the correct StockID is showing in the size menu but i just need to get that to send to cart

this is the code that sends the details to the cart

$XC_editAction1 = $_SERVER["PHP_SELF"];

if (isset($_SERVER["QUERY_STRING"])) $XC_editAction1 = $XC_editAction1 . "?" . $_SERVER["QUERY_STRING"];

if (isset($_POST["XC_addToCart"]) && $_POST["XC_addToCart"] == "form1") {

  $NewRS=mysql_query($query_rsProduct, $beau) or die(mysql_error());

  $XC_rsName="rsProduct"; // identification

  $XC_uniqueCol="ProductID";

  $XC_redirectTo = "";

  $XC_redirectPage = "../cart.php";

  $XC_BindingTypes=array("RS","RS","FORM","FORM","RS","RS","RS","NONE");

  $XC_BindingValues=array("StockID","ProductID","SelectSize","Quantity","Product","Price","Stock","");

  $XC_BindingLimits=array("","","","","","","","");

  $XC_BindingSources=array("","","","","","","","");

  $XC_BindingOpers=array("","","","","","","","");

  require_once('XCInc/AddToXCartViaForm.inc');

}

This topic has been closed for replies.

1 reply

Participating Frequently
June 20, 2013

>i think beauSS13_products.ProductID = beauSS13_Stock.ID shouldnt be there ( but am not sure what it should have )

Why do you think that? You are joining 4 tables so you need 3 join conditions. If that's the correct relationship ( and only you will know that) then the SQL is correct.

The problem is most likely how you are assigning values to the cart. When the user selects a color, how is the associated stock id bound to the array?

Inspiring
June 21, 2013

>>Why do you think that?

because it was looking for the specific record, but having added <?php echo $row_rsProduct['StockID']; ?> to the select list it needs to be there

>>You are joining 4 tables so you need 3 join conditions. If that's the correct relationship ( and only you will know that) then the SQL is correct.

yes that is the way the joins are meant to be

>>The problem is most likely how you are assigning values to the cart. When the user selects a color, how is the associated stock id bound to the array?

its sizes they select but same thing, in the above code that the thing i need to pass that value to the cart. the way i have it at the moment is in the code above

i have icluded the full code below to show you how its working

// *** X Shopping Cart ***

$useSessions = false;

$XCName = "beauloves";

$XCTimeout = 1;

$XC_ColNames=array("StockID","ProductID","Size","Quantity","Name","Price","Stock","Total");

$XC_ComputedCols=array("","","","","","","","Price");

require_once('XCInc/XCart.inc');

$var1_rsProduct = "-1";

if (isset($_GET['ProductID'])) {

  $var1_rsProduct = $_GET['ProductID'];

}

mysql_select_db($database_beau, $beau);

$query_rsProduct = sprintf("SELECT * FROM beauSS13_Cat, beauSS13_products, beauSS13_Stock, beauSS13_SizeList WHERE beauSS13_products.CatID = beauSS13_Cat.catID AND beauSS13_products.ProductID = beauSS13_Stock.ID AND beauSS13_Stock.SizeID = beauSS13_SizeList.SizeID AND beauSS13_products.ProductID = %s AND beauSS13_Stock.Stock != 0 ", GetSQLValueString($var1_rsProduct, "int"));

$rsProduct = mysql_query($query_rsProduct, $beau) or die(mysql_error());

$row_rsProduct = mysql_fetch_assoc($rsProduct);

$totalRows_rsProduct = mysql_num_rows($rsProduct);

mysql_select_db($database_beau, $beau);

$query_rsCategory = sprintf("SELECT * FROM beauSS13_products WHERE

beauSS13_products.CatID = (SELECT CatID from beauSS13_products WHERE beauSS13_products.ProductID = %s)", GetSQLValueString($var1_rsProduct, "int"));

$rsCategory = mysql_query($query_rsCategory, $beau) or die(mysql_error());

$row_rsCategory = mysql_fetch_assoc($rsCategory);

$totalRows_rsCategory = mysql_num_rows($rsCategory);

//  *** Add item to Shopping Cart via form ***

$XC_editAction1 = $_SERVER["PHP_SELF"];

if (isset($_SERVER["QUERY_STRING"])) $XC_editAction1 = $XC_editAction1 . "?" . $_SERVER["QUERY_STRING"];

if (isset($_POST["XC_addToCart"]) && $_POST["XC_addToCart"] == "form1") {

  $NewRS=mysql_query($query_rsProduct, $beau) or die(mysql_error());

  $XC_rsName="rsProduct"; // identification

  $XC_uniqueCol="ProductID";

  $XC_redirectTo = "";

  $XC_redirectPage = "../cart.php";

  $XC_BindingTypes=array("RS","RS","FORM","FORM","RS","RS","RS","NONE");

  $XC_BindingValues=array("StockID","ProductID","SelectSize","Quantity","Product","Price","Stock","");

  $XC_BindingLimits=array("","","","","","","","");

  $XC_BindingSources=array("","","","","","","","");

  $XC_BindingOpers=array("","","","","","","","");

  require_once('XCInc/AddToXCartViaForm.inc');

}

function DoFormatCurrency($num,$dec,$sdec,$sgrp,$sym,$cnt) {

  setlocale(LC_MONETARY, $cnt);

  if ($sdec == "C") {

    $locale_info = localeconv();

    $sdec = $locale_info["mon_decimal_point"];

    $sgrp = $sgrp!="" ? $locale_info["mon_thousands_sep"] : "";

    $sym = $cnt!="" ? $locale_info["currency_symbol"] : $sym;

  }

  $thenum = $sym.number_format($num,$dec,$sdec,$sgrp);

  return $thenum;

}

then below is the form that sends the information to the cart

<form id="form1" name="form1" method="post" action="<?php echo $XC_editAction1; ?>">

<select name="SelectSize" id="SelectSize">

        <option value="Select Size">Select Size</option>

        <?php

do { 

?>

        <option value="<?php echo $row_rsProduct['Size']?>"><?php echo $row_rsProduct['Size']?></option>

        <?php

} while ($row_rsProduct = mysql_fetch_assoc($rsProduct));

  $rows = mysql_num_rows($rsProduct);

  if($rows > 0) {

      mysql_data_seek($rsProduct, 0);

            $row_rsProduct = mysql_fetch_assoc($rsProduct);

  }

?>

      </select>

      <select name="Quantity">

        <option value="Select Quantity">Select Quantity</option>

        <option value="1">1</option>

        <option value="2">2</option>

        <option value="3">3</option>

        <option value="4">4</option>

        <option value="5">5</option>

      </select>

<input type="image" src="../images/SS13AddToCart.jpg" border="0" name="submit"/>

<input type="hidden" name="XC_recordId" value="<?php echo $row_rsProduct['ProductID']; ?>" />

      <input type="hidden" name="XC_addToCart" value="form1" />

      </form>

Inspiring
June 27, 2013

hello again, does anyone know a solution to my problem please or any suggestions?

thanks in advance