Re: Basic PHP question...




"Joseph Melnick" <jmelnick@xxxxxxxx> wrote in message
news:zcudnbipmuBAxqzeRVn-ig@xxxxxxxxxxxxx
> Hello Plato, You wrote:
> "plato" <platoTAKETHISOUT@xxxxxxxxxxxxxxxxx> wrote in message
> news:43309ee3@xxxxxxxxxxxxxxxxxxxxxxx
> >
> > "plato" <platoTAKETHISOUT@xxxxxxxxxxxxxxxxx> wrote in message
> > news:433082c3@xxxxxxxxxxxxxxxxxxxxxxx
> >>
> >> "Sander" <swuste zit bij cartel.nl> wrote in message
> >> news:43301fef$0$11867$ba620dc5@xxxxxxxxxxxxxxxxxxxxxx
> >> >
> >> > "plato" <platoTAKETHISOUT@xxxxxxxxxxxxxxxxx> schreef in bericht
> >> > news:43300681@xxxxxxxxxxxxxxxxxxxxxxx
> >> > >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
>
Still having the same problem Joe,

I followed your advice and have created the table :courses and inserted 2
fields course and places and populated them with course 1 and 11 places
(integer) and course 2 and 15 places (integer) running this 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 course,places FROM courses";
$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 ("course: %s places: %s<br>", $row['course'],
$row['places']);
}
?>

Still getting this error:

Parse error: syntax error, unexpected T_VARIABLE in c:\wamp\www\Untitled.php
on line 20

but I can't see anything wrong with that line??


.



Relevant Pages

  • Re: DBMS and lisp, etc.
    ... Naively implemented with SQL, again for 10 ... (1 query for the initial orders, 1 query for each order for its ... soon as you upgrade to the SQL database. ... (eq (order-customer orderA) ...
    (comp.lang.lisp)
  • Re: mysql_query(): supplied argument is not a valid MySQL-Link resource
    ... When I had my first attempt at using functions in a PHP script I had similar ... I got round it by passing all variables outside the function into ... > close a connection to the database very time I run a query. ...
    (comp.lang.php)
  • Re: OT: SQL
    ... query processing. ... FROM Employees e, Employees m, Management mgt ... Manager and Employee Salaries. ... The scheme used does not model database files in general, ...
    (sci.logic)
  • Re: access 2003
    ... I removed the parameters from the form query source. ... boxes from the form header, events, code, etc and ran the form query source ... forms queries and the SQL because syntax of the SQL will change randomly. ... the Access 97 database, I wouldn't have thought any expressions would be ...
    (microsoft.public.access.conversion)
  • RE: Import external data - web query
    ... Your reply for my query is very extensive, this is for importing a file from ... The data source I want isn't listed in the Select Data Source dialog box. ... information used to connect to a database. ... Check your driver First, make sure you have the right ODBC driver (Open ...
    (microsoft.public.excel.misc)