Jump to content

Help with php/mysql - If a value exists, do something.


papaface

Recommended Posts

Hello,
I was wondering if someone can help me.
I am trying to make a kind of shopping cart.
A person submits a form of checkboxes with each items quantity equaling "1".
How would I go about checking in the mysql database if the name of that item exists in the order table, and if so increment the current quantity stored in the table by one.
On the other hand if the value is not present, add it with the quantity equalling 1.
Hope you can understand that. I would appreciate any help anyone can give.

Thanks.

edit:
The code I currently have in a script named process.php is:

[code]if (isset($_POST["submit"]))
{
foreach ($_POST as $key => $value)
{
if ($key != "submit")
{
mysql_query("insert into orders (order_item) values ('$value')",$con) or die (mysql_error());
}

}

}[/code]
$query = mysql_query("SELECT * FROM orders WHERE order_item = '$value'");
$check = mysql_num_rows($query);
if(!$check)
{
mysql_query("insert into orders (order_item) values ('$value')",$con);
} else {
            $query = mysql_query("SELECT quantity FROM orders WHERE order_item='$value'");
            $data = mysql_fetch_assoc($query);
            $quantity = $data["quantity"];
            $quanitity = $quantity + 1;
mysql_query("UPDATE orders SET quantity=$quantity WHERE order_item='$value'");
}

That should work.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.