1

This question is relating to a problem that a previous questions answer yielded! (as is the way)

I have set up a simple script to access a MySQL database and populate a dropdown combobox with usernames, but it wont accept the values from my php, it creates the names however so its some form of syntax error but not one that causes a crash.

<?php

 # parameters for connection to MySQL database
              $hostname="";
              $database="";
              $ausername="";
              $apassword="";

              mysql_connect ("$hostname","$ausername","$apassword");
              mysql_select_db ("$database");

$query = mysql_query("SELECT DISTINCT username FROM dbusers") or die(mysql_error());
?>

<div id="select_users" style="position:absolute;width:466px;height:108px;">
<form name="select_user" method="POST" action="./page5.php" id="Form1">
<div id="select_a_user" style="position:absolute;left:0px;top:20px;width:100px;height:20px;z-index:11;text-    align:left;">
<span style="color:#000000;font-family:Arial;font-size:15px;">Select User</span></div>
<select name="users[]" multiple = "multiple"  id="users" style="position:absolute;left:93px;top:15px;width:200px;height:75px;z-index:12;">

<option value="test"> Select a user</option>
 <?php
 while($row = mysql_fetch_assoc($query)){
    $username = $row["username"];
    ?>
  <option value= <?php $username ?> > <?php echo  $username ?> </option>
<?php
  }?>
</select>
<input type="submit" id="Button1" name="" value="Submit" style="position:absolute;left:93px;top:100px;width:96px;height:25px;z-index:13;">
</form>
    </div>

the section

<option value= <?php $username ?> > <?php echo  $username ?> </option>"

is what is causing the issue i think as its not assigning the variable value

Im using the following script to debug and display what is supposed to be inside the array after POST

<?php

if(isset($_POST['users'])) 
{
      $ausers = $_POST['users'];

  if(!isset($ausers)) 
  {
    echo("<p>You didn't select any users!</p>\n");
  } 
  else
  {
    $nusers = count($ausers);

    echo("<p>You selected $nusers user: ");
    for($i=0; $i < $nusers; $i++)
    {
      echo($ausers[$i] . " ");
    }
    echo("</p>");
  }
}

?>

It will output the number of data files in the array but wont display their content, which after force echo'ing the variables has lead me to believe that the values of the array are all empty.

The end result of this selection is to store each entry of the array as a variable for another MySQL query if that any help with the correct code to achieve the result.

Thanks!

OJ102
  • 49
  • 8
  • [Please stop using `mysql_*`.](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Terry Harvey May 23 '14 at 18:59
  • I will update the mysql code once I have the rest of this script working, im aware it may soon stop being effective. its just simply changing the mysql_ to mysqli_ isnt it? – OJ102 May 23 '14 at 19:01
  • I know it's not what you want to hear, but you should change your methods now before you get into bad habits. – Terry Harvey May 23 '14 at 19:03

2 Answers2

1
<option value= <?php $username ?> 
                    ^----you forgot 'echo' here

No echo, no output, and your generated html becomes

<option value=>foo</option>
Marc B
  • 356,200
  • 43
  • 426
  • 500
0

I am not sure what the issue is here, sometimes when dealing with a database though it will create a multidimensional array. If this is the case you aren't actually looking at any values. To help you check try doing a var_dump on the variable to see everything about the data. It will even tell you if it is truly empty it should look like this:

echo var_dump($varName);

That should help you with debugging and maybe help you post some better information.

CipherLock
  • 26
  • 5