Jump to content

Duplicate entry in SQL Database


ultraspoon

Recommended Posts

Hi everyone, I need a little bit of help finishing off my code. Ive managed to get this far.

 

<?php
mysql_connect("") or die ("Not Connected to MYSQL");
echo "</br>";
mysql_select_db("") or die ("Not Connected to DB");
// Database Connection stuff

$partialNumber = $_POST['partialNumber']; // Post the Partial number
$partialNumber = strtoupper($partialNumber);
$numberSearch = mysql_query("SELECT * FROM product_option_value_description WHERE name LIKE '%$partialNumber%'") or die (mysql_error()); // Query to select the key number

//Query to get product ID //

$productId = "SELECT product_id FROM product_option_value_description";

//Query to get product ID //

while ($keyNumber = mysql_fetch_array($numberSearch)) {
$id = $keyNumber['product_id'];
// Query for the images // 
$query = "SELECT image FROM product WHERE product_id = '$id'";
$result = mysql_query($query);
$row = mysql_fetch_array($result) or die(mysql_error());

$query2 = "SELECT product_option_id FROM product_option_value WHERE product_id = '$id'";
$result2 = mysql_query($query2);
$row2 = mysql_fetch_array($result2) or die(mysql_error());

$query3 = "SELECT product_option_value_id FROM product_option_value WHERE product_id = '$id'";
$result3 = mysql_query($query3);
$row3 = mysql_fetch_array($result3) or die(mysql_error());
?>
    <div>
    
    <br /><br />
    Key Number: <? echo $keyNumber['name']; ?></a>
    
    <form action="http://www.co.uk/teststore/index.php?route=checkout/cart" method="post" enctype="multipart/form-data" id="product">
<br />
<table style="width: 100%;">
<tr>
<td>
Colour:
<select name="option[<? echo $row2['product_option_id']; ?>]">
<option value="<? echo $row3['product_option_value_id']; ?>"></option>
</select></td>
</tr>
</table>
<div class="content"> Qty: <input type="text" name="quantity" size="3" value="1" />
    <input type="hidden" name="product_id" value="<? echo $id; ?>" />
    <input name="submit" type="submit" value="Add to Cart" />

</div>
</form>
<? echo $row3['product_option_value_id']; ?>
</div>
<br />
<img height="150" width="150" src='http://www.co.uk/teststore/image/<? echo $row['0']; ?>'/>
<? } 
?>

 

And here is my SQL Table code.

 

product_option_value_id		product_option_id	product_id	
599				               302		               49		
598				               302		               49		
589				               297		               42		
588				               297		               42

 

So as you can probably tell, it is a search program that looks for products on a shopping cart. The products will have different option values, and the php script will grab the option values and echo them in a form to post back to the cart to add the product to the basket.

 

The problem is that the "product_option_value_id" can have lots of different values, but my code echos only the first one it finds. So when I click the add to cart button, it will only add the first option value for the product it finds.

 

For some reason I am having a hard time explaining this, so I hope someone can help me.

 

Thanks for looking.

Link to comment
Share on other sites

Thanks Muddy_Funster,

 

It has been a long while since I was last coding, im surprised I have managed to throw that together. From what I remember there are a few different kind of loops. Could you point me in the direction of which kind and then I can research into it.

 

Thank you

Link to comment
Share on other sites

now that I actualy have a close look at the code I see you have one loop in there already, a while loop.  Unfortunately, you have chosen to perform 3 additional queries within this loop - very bad idea!  This will hammer you db server as the table grows. fist up we need to condense all those queries into one:

$sql = <<<SQL_BLOCK
SELECT product_option_value_description.product_id, product_option_value_description.name, product.image, product_option_value.product_option_id, product_option_value.product_option_value_id 
FROM product_option_value_description LEFT JOIN product ON (product_option_value_description.product_id = product.product_id)
LEFT JOIN product_option_value ON (product_option_value_description.product_id = product_order_value.product_id)
WHERE product_option_value_description.name LIKE '%$partialNumber%'
SQL_BLOCK;

should do the trick.

 

next you want to move your output inside the while loop that you have for the data retreval:

