Update mysql using PHP

Hi everyone, I’m new to PHP and having a problem with updating a database using PHP. Essentially this is a reservation script for a shop so people can reserve items and the amount reserved gets added to the reserve column of the database for that item.

So to clarify, if there are 5 products and I only want to reserve 3 bags of crisps, it should only update the row for crisps and column for reserve by adding the reserve I enter in an input box on to the reserve amount that exists.

Here is my code for viewing how many is reserved at the moment and to update it. It is correctly showing how many is reserved but failing to update the mysql database.

[php]

<?php // Connects to your Database mysql_connect("localhost", "database", "password") or die(mysql_error()) ; mysql_select_db("table") or die(mysql_error()) ; //Retrieves data from MySQL $data = mysql_query("SELECT * FROM products") or die(mysql_error()); while($info = mysql_fetch_array( $data )) { $sum_total = $info['stock'] - $info['reserve'] ; echo "In Stock",($sum_total),"

"; echo ""; echo "Reserve How Much? <input type="; echo ""; $id=$_GET['id']; $data = mysql_query("UPDATE table SET reserve='$reserve + reserve' WHERE (id = $id)") ; } ?>

[/php]

Here is the submit php

[php]

<?php // Connects to your Database mysql_connect("localhost", "database", "password") or die(mysql_error()) ; mysql_select_db("table") or die(mysql_error()) ; print $_POST["id"]; // the name of the HTML element is the key print $_POST["reserve"]; // again, note that we use the name as the key $id = $_POST["id"]; $reserve = $_POST["reserve"]; $sql = "UPDATE products SET reserve = '$reserve + reserve' WHERE id = $id"; ?>

[/php]

Help is appreciated, thank you.

you got this back-to-front:
[php]$sql = “UPDATE products SET reserve = ‘$reserve + reserve’ WHERE id = $id”;[/php]

it should be:
[php]$sql = “UPDATE products SET reserve = reserve+” . $reserve . " WHERE id = $id";[/php]

notice i lost the quotes too as i assume your storing a number (integer)?

Hope that helps,
Red :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service