problem with syntax

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
Beebosoft
Forum Newbie
Posts: 12
Joined: Mon Jun 25, 2007 9:18 am

problem with syntax

Post by Beebosoft »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


i have a database which is taking bookings for courses.  when i abooking has been made i am trying to get the places available field to decrease by 1.  when it does this it goes from 15 to 0.  please can anyone shed any light on this.  my code etc is below

Code: Select all

$sql = "SELECT * FROM rsu  WHERE CourseID LIKE '$courseid'";
$resultID1 = mysql_query($sql, $linkID)or die(mysql_error());
if(mysql_num_rows($resultID1) < 1) 
{
print "There has been an internal error.  We apologise for this please try again later";
}
else
{ 
$places =$row['placesavailable'];

 $places --;
$result = mysql_query("UPDATE rsu SET placesavailable = '$places' where CourseID = '$courseid'", $linkID)or die(mysql_error()); 

}
I am using mysql and my datatype for the placesavailable field is integer (11)

Many thanks in anticipation


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

<?php
$query = "UPDATE rsu SET placesavailable = placesavailable-1 where CourseID = '$courseid'";
echo '<div>Debug: ', $query, "</div>\n";
$result = mysql_query($query , $linkID)or die(mysql_error());
Beebosoft
Forum Newbie
Posts: 12
Joined: Mon Jun 25, 2007 9:18 am

Post by Beebosoft »

Many thanks that worked. any idea why mine didn't

Angie
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

No idea. But you shouldn't use SELECT + php code + UPDATE anway since it's prone to race conditions (unless you lock the table for the duration of the whole process)
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

Beebosoft wrote:Many thanks that worked. any idea why mine didn't
Where maybe because off $row is unassigned and error_reporting is 0?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

Beebosoft wrote:Many thanks that worked. any idea why mine didn't

Angie
You put single quotes around the value in the SQL statement, so it was treated like alphabetic characters, which have the value zero.
Beebosoft
Forum Newbie
Posts: 12
Joined: Mon Jun 25, 2007 9:18 am

Post by Beebosoft »

Thank you all for your help
Post Reply