$result= mysql_query($sql) or die(mysql_error());
$display='';
while ($row = mysql_fetch_array($result){
$display .= <<<HTML_BLOCK
    <div>
    
    <br /><br />
    Key Number: {$row['name']}</a>
    
    <form action="http://www.co.uk/teststore/index.php?route=checkout/cart" method="post" enctype="multipart/form-data" id="product">
<br />
<table style="width: 100%;">
<tr>
<td>
Colour:
<select name="option {$row['product_option_id']}">
<option value="{$row['product_option_value_id']}"></option>
</select></td>
</tr>
</table>
<div class="content"> Qty: <input type="text" name="quantity" size="3" value="1" />
    <input type="hidden" name="product_id" value="$row['product_id'];" />
    <input name="submit" type="submit" value="Add to Cart" />

</div>
</form>
{$row['product_option_value_id']}
</div>
<br />
<img height="150" width="150" src='http://www.co.uk/teststore/image/{$row['0']}>
HTML_BLOCK;
echo $display

 

that should at least be close enough for you to get the idea.  Another thing, you'll need to get out the habbit of using short open tags (<? ) - they are depreciated.

Link to comment
Share on other sites

Thanks for all your help, Muddy_Funster and dmikester1,

 

Ive manged to sort out all the small issues, so the code is now fully working!

 

There is only one small issue now, when you do a search, it shows two of each of the same products, instead of just one. In my test store, I have 2 products each with 2 option values, so it should show 4 products, not 8.

This is the last thing now I promise! Thank you

 

$partialNumber = $_POST['partialNumber']; // Post the Partial number
$partialNumber = strtoupper($partialNumber);
$sql = <<<SQL_BLOCK
SELECT product_option_value_description.product_id, product_option_value_description.name, product.image, product_option_value.product_option_id, product_option_value.product_option_value_id 
FROM product_option_value_description LEFT JOIN product ON (product_option_value_description.product_id = product.product_id)
LEFT JOIN product_option_value ON (product_option_value_description.product_id = product_option_value.product_id)
WHERE product_option_value_description.name LIKE '%$partialNumber%'
SQL_BLOCK;

$result= mysql_query($sql) or die(mysql_error());
$display = '';
while ($row = mysql_fetch_array($result)){
$display .= <<<HTML_BLOCK

<div>
<br /><br />
Key Number: {$row['name']}</a>
<form action="http://www./teststore/index.php?route=checkout/cart" method="post" enctype="multipart/form-data" id="product">
<br />
<table style="width: 100%;">
<tr>
<td>
<select name="option[{$row['product_option_id']}]">
<option value="{$row['product_option_value_id']}"></option>
</select></td>
</tr>
</table>
<div class="content"> 
Qty: <input type="text" name="quantity" size="3" value="1" />
<input type="hidden" name="product_id" value="{$row['product_id']}" />
<input name="submit" type="submit" value="Add to Cart" />
</div>
</form>
</div>
<br />
<img height="150" width="150" src='http://www..co.uk/teststore/image/{$row['image']}'>
HTML_BLOCK;
}
echo $display;
?>

Link to comment
Share on other sites

try this:

$sql = <<<SQL_BLOCK
SELECT product_option_value_description.product_id, product_option_value_description.name, product.image, product_option_value.product_option_id, product_option_value.product_option_value_id 
FROM product_option_value_description LEFT JOIN product ON (product_option_value_description.product_id = product.product_id)
LEFT JOIN product_option_value ON (product_option_value_description.product_id = product_option_value.product_id)
WHERE product_option_value_description.name LIKE '%$partialNumber%' GROUP BY product_option_value.product_option_id
SQL_BLOCK;

Link to comment
Share on other sites

Fixed it, i put this.

 

$sql = <<<SQL_BLOCK
SELECT product_option_value_description.product_id, product_option_value_description.name, product.image, product_option_value.product_option_id, product_option_value.product_option_value_id 
FROM product_option_value_description LEFT JOIN product ON (product_option_value_description.product_id = product.product_id)
LEFT JOIN product_option_value ON (product_option_value_description.product_id = product_option_value.product_id)
WHERE product_option_value_description.name LIKE '%$partialNumber%' GROUP BY product_option_value_description.name
SQL_BLOCK;


Thanks dude.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.