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]
Link to comment
Share on other sites

$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.
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.