By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,636 Members | 2,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,636 IT Pros & Developers. It's quick & easy.

strange variables

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
none wrote:
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]'");


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");
Jul 17 '05 #2

P: n/a
ki*****@aol.com (none) wrote in message
news:<86**************************@posting.google. com>...

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.


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
Jul 17 '05 #3

P: n/a
nc@iname.com (Nikolai Chuvakhin) wrote in message news:<32*************************@posting.google.c om>...
ki*****@aol.com (none) wrote in message
news:<86**************************@posting.google. com>...

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.


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

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)
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.