[SOLVED] Php MySQL, Custom PC shoppingcart Database problem?

I’m working on making a shopping cart using Php and MySQL. Building a Custom PC shopping cart…
I’m getting the pages put together and putting info into the database AND KNOW I have been hit with a major problem. (I provided an image below so you can see the structure)…

When I start pulling components into the pages, I will get every component for every product…

What I need is… Some components for certain PC products. So some PC products may have the same components as other products, but it may only have a few.

I’m stuck with a many to many relationship here. I cant figure other how to fix it, or maybe put something in between them to correct it. (I could assign each component to it’s own PC product, but if that component is for ALL of the PC products, I will have to insert that SAME data 5 to 10 times AND then it would be a one to one relationship).

This is so frustrating. Can anyone help me figure this out. Or please give me some pointers.

if i uder sood u right u don’t know how to get a n:m relation from produkts to components? or at leads thets ur main conserne.

as i alrady poited out u just need a table in between.

lets call this table product_component (cause it will only be there to make the relation)

i allways use a id (PRIMARY, AUTO INCRENENT) its good to have that to reference them later, but is not realy needed here. so u may leaf that away.

the new table:
tablename: product_component
product_id: INT
component_id: INT

now to add a component to a PC u just need to add the PC’s id and the Component’s id in that table.

to get ur SELECT’s working again u need to rewrite them.
an example. u know the pord id (e.g. 42) and wanna select all components that are Motherboards:
SELECT * FROM product_components, components WHERE product_id=42 AND components_id = components.id AND type=“Motherboard”

hope this helps

P.S.: i’m not sure whats ur prob with the Types and Technologys. Maybe u could specify that again.

Hey thanks for the reply…

I JUST THOUGHT AND DID EVERYTHING YOU POSTED like 10 minutes before you posted it!!! Crazy! It’s like I was reading your mind as your were typing it! …

As you posted this I ran into another problem: I use dreamweaver and recordsets most of the time.

How can I make it so I can update or create more then one row at a time?
what I mean is… I have 4 check boxes that come from a while loop for my product… When I submit them, it only creates 1 row into the database (whatever the last checkout that was selected.)

If I made no since in this reply, just tell me and I will gladly retype it.

i guess u used the same name for all checkboxes.?
if not please rephrase ur question.

the prom is that u need 3 infos per checkbox: a name, an id, and the information whether it was checked or not.

to solve this u may use arrays.

now there are 2 ways:
use an array witch caches all values of the selected checkboxes as value:

<input name="field[]" value="17" /> Item with the id 17 <input name="field[]" value="42" /> Item with the id 42

this will create an array like:
[php]$_POST[‘field’]=array(17,42) /* only the checked values */[/php]

use an array witch caches all values of the selected checkboxes as key:

<input name="field[17]" value="whatever" /> Item with the id 17 <input name="field[42]" value="whatever" /> Item with the id 42

this will create an array like:
[php]$_POST[‘field’]=array(17 => “whatever”, 42 => “whatever”) /* only the checked values */[/php]

the first one should be enough for now, but as ur script gets more complicated, or if using radiobuttons the second one may be needed in the future.

hope this helps, otherwise plz ask again

P.S.: stop reading my mind :slight_smile:

Ok ok I wont read your mind any more… In fact I was far from it this time around.

I’m alittle confused on what you had writen. I’m not that good at PHP.

How does all of this work with inserting the data into the database using the array stuff that you talked about.

I understand what an array is and I have just one before, but I never justed on in a real word project.

You see I am creating all of my check boxes with a while loop, pulling all of the data from the products table.

[] Product name 1
[] Product name 2
[] Product name 3
[] Product name 4

The value of every check box (when checked) is the Products_ID

[HIDDEN FIELD = Component ID = 1]

When I check all of the check boxes and click submit. The only thing I gets put into the database is the closes check box to the submit button.

In the database it looks like this:
Product_ID: 4 Component_ID: 1

This is okay but if I select all of the check boxes I need it to put the data of all products into the database in new rows like this:
Product_ID: 1 Component_ID: 1
Product_ID: 2 Component_ID: 1
Product_ID: 3 Component_ID: 1
Product_ID: 4 Component_ID: 1

and dreamweaver just is not doing it right…

Does that make any since … Sometimes I get to typing and mess what I’m saying up…

Talk soon.

Thanks Brian

i think i always understand u. u may have realised that what i mean and what i type isn’t always the same in my posts as well.

could u post the relavant code?

the loop creating the checkboxes and the loop u use to do the sql-update?

sounds like u r doing everything right, but i need to see the code to find the (probably small) mistake.

btw.: where r u from?

I’m from the US in Maryland. Where are you at?

I have this already:

<p>Match this Component with 1 or more Products.</p>
// 3. Perform database query
mysql_select_db($database_DM_database, $DM_database);
$db_result1 = mysql_query("SELECT tblproducts.Product_ID, tblproducts.Product_Name FROM tblproducts", $DM_database);
if (!$db_result1) {
	die("Database query failed: " . mysql_error());
} ?>

  <form name="form1" method="post" action="">
      <?php // 4. Use database retured data

while ($row = mysql_fetch_array($db_result1)) {
	echo "<input type="checkbox" name="" . $row["Product_ID"] . "" value="checkbox" />" . " " . $row["Product_Name"] . " <br />" ;
      <?php if (isset($_GET['Component_ID'])) {
  $Component_ID = (get_magic_quotes_gpc()) ? $_GET['Component_ID'] : addslashes($_GET['Component_ID']);
} ?>
     <input type="hidden" name="Component_ID" value="<?php if (isset($Component_ID)) { echo $Component_ID; }?>" />
      <input type="submit" value="Submit" />

How do I make it submit into the database more then one field.
For example: Currently there are 4 products. If I select all 4 of the products, I need PHP to insert 4 different times into the database
Looking like this:

Product_ID … Component_ID
1 … 1
2 … 1
3 … 1
4 … 1

What does the php code for that look like, how can I write this php…



This post can be marked as solved.

w?rttemberg, germany


Sponsor our Newsletter | Privacy Policy | Terms of Service