Connecting Tech Pros Worldwide Help | Site Map

strange variables

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 04:56 AM
none
Guest
 
Posts: n/a
Default strange variables

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

  #2  
Old July 17th, 2005, 04:56 AM
MJaC
Guest
 
Posts: n/a
Default 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");
  #3  
Old July 17th, 2005, 04:56 AM
Nikolai Chuvakhin
Guest
 
Posts: n/a
Default 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
  #4  
Old July 17th, 2005, 04:57 AM
none
Guest
 
Posts: n/a
Default 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)
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.