Jump to content

php dropdown list box


ilikephp

Recommended Posts

Hi,

 

I am trying to call the data from Mysql but I am getting an empty drop down list, this is the code:

 

 

mysql:

 

create table years
(  yearID integer auto_increment,
   year varchar(30),
   primary key (yearID)
);

insert into years (yearID, year) values ('1', '2007-2008');
insert into years (yearID, year) values ('2', '2008-2009');
insert into years (yearID, year) values ('3', '2009-2010');
insert into years (yearID, year) values ('4', '2010-2011');
insert into years (yearID, year) values ('5', '2011-2012');
insert into years (yearID, year) values ('6', '2012-2013');

 

PHP:

 

<?php require_once('../Connections/connection.php'); ?>
<?php

$result = @mysql_query( "select yearID, year, from sss.years");

print "<p>Select a year:\n";
print "<select name=\"yearID\">\n";
while ($row = mysql_fetch_assoc($result)){
$yearID = $row[ 'yearID' ];
$year = $row[ 'year' ];
print "<option value=$yearID>$year\n";
}
print "</select>\n";
print "</p>\n";
?>

 

 

Thank you!

Link to comment
Share on other sites

Remove the error suppression and you might actually see an error message. Better, yet, check for one.

 

<?php require_once('../Connections/connection.php'); ?>
<?php

if ($result = mysql_query( "select yearID, year, from sss.years")) {
  if (mysql_num_rpws($result)) {
    print "<p>Select a year:\n";
    print "<select name=\"yearID\">\n";
    while ($row = mysql_fetch_assoc($result)) {
      $yearID = $row[ 'yearID' ];
      $year = $row[ 'year' ];
      print "<option value=$yearID>$year\n";
    }
    print "</select>\n";
    print "</p>\n";
  } else {
    // no record found.
  }
} else {
  trigger_error(mysql_error());
}

Link to comment
Share on other sites

I am using the code below, it is working fine, but how can I add to the first line of the list an item without a value like: "Select please"

 

<?php
$query="SELECT yearID,year FROM years";

$result = mysql_query ($query);
echo "<select name=education_level value=''>Education Level</option>";

while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[yearID]>$nt[year]</option>";
}
echo "</select>";
?>

Link to comment
Share on other sites

Its simply checking to make sure the called functions actually work before using there results in another function.

 

As for you question. Just change....

 

echo "<select name=education_level value=''>Education Level</option>";

 

to....

 

echo "<select name=education_level value=''>Education Level</option>";
echo "<option value=''>Select PLease</option>";

Link to comment
Share on other sites

THANKS A LOT (Y)

 

still have one issue:

 

when the form is submitted successfully the success.php page will open

 

$insertGoTo = "success.php";

 

 

if someone tries to put this link www.mywebsite.php/success.php it will be opened, how can I restricted from being viewed unless the form is submitted?

 

 

Thanks,

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.