Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

php

Options
  • 27-02-2008 6:02pm
    #1
    Registered Users Posts: 272 ✭✭


    Hi im trying to get a dop down list to populate with a col. from mymsql db.
    <html>
    <body>
    <select name="Computer Name" style="background-color: #008000; color:#FFFFFF;">
    <option>Select Computer</option>';
    <?php>
    $query = "SELECT Computer FROM table";
    $result = mysql_query($query);
    while ($col=mysql_fetch_assoc($result)){
    <option value="$col>}

    </select>
    </body>
    </html>


    Any ideas on what im doing wrong


Comments

  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    A working example:

    [php]
    $sql="SELECT * FROM tbl_name";
    $result=mysql_query($sql) or die(mysql_error());
    while($row=mysql_fetch_array($result)){

    echo $row;
    echo '<br />';

    }

    [/php]

    You didn't echo your result, and you have to specify the field name(s) you want to return as per example. Hope that helps, you should be able to work it out from there.


  • Registered Users Posts: 272 ✭✭hannable80


    Its still not working


    <html>
    <body>
    <select name="Computer Name" style="background-color: #008000; color:#FFFFFF;">
    <option>Select Computer</option>';
    $sql="SELECT Computer FROM $dbname";
    $result=mysql_query($sql) or die(mysql_error());
    while($row=mysql_fetch_array($result)){

    echo $row;
    echo '<br />';

    }
    </select>

    </body>
    </html>

    </code>


    This is what i have going now but no joy still it brings up a dropdown box but only populates its with Computer name.
    I need to get all info for that col in the dropdown less dupilates....

    Also once i have selected a computer name i need to pass it to a button where onclick it will generate a table with the computer info.

    Very new to PHP sorry :eek:


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Ok here's an example of what your doing:

    [php]
    <html>
    <body>
    <select name="Computer Name" style="background-color: #008000; color:#FFFFFF;">
    <option value="">Select Computer</option>';

    <?php //you must open and close your php tags around php code.

    //execute your query
    $sql="SELECT Computer FROM $dbname";
    $result=mysql_query($sql) or die(mysql_error());

    //open the while loop
    while($row=mysql_fetch_array($result)){

    //here you need to do more than just echo the results, you need to enclose them in <option> tags to keep with the html syntax of the select box.
    //also, you need to assign a value to each option. this will likely be the id for each entry, but not necesarily. change ID to your field of
    //choice

    echo '<option value="'.$row.'">'.$row.'</option>';

    } //close your while loop

    // and close your php tags
    ?>

    </select>

    </body>
    </html>
    [/php]

    You had no php tags around your php code. Going by that, I would guess maybe you haven't named the file with the .php extension?


  • Closed Accounts Posts: 81 ✭✭dzy


    hannable80 wrote: »
    Its still not working
    $sql="SELECT Computer FROM $dbname";

    Select ID, Computer From Computers?


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Ah yes, well spotted dzy, you will need to specify the ID in the select also. However that's not the reason it wasn't working, some more fundamental errors going on there.

    Incidentally hannable, where are you specifying $dbname?


  • Advertisement
  • Registered Users Posts: 272 ✭✭hannable80


    I specified it with the db connection the $dbname var is the same as the table name, i defo knew one of you would spot it..I ran a previous SQL statment just to print it out on a table and it worked fine so connection isnt an issue. The PHP?> was called at the start of the connection do i have to close it off and start it again when i get to the sql statement?

    also i should mention this is running in a wordpress pane, I do have the add on to alow php code to execute


  • Registered Users Posts: 272 ✭✭hannable80


    its still isnt working:
    <code>
    <?php
    $dbhost = 'test.test.com';
    $dbuser = 'audit';
    $dbpass = 'audit';

    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
    $dbname = 'WinAudi2';
    ?>
    <html>
    <body>
    <select name="Computer Name" style="background-color: #008000; color:#FFFFFF;">
    <option>Select Computer Name</option>';
    <?php
    $sql="SELECT Computer FROM $dbname";
    $result=mysql_query($sql) or die(mysql_error());
    while($row=mysql_fetch_array($result)){

    echo '<option value="'.$row.'">'.$row.'</option>';

    }
    ?>
    </select>

    </body>
    </html>

    </code>


  • Registered Users Posts: 272 ✭✭hannable80


    or maybe it is the connection!!! help!


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    If you trust me to PM the details feel free, I'll test the connection and code for you.


  • Registered Users Posts: 159 ✭✭magooly


    OP

    As Mirror said, ensure the file has the .php extension. does it connect and display the items correctly when executed on the command line, ie.
    $ php file.php

    if it does maybe your webserver doesnt associate .php files with the loaded modphp library.


  • Advertisement
  • Registered Users Posts: 272 ✭✭hannable80


    got it working cheers lads i was missing the connect statement was thinking the code was wrong for about two days.

    I need now to pass the value in the drop down box to a button to generate a table....How would i pass this >?


  • Closed Accounts Posts: 81 ✭✭dzy


    hannable80 wrote: »
    got it working cheers lads i was missing the connect statement was thinking the code was wrong for about two days.

    I need now to pass the value in the drop down box to a button to generate a table....How would i pass this >?

    Try reading up on HTML forms first and what happens when a form with a Select element is submitted.


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Google forms. Or php forms. etc.

    In fairness you're getting handed this stuff. Are you a student? This would be frowned upon in such a case.


  • Registered Users Posts: 159 ✭✭magooly


    its good practice to always close your connection implicitally when you are done.

    mysql_close($connection);

    You seem to be way ahead of yourself here, maybe spend a day or two on some basic php tutorials on the net everywhere, that would be two days better spent than where you are at now.s


  • Closed Accounts Posts: 402 ✭✭newestUser


    magooly wrote: »
    its good practice to always close your connection implicitally when you are done.

    mysql_close($connection);

    Um, wouldn't that be 'explicitly'?


  • Registered Users Posts: 272 ✭✭hannable80


    Im defo not a student done a fair bit of java programming never php its been about 3 years scince i have done a web page....Forms would this mess up wordpress?


  • Closed Accounts Posts: 81 ✭✭dzy


    hannable80 wrote: »
    Im defo not a student done a fair bit of java programming never php its been about 3 years scince i have done a web page....Forms would this mess up wordpress?

    Forms shouldn't mess up wordpress i'd read up up on them to find out how to use them if i were you first though because it seems that you need to do a bit of research before doing any coding like someone else said you seem to be a bit ahead of yourself.


  • Registered Users Posts: 272 ✭✭hannable80


    do you have to use a separate page to use the $_get


  • Registered Users Posts: 272 ✭✭hannable80


    Even as a Child i was head of myself hahahahah:D


  • Closed Accounts Posts: 81 ✭✭dzy


    hannable80 wrote: »
    do you have to use a separate page to use the $_get

    You can submit your form back to the same page. That's no problem.


  • Advertisement
  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    hannable80 wrote: »
    do you have to use a separate page to use the $_get
    Not necessarily, you can submit a form back on the page it originated from. But seriously, the guys have given you some good advice, spend a few hours at least, if not a couple of days digesting and trying out forms from tutorials, there are literally thousands out there and it would do you good because they tend to be well commented, step-by-step lessons which you will learn from.


  • Registered Users Posts: 159 ✭✭magooly


    newestUser wrote: »
    Um, wouldn't that be 'explicitly'?
    I guess it would... but id be guessing


  • Registered Users Posts: 272 ✭✭hannable80


    cheers for all you help :D


Advertisement