Mysql, PHP and FORM QUERY

I do not know how to consult, and call the fields in a table and display it in a HTML

<select name="" id=""> <option> </option> </select>

This is my code:

<?php  
    $loginDB=mysql_pconnect("localhost", "root", "");
    mysql_select_db("bookstore",$loginDB); 
    $sql="SELECT bookloans.id_usr FROM bookloans"; 
?>
    <select name="id_usr" id="id_usr">
        <option><?php              
            $result=mysql_query($sql);             
            $num_resul=mysql_num_rows($result);             
            for ($i=0;$i<$num_result;$i++)
            {        
                $row=mysql_fetch_array($result);     
                echo $sql['id_usr'] ;
            }
        ?></option>    
    </select>     
</p>  

Answers


You must do your loop around the <option> tag :

<select name="id_usr" id="id_usr">
    <?php           
    $result = mysql_query($sql);                      
    while ($data = mysql_fetch_assoc($result)) {  ?>

        <option><?php echo $data['id_usr']; ?></option>

    <?php   
    }
    ?>
</select>

But as said, the mysql_* functions are deprecated. You should take a look at mysqli_* functions or PDO.


Firstly, mysql_* is deprecated, use mysqli_*, secondly. What exactly are you asking?

Your PHP code is putting everything in one option tag; you must include the opening and closing option tags within your loop.

<select name="id_usr" id="id_usr">
         <?php           
         $result=mysql_query($sql);          
         $num_result=mysql_num_rows($result);             
        for ($i=0;$i<$num_result;$i++)
            {     
                echo "<option>";
                $row=mysql_fetch_array($result);     
                     echo $sql['id_usr'] ;
                echo "</option>";
            }
         ?>

   </select>     
 </p>`

Finally, I'd recommend looking through the PHP and HTML tutorials found at W3Schools.com

More over, I might be tired or mistaken but this may better suit your needs for querying and returning results with PHP and SQL:-

<?php 
$result = mysqli_query($db_con, $query);
if(mysqli_num_rows($result)>0)
{
  while($row=mysqli_fetch_array($result))
  {
      echo "<option>" . $row['id_usr'] . "</option>";
  }
?>

mysqli_query()


Try some thing like this.

<?php  
    $loginDB=mysql_pconnect("localhost", "root", "");
    mysql_select_db("bookstore",$loginDB); 
 ?>
    <select name="id_usr" id="id_usr">
<?php 
 $sql="SELECT bookloans.id_usr FROM bookloans"; 
 $result=mysql_query($sql);             
 while($data=mysql_fetch_array($result))
{
?>
<option value="<?php echo $data['id_usr]; ?>" ><?php echo $data['id_usr];?> </option>
<?php
}
?>
</select>

Since mysql_* functions are deprecated ,better try with mysqli_* functions or PDO.


Need Your Help

Mysql for update

mysql transactions sql-update

MySQL supports "for update" keyword. Here is how I tested that it is working as expected. I opened 2 browser tabs and executed the following commands in one window.

Using BitBlt to capture desktop pixel colors

c winapi bitblt

Right now I'm using GetPixel() to retrieve about 64 pixels from the desktop in order to obtain their color. I read about GetPixel() being slow but didn't think it would matter for a few pixels but ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.