Page 1 of 1

problem with syntax

Posted: Mon Jun 25, 2007 9:27 am
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]

Posted: Mon Jun 25, 2007 9:36 am
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());

Posted: Mon Jun 25, 2007 9:42 am
by Beebosoft
Many thanks that worked. any idea why mine didn't

Angie

Posted: Mon Jun 25, 2007 9:58 am
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)

Posted: Mon Jun 25, 2007 10:51 am
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?

Posted: Mon Jun 25, 2007 6:48 pm
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.

Posted: Tue Jun 26, 2007 3:07 am
by Beebosoft
Thank you all for your help