mysql - PHP - Reducing an independent record stock value by 1 -
i'm doing school project , i'm using dreamweaver along backend database using phpmyadmin.
now, need is, when click button, reduce stock column value in "products" table 1.
however there different products in table. shown below:
http://i.stack.imgur.com/vlzxq.png
so lets say, user on game page "destiny" , clicks on buy button, how can make reduce stock level one, only destiny record , not fifa 15 column. destiny stock becomes 49, fifa stays 50. need make each button have different script or?
currently, made button in page, links action script, im not sure sort of code using.
thank you
xneyte giving advice, comes across me - xrin - new programming database contents php or similar?
so step steps:
mysql databases should connected 1 of 2 types of connection - pdo , mysqli_ . mysql databases work using native mysql xneyte mentioned - deprecated , highly discouraged .
so have pass information php page, list of games on index.php , working page update number of games ordered update.php, in example.
the index.php file passes via anchor link , $_get
values (although highly recommend using php form , $_post
better alternative), update.php page, needs following things (in order) work:
update.php
load valid database login connection page can communicate database
take values passed original page , check valid.
establish connection database , adjust values required.
establish update above worked , give user feedback
so, step step we'll go through these parts: going pain , use mysqli rather pdo - xneyte used pdo syntax in answer , correct , various better mysqli, sake of clarity , knowledge of mysql native, may easier see/understand what's going on mysqli.
part 1:
connection database. should done object orientated - classes,
class database { private $dbuser = ""; private $dbpass = ""; //populate these values private $dbname = ""; public $dblink; public function __construct() { $this->dblink = new mysqli("localhost", $this->dbuser, $this->dbpass, $this->dbname); } if (mysqli_connect_errno()) { exit('connect failed: '. mysqli_connect_error()); } if ( ! $this->dblink ) { die("connection error (" . mysqli_connect_errno() . ") " . mysqli_connect_error()); mysqli_close($this->dblink); } else { $this->dblink->set_charset("utf-8"); } return true; } //end __construct } //end class
the whole of above code block should in database.php
referenced xneyte - class call interact database.
so using above code in database.php
object, need call database object @ top of code, , need generate instance of class:
include "database.php"; ////include file $database = new database(); ///create new instance of class.
now when write $database->dblink
connection database. if not know database connection use details phpmyadmin uses, carries out tasks in same way.
sooo
part 2:
that database connection established - need run update: first off need check value given valid:
if (is_numeric($_get['id']) && $_get['id'] >0 ){ $id = (int)$_get['id']; }
this simple code check value passed link integer number. never trust user input.
it idea never directly plug in get
, post
values sql statements. hence i've copied value across $id
part 3:
$sql = "update <table> set stock = stock-1 product_id = ? limit 1";
the table name table name, limit 1
ensures works on 1 row, not effect many stocked games.
that above sql how make work in php:
first off, statement needs prepared, once prepared, value(s) plugged ?
parts (this mysqli syntax, pdo has more useful :name
syntax).
so:
include "database.php"; ////include file $database = new database(); ///create new instance of class. if (is_numeric($_get['id']) && $_get['id'] >0 ){ $id = (int)$_get['id']; $sql = "update <table> set stock = stock-1 product_id = ? limit 1"; $update = $database->dblink->prepare($sql); $update->bind_param("i",$id); $update->execute(); $counter = $update->affected_rows; $update->close(); //////gap later work, see below: } else { print "sorry nothing update"; }
there's quite lot going on here, first off bind_param
method sets values plug sql query, replacing ?
value of $id
. i
indicates meant integer value. please see http://php.net/manual/en/mysqli-stmt.bind-param.php
the $counter
value gets return of number of affected rows , can inserted:
if ($counter > 0 ){ print "thank order. stock has been reduced accordingly."; } else { print "sorry not stock order."; }
part 4
and if wish can output print messages or tend put messages session, , redirect php page back.
i hope has helped bit. highly recommend if you're not used database interactions in way either use pdo or mysqli not combine two, cause sorts of syntax faults. using mysqli means know mysql can do, done better addition of letter "i" in function call. referencing php.net manual has excellent clear detailed examples of how use each php function.
Comments
Post a Comment