georgehowell Posted June 13, 2010 Share Posted June 13, 2010 Hi there the following table that is populated dynamically from SQL table. <?php try{ $database = "SkyZone"; $username = "root"; $password = ""; // connect to database $db = new PDO("mysql:host=localhost;dbname=$database",$username,$password); $query = "SELECT id, title, on_hand FROM SZ_products order by on_hand asc;"; $table =$db->query($query); echo "<form method='post' name='onHandID' action='updateInventory.php'>"; echo "<table width='550px' class='altrowstable' id='alternatecolor'>"; foreach($table as $row) { echo "<tr id='row'>\n"; echo "<td style='text-align:center'>" . $row['id'] . "</td>"; echo "<td style='padding-left:5px'>" . $row['title'] . "</td>"; echo "<td style='text-align:right'>" . $row['on_hand'] . "</td>"; echo "<td><input name='onHandID' type='submit' value='add'></td>"; echo "</tr>"; } echo "</table></form>"; //destroy the PDO object $db = null; //if connection fails throw a PDO exception }catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die(); } ?> The "add" button is intended to increment the value "on_hand" by one. Here's the PHP files called on submit: "updatedInventory.php" <?php ob_start(); require_once("lib/class_loader.php"); session_start(); ob_flush(); $user = $_SESSION['user']; $user->updateInventory($_GET); include("Admin_4.php"); ?> "Administrator.php" <?php class Administrator extends User { public function __construct() { parent::__construct(); } public function updateInventory($on_hand) { $ps = $this->db->prepare("UPDATE SZ_products SET on_hand= on_hand+1 WHERE onHandID = $on_hand"); $ps->execute(array($on_hand)); return ($ps->rowCount() == 1); } } The "WHERE" /sql part of the query is not working here. Any comments appreciated cheers, George Link to comment https://forums.phpfreaks.com/topic/204644-incrementing-a-field-in-an-sql-table-by-1-using-forms-and-php/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.