0

I am currently trying to create a ticketing system from scratch using PHP and MYSQL. I would like to have a drop down that allows you to assign tickets to a user based on the user list from MYSQL. Currently this is the code I have to populate the with options:

$sqlq1 = "SELECT fname, lname FROM ticket_users";
$query = mysql_query($sqlq1);
$array = mysql_fetch_array($query);


foreach($array as $row){
echo '<option value ='."$row[fname]".'>'.$row[fname].$row[lname].'</option>';
}

The issues I'm having are three fold:

  1. The code only prints from the first row of the SQL
  2. The code does not cycle equal to the number of user records
  3. When the code does print the information from the first row it's only printing the first characters from each field and is multiplying them.

The output looks like this:

<h3> Assign To: <select name="assignee">
<option value =J>JJ</option>
<option value =J>JJ</option>
<option value =F>FF</option>
<option value =F>FF</option>

</select></h3>

And this is what a print_r of the array looks like:

Array ( [0] => Jim [fname] => Jim [1] => Frail [lname] => Frail )

And Here's the var dump:

array(4) { [0]=> string(3) "Jim" ["fname"]=> string(3) "Jim" [1]=> string(5) "Frail" ["lname"]=> string(5) "Frail" }

I wasn't even sure how to search for a solution to this issue so hopefully my title will help some other people find the solution as well.

Thanks in advance for the help.

PS. After some looking in to it I have been able to determine that the J's all come from the first name of the first row and all the F's come from the last name of the first row. Hope this helps.

Fedaura
  • 13
  • 6
  • 3
    `opton` <= a typo. Plus, you should quote your values. – Funk Forty Niner May 01 '15 at 18:48
  • Thanks for the catch on that! Unfortunately doesn't seem to be what was causing the echo to get confused. – Fedaura May 01 '15 at 18:50
  • Also, you're using `mysql_` functions which [are deprecated and will be removed in PHP 7](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Machavity May 01 '15 at 18:50
  • update your question then in order to reflect it. – Funk Forty Niner May 01 '15 at 18:50
  • Write `var_dump($array);` above the `foreach loop` [Debugging purposes only] – Ofir Baruch May 01 '15 at 18:50
  • *"The code only prints from the first row of the SQL"* - Use `mysql_fetch_assoc` instead of `mysql_fetch_array` including a `while` loop. – Funk Forty Niner May 01 '15 at 18:51
  • *"Thanks for the catch on that! Unfortunately doesn't seem to be what was causing the echo to get confused. "* - Again, and please update your question's code where `opton` are all typos. It's misleading and could lead to some false answers. – Funk Forty Niner May 01 '15 at 18:56
  • Updated to show the Var Dump and the Typo fix – Fedaura May 01 '15 at 19:01
  • You've been given answers below. Have you tried any of them? If so and none worked, please let them know by placing a comment underneath their answer. If it did, then consider accepting their answer. – Funk Forty Niner May 01 '15 at 19:17

3 Answers3

1

Quotes can be very confusing, I always prefer to set the variables outside the echo block. The while loop saves some code.

Try this instead:

$sqlq1 = "SELECT fname, lname FROM ticket_users";
$query = mysql_query($sqlq1);

while($row = mysql_fetch_array($query)){
 $fname =  $row['fname']; // you missed the single-quotes
 $lname =  $row['lname'];
echo "<option value ='$fname'>$fname $lname</option>";
}
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • 1
    Thanks that makes it much easier to read. I didn't realize that the query didn't just pull the whole column all at once. – Fedaura May 01 '15 at 19:16
0

You need a while loop top iterate over your recordset. This way you pull one record per row and echo that

$sqlq1 = "SELECT fname, lname FROM ticket_users";
$query = mysql_query($sqlq1);

while($row = mysql_fetch_array($query)){
    echo '<option value ="' . $row['fname'] . '">' . $row['fname'] . $row['lname'] . '</option>';
}
Machavity
  • 30,841
  • 27
  • 92
  • 100
  • Indeed, `$array = mysql_fetch_array($query);` is only fetching the first one I believe. – Ye. May 01 '15 at 18:52
0

mysql_fetch_array fetches a result row as an associative array, a numeric array, or both. If you want to fetch all records returned by query you need to use while loop similar to:

 while($array = mysql_fetch_array($query)){
    echo '<option value ='."$array[fname]".'>'.$array[fname].$array[lname].'</opton>';
    }
Manish Shukla
  • 1,355
  • 2
  • 8
  • 21