Connecting Tech Pros Worldwide Help | Site Map

strange variables

none
Guest
 
Posts: n/a
#1: Jul 17 '05
Hi, when i use the following code;

$current_available = mysql_query("SELECT availableplaces FROM
tblCourses WHERE course_id='$_POST[selectedcourse]'");
$current_available -= 1;
mysql_query("UPDATE tblCourses SET
availableplaces='$current_available' WHERE
course_id='$_POST[selectedcourse]'");


The variable $current_available is initially 32, after running the
above it is changed to 2.

I'm really lost and would appreciate some direction.

Many thanks
MJaC
Guest
 
Posts: n/a
#2: Jul 17 '05

re: strange variables


none wrote:[color=blue]
> Hi, when i use the following code;
>
> $current_available = mysql_query("SELECT availableplaces FROM
> tblCourses WHERE course_id='$_POST[selectedcourse]'");
> $current_available -= 1;
> mysql_query("UPDATE tblCourses SET
> availableplaces='$current_available' WHERE
> course_id='$_POST[selectedcourse]'");[/color]

I think I understand what you want, however you are doing it wrong.
Firstly you have an error, you need to fetch the result then decrement it.

$course_places = mysql_query("SELECT `availableplaces` - 1 FROM
`tblCourses` WHERE `course_id`='$_POST[selectedcourse]' LIMIT 1");
$amount_available = mysql_result($current_available, 0, 'availableplaces');
mysql_query("UPDATE `tblCourses` SET
`availableplaces`='$amount_available' WHERE
`course_id`='$_POST[selectedcourse]' LIMIT 1");

That should work, however there is a much faster, and easier way of
doing it (UNLESS YOU WANT THE AMOUNT AVAILABLE)!

mysql_query("UPDATE `tblCourses` SET
`availableplaces`=`availableplaces`-1 WHERE
`course_id`='$_POST[selectedcourse]' LIMIT 1");
Nikolai Chuvakhin
Guest
 
Posts: n/a
#3: Jul 17 '05

re: strange variables


kinskai@aol.com (none) wrote in message
news:<863d38a1.0404220935.34598ce8@posting.google. com>...[color=blue]
>
> when i use the following code;
>
> $current_available = mysql_query("SELECT availableplaces FROM
> tblCourses WHERE course_id='$_POST[selectedcourse]'");
> $current_available -= 1;
>
> The variable $current_available is initially 32, after running the
> above it is changed to 2.
>
> I'm really lost and would appreciate some direction.[/color]

OK, here is your code again, slightly reformatted:

$current_available = mysql_query("SELECT availableplaces
FROM tblCourses WHERE course_id='$_POST[selectedcourse]'");

This makes $current_available a resource.

$current_available -= 1;

Now all of a sudden you decide to treat $current_available as if
it were an integer. Needless to say, the results are not what
you expect them to be...

Cheers,
NC
none
Guest
 
Posts: n/a
#4: Jul 17 '05

re: strange variables


nc@iname.com (Nikolai Chuvakhin) wrote in message news:<32d7a63c.0404221505.f72a83d@posting.google.c om>...[color=blue]
> kinskai@aol.com (none) wrote in message
> news:<863d38a1.0404220935.34598ce8@posting.google. com>...[color=green]
> >
> > when i use the following code;
> >
> > $current_available = mysql_query("SELECT availableplaces FROM
> > tblCourses WHERE course_id='$_POST[selectedcourse]'");
> > $current_available -= 1;
> >
> > The variable $current_available is initially 32, after running the
> > above it is changed to 2.
> >
> > I'm really lost and would appreciate some direction.[/color]
>
> OK, here is your code again, slightly reformatted:
>
> $current_available = mysql_query("SELECT availableplaces
> FROM tblCourses WHERE course_id='$_POST[selectedcourse]'");
>
> This makes $current_available a resource.
>
> $current_available -= 1;
>
> Now all of a sudden you decide to treat $current_available as if
> it were an integer. Needless to say, the results are not what
> you expect them to be...
>
> Cheers,
> NC[/color]


Thanks for the input,

I'm quite new to php but i suspected it may be something I was doing
with the variables.

The query;

mysql_query("UPDATE `tblCourses` SET
`availableplaces`=`availableplaces`-1 WHERE
`course_id`='$_POST[selectedcourse]' LIMIT 1");

worked a treat, but the first one (long way) threw an error as not
been valid syntax.

Cheers anyway :o)
Closed Thread