SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Enthusiast marc_2ky's Avatar
    Join Date
    Apr 2004
    Location
    Glasgow, Scotland
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face Reading sql data into a form list?

    Hi, does anyone know how I could read a column from a sql database into a form list.

    I have tried numerious ways such like...

    Code:
    <option value="<?php echo $_POST["id"]; ?>"><?php echo $_POST["id"]; ?></option>
    or
    Code:
    <option value="<?php echo $id;?>"><?php echo $id;?></option>
    but all that shows up on the form list is empty.

    Please can anyone advise me on what I am doing wrong and how to show the data from the column.

    Many Thanks
    Marc
    I am a learner, be gentle!

  2. #2
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That looks okay to me, what's your sql? Code?

  3. #3
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, first you need to issue a query to the database. Have you done that? if so, let's see the code you used and then we can show you how to loop through the result.

  4. #4
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?
     
    $res 
    mysql_query("SELECT someField FROM someTable");
    $rec mysql_fetch_array();
    print(
    "<option value=\"" $rec["someField"] . "\">" $rec["someField"] . "</option>");
     
    ?>

  5. #5
    SitePoint Enthusiast marc_2ky's Avatar
    Join Date
    Apr 2004
    Location
    Glasgow, Scotland
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?
    $DBhost = "localhost";
    $DBuser = "idbase";
    $DBpass = "12345";
    $DBName = "idbase";
    $table = "myids";
    mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");

    @mysql_select_db("$DBName") or die("Unable to select database $DBName");

    $sql = mysql_query("SELECT * FROM $table");

    ?>
    I am a learner, be gentle!

  6. #6
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    See comments in code
    PHP Code:
    <?
    $sql 
    mysql_query("SELECT * FROM $table");
    //it's not enough to do the query, you also have to fetch the resultset
    //using mysql_fetch_array(), while will loop through it
    while($row mysql_fetch_array($sql)){
        print 
    "<option value=\"$row[id]\">$row[id]</option>";
    }
    ?>

  7. #7
    SitePoint Member
    Join Date
    Jan 2005
    Location
    New Zealand
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?
    $DBhost 
    "localhost";
    $DBuser "idbase";
    $DBpass "12345";
    $DBName "idbase";
    $table "myids";
    mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");

    @
    mysql_select_db("$DBName") or die("Unable to select database $DBName");

    $sql mysql_query("SELECT * FROM $table");
    //now to get a row:
    while($tablerow mysql_fetch_row($sql)) {
    $id $tablerow[/*table with the row you want goes here*/];
    }
    ?>
    then just echo $id where you want it.

  8. #8
    SitePoint Member
    Join Date
    Jan 2005
    Location
    New Zealand
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol, how many seconds are these post apart .. :P

  9. #9
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not many!

  10. #10
    SitePoint Enthusiast marc_2ky's Avatar
    Join Date
    Apr 2004
    Location
    Glasgow, Scotland
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi guys, thanks for your quick response. I still have nothing showing up

    I will keep working on it though.

    Thanks again
    Marc
    I am a learner, be gentle!

  11. #11
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Does'nt it depend on what you are going to use in your form? I mean, is it going to be one row from your database as in an update form or looping the result into a dropdown list....
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  12. #12
    SitePoint Enthusiast nathanj's Avatar
    Join Date
    Dec 2004
    Location
    Perth, WA, Australia
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP:
    PHP Code:
    <?php

    error_reporting
    (E_ALL);

    $DBhost "localhost";
    $DBuser "username";
    $DBpass "password";
    $DBname "database";

    mysql_connect($DBhost,$DBuser,$DBpass) or die(mysql_error());

    mysql_select_db($DBname) or die(mysql_error());

    // my table only contains id and menu
    $sql mysql_query("SELECT * FROM `table`") or die(mysql_error());

    echo 
    "<form>\n<select name=\"menu\">\n";

    while(
    $row mysql_fetch_row($sql)) {
        
        echo 
    "<option value=\"$row[0]\">$row[1]\n";
        
    };

    echo 
    "</select>\n</form>";

    ?>
    MySQL:
    Code:
    -- 
    -- Table structure for table `table`
    -- 
    
    CREATE TABLE `table` (
      `id` int(10) unsigned NOT NULL auto_increment,
      `menu` varchar(255) NOT NULL default '',
      PRIMARY KEY  (`id`)
    ) TYPE=MyISAM AUTO_INCREMENT=3 ;
    
    -- 
    -- Dumping data for table `table`
    -- 
    
    INSERT INTO `table` VALUES (1, 'php');
    INSERT INTO `table` VALUES (2, 'mysql');
    Outputs:
    HTML Code:
    <form>
    <select name="menu">
    <option value="1">php
    <option value="2">mysql
    </select>
    </form>
    This code has been fully tested and is working fine here. Good luck! Hope this helps ya!

  13. #13
    SitePoint Enthusiast marc_2ky's Avatar
    Join Date
    Apr 2004
    Location
    Glasgow, Scotland
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am only using the form to display a list of what i have in the id column of my table.

    Thanks nathanj, i will try your code.

    Marc
    I am a learner, be gentle!

  14. #14
    SitePoint Enthusiast nathanj's Avatar
    Join Date
    Dec 2004
    Location
    Perth, WA, Australia
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by marc_2ky
    I am only using the form to display a list of what i have in the id column of my table.

    Thanks nathanj, i will try your code.

    Marc
    What is in your id column? Just numbers? If so wouldn't you want the user to be selecting a name and then using the value they selected as what they enter?

    If you look at my code it sets the value to the id and the name to the name of the item in the table. Now if you wanted to add that particular item to a users personal file you just record the id number and later call that particular id from the database table.

    Good luck!

  15. #15
    SitePoint Enthusiast marc_2ky's Avatar
    Join Date
    Apr 2004
    Location
    Glasgow, Scotland
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey nathanj,

    Thanks for your help managed to get it to show

    Thanks again!
    Marc
    I am a learner, be gentle!

  16. #16
    SitePoint Enthusiast nathanj's Avatar
    Join Date
    Dec 2004
    Location
    Perth, WA, Australia
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by marc_2ky
    Hey nathanj,

    Thanks for your help managed to get it to show

    Thanks again!
    Marc
    No problem, glad to help.

  17. #17
    SitePoint Enthusiast marc_2ky's Avatar
    Join Date
    Apr 2004
    Location
    Glasgow, Scotland
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One last question, the code is displaying the data ok (numbers) but is showing it by when i added it and not in order. How can i show the numbers in order?

    Thanks
    Marc
    I am a learner, be gentle!

  18. #18
    SitePoint Enthusiast nathanj's Avatar
    Join Date
    Dec 2004
    Location
    Perth, WA, Australia
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Change the query to the following:

    PHP Code:
    <?php
    $sql 
    mysql_query("SELECT * FROM `table` ORDER BY id") or die(mysql_error());
    ?>
    This will order it by the "id" column in your table.

  19. #19
    SitePoint Enthusiast marc_2ky's Avatar
    Join Date
    Apr 2004
    Location
    Glasgow, Scotland
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again, you're a star!

    Marc
    I am a learner, be gentle!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •