Updating quantities in database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
sleepydad
Forum Commoner
Posts: 75
Joined: Thu Feb 21, 2008 2:16 pm

Updating quantities in database

Post by sleepydad »

I always preface these posts asking for patience because I'm a newbie to php, so here goes ...

I have a database that I ultimately want to use to store a wine inventory. I currently have the following fields: 'quantity', 'vintage', 'name', 'variety', 'price', 'redOrWhite'. I have figured out how to search for a match based on HTML input from a form using (abbreviated):

<?php
$quantity=$_POST['quantity'];
$vintage=$_POST['vintage'];
//name, variety, price, redWhite ..
$userInput=$vintage.$name.$variety
$dupe=false;

@ $db=new mysqli('localhost', 'root', mypassword, 'wine');

$query=//need assistance here;

$result=$db->query($query);
$numRows=$result->num_rows;

for($i=0; $i<$numRows; $i++) {
$row=$result->fetch_assoc();
$checkDupe=$row['vintage'].$row['name'].$row['variety'];
if($checkDupe==$userInput) {
$dupe=true;
/// NOW WHAT??
}
}

$db->close();
$result->free();

I'm trying to learn php using OO rather than procedural, so I'd appreciate answer(s) using OO if possible. Don't mean to sound snooty, just that my programmer friends say I'm best off learning this way based on my future endeavors using PHP.

Many thanks in advance to all.
sleepydad
jason.carter
Forum Commoner
Posts: 35
Joined: Sat Jan 10, 2009 10:05 am

Re: Updating quantities in database

Post by jason.carter »

hi sleepydad :)

Just use SQL query

UPDATE table SET quantity = quantity + 1

You are done right? You can replace 1 with whatever you want.
sleepydad
Forum Commoner
Posts: 75
Joined: Thu Feb 21, 2008 2:16 pm

Re: Updating quantities in database

Post by sleepydad »

Thanks Jason.

Would I put my query in the for() loop, then, and nest an if/else statement ie:

<?

>
>
>

for($i blah, blah, blah) {
if($dupe) {
$query="UPDATE table SET quantity = quantity + 1"; // is this syntax correct BTW?
} else {
$query="insert into wine values ('".$quantity."',//the rest)";
}
}

// close
// free

?>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Updating quantities in database

Post by Benjamin »

Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums.  Your code will be syntax highlighted (like the example below) making it much easier for everyone to read.  You will most likely receive more answers too!

Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces.  You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:

[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]

If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online.  You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member.  Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax]
Post Reply