Problem with mysql dropdownlist in php not working


#1

Hi, I working on my personal project on my own and I attempt to try and get the dropdownlist from mysql,php and html to work but it didn't work.

This is what the actual result look like:

Here's my actual code and it contains the comments:

<!DOCTYPE html>
<html>
    <body>
<!--This is the form for the textbox firstname,lastname,password,email address and dropdownlist that will be call from the database-->
        <form method="POST">
            First name:<br>
            <input type="text" name="FirstName">
            <br>
            Last name:<br>
            <input type="text" name="LastName">
            <br>Password:<br>
            <input type ="text" name="Password">
            <br>
            Email Address:<br>
            <input type="text" name="EmailAddress" value="" />
            <br>

<!--This is the Role Name that will be populate from the mysql database in the php section-->
            Role Name:<br>
            <!--<input type="text" name="RoleName" value="2" /> -->
            <select name='RoleName'>           
            <br><br>
<!--This is the submit button-->
            <input type="submit">

<!--</form> -->

      
        <?php
        
        
        
        
  //connect to database 
  $connection = mysqli_connect("localhost", "user", "password", "construction", "3306");

  //run the store procedure call getallroles for dropdownlist
  $sql = mysqli_query($connection, 
     "CALL getallroles") or die("Query fail: " . mysqli_error());  //run your query

// That selectname RoleName came from the html section and the dropdownlist should work only from html5 section not
//php section I want to get of echo "<select name='RoleName'>"; // list box select command but I can't
//because its part of mysql database
  
  echo "<select name='RoleName'>"; // list box select command
 
	//This is the part where it fetches the RoleNames for Admin and User Names
while($row=mysqli_fetch_array($sql))
        { $select.='<option value="'.$row[0].'">'.$row[0].'</option>';
//Option values are added by looping through the array
  }
  
   
        
      
        
           echo $select;// Closing of list box

//This is the part where it prints out but the RoleName dropdownlist does not work
        
  echo("First name: " . $_POST['FirstName'] . "<br />\n");
        echo("Last name: " . $_POST['LastName'] . "<br />\n");
        echo("Email Address: " . $_POST['EmailAddress'] . "<br />\n");
        echo("Role Name: " . $_POST[$select] . "<br />\n");
       
        
        ?>

</select>
        
         </form> 

    </body>
</html>

#2

This is the result that might like like this:


#3

<select></select> is a container object that requires both opentag and endtag. The child elements are <option></option>, also container elements.

Your form HTML is not well formed. Raw text should be incorporated into labels with a form control context.

<label for="firstname">First Name: </label>
<input id="firstname" name="firstname" type="text">

LABEL is block level so all those nasty line breaks can be removed.


#4

I've added label tags and I've having problem with dropdownlist not able to populate and I wrote the comments under select name='RoleName:



    <form method="POST">
        <label for= "FirstName">First name:<br> </label>
        <input id="FirstName" name="FirstName" type="text">
        <br>
        <label for="LastName">Last name:<br> </label>
        <input id="LastName" name="LastName" type="text">
        <label for="Password"><br>Password:<br> </label>
        <input id ="text" name="Password" type="text">
        <br>
        <label for="EmailAddress">Email Address:<br></label>
        <input id="text" name="EmailAddress" type="text" />
        <br>
        <label for="RoleName"> Role Name:<br> </label>
        //This is the part where getting dropdown list to work but it didn't work but manage to get 
        //the data but not the actual dropdownlist
        <select name='RoleName'> 
            <?php

//connect to database
$connection = mysqli_connect("localhost", "user", "password", "construction", "3306");

//run the store proc
$sql = mysqli_query($connection,
"CALL getallroles") or die("Query fail: " . mysqli_error()); //run your query

echo ""; // list box select command

while($row=mysqli_fetch_array($sql))
{ $select.=''.$row[0].'';
//Option values are added by looping through the array
}
echo $select;// Closing of list box

            ?>
        </select>
        <br><br>
       <!-- <input type="submit" -->
        <button type ="submit" value="Submit">Insert</button>



#5

Here's the picture of actual result


#6

This does not look right.

Remember to generate the OPTION element.


#7

This reminds me so much of my early days brute forcing...good times ^^ not really...