You are here: Re: Basic PHP question... « All PHP « IT news, forums, messages
Re: Basic PHP question...

Posted by Joseph Melnick on 09/21/05 16:10

Hello Plato, You wrote:
"plato" <platoTAKETHISOUT@telpacific.com.au> wrote in message
news:43309ee3@news.rivernet.com.au...
>
> "plato" <platoTAKETHISOUT@telpacific.com.au> wrote in message
> news:433082c3@news.rivernet.com.au...
>>
>> "Sander" <swuste zit bij cartel.nl> wrote in message
>> news:43301fef$0$11867$ba620dc5@text.nova.planet.nl...
>> >
>> > "plato" <platoTAKETHISOUT@telpacific.com.au> schreef in bericht
>> > news:43300681@news.rivernet.com.au...
>> > >I think this is more php than sql.
>> > >
>> > > I want to create a form which has a check box which adds entries to a
>> > > database e.g. value= course_1 and also on the form is a number
> denoting
>> > > places available e.g. value = places_1
>> > > <check box> Course 1 (There are 11 places available)
>> > >
>> > > each time a form with a course selected is submitted the next time
>> > > the
>> > > form
>> > > is accessed it reduces the places available by 1 until it reaches 0
> when
>> > > an
>> > > error message is returned.
>> > >
>> > > Can someone give me an idea of the script I can use to practice this?
>> >
>> > What you basically want is a php page that refeshes the free place for
> a
>> > certain course. So lets just assume you have:
>> > coursename places_avail
>> > course_a 3
>> > course_b 15
>> > course_c 9
>> >
>> > You want you'r page to read the course name's, put them in a drop-down
>> list,
>> > and print the availability at the very end.
>> >
>> > We can start with just a very simple script like this:
>> > <?PHP
>> >
>> > //alter you variables here
>> > $host = "localhost";
>> > $username="";
>> > $password="";
>> > $database="";
>> >
>> > // create link to database
>> > $link = mysql_connect($host, $username, $password)
>> > or die("Cannot connect to database");
>> >
>> > mysql_select_db($database)
>> > or die("Cannot select database");
>> >
>> > //now we make te query which will ask the database for the course
>> > information
>> > //change the my_table to your course table name
>> >
>> > $query = "SELECT * FROM my_table";
>> > $result = mysql_query($query)
>> > or die("Query cannot be executed");
>> >
>> > //now we are going to print the information to your screen
>> > while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
>> > printf ("Coursename: %s Availability: %s<br>", $row[0],
>> $row["1"]);
>> > }
>> > ?>
>> >
>> > It is just that simple. Next you have to do is put them i a drop-down
>> menu.
>> > That is nothing more that changing the printf to something that
>> > included
>> the
>> > HTML tag <OPTION>.
>> >
>> > You also want to update your page. That is just an update or insert in
>> > a
>> > mysql and after refreshing the current page things will work just as
>> > you
>> > wanted.
>> >
>> > I hope this solves your question. If you need more on this. You know
> where
>> > to find the answers... here.
>> >
>> > Sander Swuste
>> >
>> Thanks Sander I will give it a go...much appreciated.
>>
> OK this is what I have done:
>
> Here is my database details:
> SQL-query:
> INSERT INTO `course` ( `number` , `places` )
> VALUES (
> CHAR( 'Course 1' ) , '9'
> ), (
> 'Course 2', '11'
> );
> I ran this php script:
> <?PHP
>
> //alter you variables here
> $host = "localhost";
> $username="";
> $password="";
> $database="paul";
>
> // create link to database
> $link = mysql_connect($host, $username, $password)
> or die("Cannot connect to database");
>
> mysql_select_db($database)
> or die("Cannot select database");
>
> //now we make the query which will ask the database for the course
> information
> //change the my_table to your course table name
>
> $query = "SELECT * FROM course";
> $result = mysql_query($query)
> or die("Query cannot be executed");
>
> //now we are going to print the information to your screen
> while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
> printf ("number: %s places: %s<br>", $row[0], $row["1"]);
> }
> ?>
> And I get this error:
> Parse error: syntax error, unexpected T_VARIABLE in c:\wamp\www\paul.php
> on
> line 20
>
> Comments?
>
>
Since you know your field names can change your query to:
$query = "SELECT * FROM course";
to:
$query = "SELECT number,places FROM course";

nb: I would use course as a field name rather than number. As this better
conveys what is contained in the field.
as shown below.

$query = "SELECT course,places FROM course";

and change the fetch loop:
> while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
> printf ("number: %s places: %s<br>", $row[0], $row["1"]);
> }
to
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
printf ("number: %s places: %s<br>", $row['course'], $row['places']);
}

Joseph Melnick
JM Web Consultants
Toronto ON Canada
www.jphp.com

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация