Bringing values from MySql database into shopping cart form

I’m trying to figure out how to convert my current shopping cart (hosted on Mal’s E-comm) into one which draws its “products” from a database.

This is the current html coding for one item (a dog show trophy donation):

    <p>
      <input type="checkbox" name="qty2" value="1"> Best Junior Showman Rosette - $30
	  <input type="hidden" name="product2[]" value="Best Junior Showman Rosette" />
	  <input type="hidden" name="price2" value="30" />
			  Dedication:
	  <input size="40" name="product2[]">
	  <input type="hidden" name="units2" value="1">
	  <input type="hidden" name="noqty2" value="1">
    </p>

It looks like this on the page in a browser:

I found some coding on w3schools which I’ve copied and tweaked but I’m obviously doing something wrong. This is what I’ve come up with:

<?php
$servername = "";
$username = "*****";
$password = "*****";
$dbname = "*****";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT name, price, dedication FROM juniortrophies";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<p>
		<input type='checkbox' name='qty7' value='1'>". $row["name"]. " - $". $row["price"]. 
			"<input type='hidden' name='product7[]' value='". $row["name"]. "'/>
			<input type='hidden' name='price7' value='". $row["price"]. "'/>
			- Dedication: ". $row["dedication"]. 
			"<input size='40' name='product7[]'>
			<input type='hidden' name='units7' value='1'>
			<input type='hidden' name='noqty7' value='1'>
        </p>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>	

which results in the cart looking like this:

instead of like this:

It is displaying ALL the records in the cart instead of just the one required. Note that it also has the price wrong - it should be $20, not $40.

I’m guessing it’s misbehaving because one shouldn’t include the same value twice in one echo command but I need to have those values in there twice in order for the cart to work.

Does anyone have any idea about how to do this? I’ve read all the support articles on Mal’s website but unfortunately his user forum is down, so I can’t ask people who regularly use his carts. I’ve been using them for over 11 years and love them but now that I need to get a bit “fancier” I’m having this problem.

The reason I’m trying to do this (before someone asks :slight_smile: ) is because currently when someone pays to donate for a particular trophy I have to manually open that html page and remove (or hide) that trophy so that nobody else can donate for it. The problem is, not everyone is in my time zone, so I’ve had 3 people donate for the exact same trophy while it was the middle of the night here.

I’d like to use Mal’s Remote Call feature to turn a Show_Hide field from ‘Y’ to ‘N’ so that the trophy with automatically disappear but in order to do that I’m pretty sure I need to bring in the trophy information from the DB.

Is this a pipedream on my part or can it really be accomplished?

the code is totally unrelated to what you’ve shown in the picture - the picture doesn’t show any checkbox and the code doesn’t show anything name related.

No, there’s nothing wrong with doing that usually.

You’re inconsistent with your input tags - sometimes you close them with a trailing /, and sometimes you don’t. I don’t know what html standard you’re working with, but you should be consistent.

I think the issue is that some of the field names are the same for every iteration of the loop. Sometimes you use an array, but equally I wonder whether the ‘7’ on the end of the name is significant.

It would be helpful to see the code that your loop produces, and the code you use to take the input from that loop and place the item(s) in the shopping cart. Without that, we’re guessing as to the cause.

The way I’d do that, then, is once the donation is processed (i.e. the shopping cart is finished) I’d have a column in juniortrophies to show that that particular trophy has been sold, a Boolean column or Y/N or whatever suits. Have the processing code set that to whatever value you need to indicate that the trophy is sold, and alter your initial query to only display those that have not been sold.

@chorn, I think the issue is that there is a step we’re not seeing here - that code does not produce that display as you said, there’s no checkbox in there, but the OP is saying that the code products a screen which once processed in the next step produces that information in the cart.

Actually, it does. :slight_smile: The checkbox is generated by this code:

<input type="checkbox" name="qty1" value="1">

Thanks for all your info droopsnoot - I have to head out to work soon but will try your suggestions afterwards. In the meantime, this is what page looks like (and does, even with my attempt to ad the DB fields):

The user clicks on one (or more) of the little checkboxes, fills in their dedication and then proceeds to the cart. The cart should then show only the checked trophy name plus dedication and price, not the whole list of names as in the screenshot in my previous post. :slight_smile:

I will definitely double-check those /> vs > tag endings. Some of this code is from YEARS ago and those /> came from there. Since it (so far) hasn’t affected the performance of the pages, I haven’t worried about them.

I’m dealing with 9 tables, by the way - the Juniors one is just the smallest # of trophies, so I’m experimenting with that. Some areas (ie regular classes) have close to 50 items/checkboxes. :slight_smile:

It probably won’t make any difference (depending on your DOCTYPE declaration) but it’s inconsistent. I did notice that it was the same in the original code.

We really need to see the code for that page, not what it looks like visually. Right-click the browser and “view source”, and post that code. I suspect (from your earlier loop code) that all of your checkboxes are called qty7, and that will make it difficult for the code to decide which one you’ve ticked.

Aha!! The penny has dropped. You’re absolutely correct and I couldn’t see the forest for the trees. I need to put all the cart values into the database as well!! Duh. Of COURSE they’re going to return the same item each time. Thanks so much droopsnoot - I’m just on a break and have to go back to work but will add the quantity # to the DB once I’m off work again.

I’m thinking that I just need to add the 7 (ie the item count) part of the coding to the DB and then in the html it will be "<P><input type='checkbox' name='qty". $row['count']. " value='1'>". $row["name"]. etc., substituting the $row[‘count’] bit for each of the 6 numbers in each item.

Thanks so much for bumping my brain over onto a different path!

I’ve figured it out! Your tip about all the records using the same number for all the fields did the trick. I changed all of those in the 8 records, then set the Show_Hide field in the first 4 (which have already been donated for) to ‘N’, added WHERE Show_Hide='Y' to the SELECT line and bingo! When I load the page, only the four unpaid trophies are displayed.

Thank you SO much droopsnoot - I can’t thank you enough!! Now to add all the other (probably 100s of) trophies. That should keep me quiet for quite a while. :slight_smile:

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.