Jump to content

[SOLVED] Help with function to display form from database!


doucie

Recommended Posts

Hello,

 

I'm new to all this php business, so I hope you can help?  I'm trying to write a function which gets all the client names from a table and displays them in a drop down list.  My function is displaying a form with nothing in!  Help!

 

function get_clients()

{

$db=mysqli_connect('localhost','root','','debtnet');

if(mysqli_connect_errno())

{

echo 'Could not connect to the database';

exit;

}

$query="SELECT client_name FROM groups";

$result=mysqli_query($db,$query); ?>

<form action="group.php" method="POST" />

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

<?php while($list=mysqli_fetch_array($result)){

echo "<option value='$list'</option>

</form>";

}

}

Link to comment
Share on other sites

<?php
function get_clients()
{
   $db=mysqli_connect('localhost','root','','debtnet');
   if(mysqli_connect_errno())
   {
      echo 'Could not connect to the database';
      exit;
   }
   $query="SELECT client_name FROM groups";
   $result=mysqli_query($db,$query); ?>
   <form action="group.php" method="POST" />
   <select name="Client" id="Client" />
   <?php while($list=mysqli_fetch_array($result)){
      echo "<option value='".$list['client_name']."'>".$list['client_name']."</option>";

   }
   echo  "</form>";
}
?>

 

try that.

 

EDIT: fixed some syntax I noticed was wrong.

Link to comment
Share on other sites

try this

 

<?php
function get_clients()
{
   $db=mysqli_connect('localhost','root','','debtnet');
   if(mysqli_connect_errno())
   {
      echo 'Could not connect to the database';
      exit;
   }
   $query="SELECT client_name FROM groups";
   $result=mysqli_query($db,$query);

   $results = '<form action="group.php" method="POST" />';
   $results .= '<select name="Client" id="Client" />';
     while($list=mysqli_fetch_array($result)){
      $results .= "<option value='".$list['client_name']."'>".$list['client_name']."</option>";
      }
   $results .= "</form>";
return $results;
}

echo get_clients();
?>

 

leaving it the way you have it you are create a form for every row of data in the table

 

Ray

Link to comment
Share on other sites

yes you can have the value you want "value="blah blah blah"" but show friendly text in the dropdown menu

 

<option value="1">ON</option>

 

will display ON in the dropdown but will pass "1" as the value.

 

If you do not use a value it will pass what is used in between the option tags

 

<option>BlahBlahBlah</option>

 

Usually don't need a value parameter if the actual value is the same as what is being displayed

 

Ray

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.