-1

So I've got a dropdown that has a list of customers that you can select from. It uses a function (that someone else built) which works fine when there are 2 or more customers but dies when there it only one. When there's one customer, each item in the dropdown is the first character of each column for that one customer.

Here is the function:

function getCustomerBy($column = "",$value = "")
        {
            global $conn;
            if ($value == '')
                {
            return(null);
                }
            $sel = "SELECT 
            customers.*,
            customerStatus.code customerStatus
            FROM customers,
            customerStatus      
            WHERE customer_id
            and  customerStatus.id = customers.customerStatus_id and   ". mysqli_real_escape_string($conn,$column) ."='". mysqli_real_escape_string($conn,$value) ."'";
            //      error_log($sel);
            $run = mysqli_query($conn, $sel);
            $check = mysqli_num_rows($run);

            if ($check ==1)
            {
                    $row = mysqli_fetch_assoc($run);
                    return($row);
            }
            elseif($check >1)
            {
                    $rows = array();
                    while ($row = mysqli_fetch_assoc($run))
            {
                    $rows[] = $row;
            }
                    return($rows);
            }
            else
            {
                    return(null);
            }
    }

I'm fairly certain that it's the ($check == 1) stuff but I can't work out the best way to re-do all of that to make it work without causing other errors (specifically "cannot modify header")

This is what's called up on the page with the dropdown:

 $customers = getCustomerBy('users_user_id',$user['user_id']); 

Also, here is the code for the dropdown:

 <?
      foreach ($customers as $customer)
      {
      $selected = '';
      if (isset($gig['customers_customer_id']) && $customer['customer_id'] == $gig['customers_customer_id'])
 {
      $selected = ' selected ';
 }
 echo "\t<option value=\"".$customer['customer_id']."\"      $selected>".$customer['customer_company_name']."</option>";
 }
 ?>

Broken dropdown

Topher
  • 181
  • 1
  • 2
  • 13
  • *"(specifically "cannot modify header")"* - Fix that and see where your code goes. http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Funk Forty Niner Oct 27 '15 at 19:01
  • That error only happened when I hacked up the code above, and probably badly, which is why I'm hoping someone might be able to help steer me in the right direction :) – Topher Oct 27 '15 at 19:05
  • Why not just get rid of the `if $check == 1` block altogether and treat both cases the same? – miken32 Oct 27 '15 at 22:13

2 Answers2

0

I don't know how the returning array is used by the rest of your code but you can try the following:

Change:

if ($check ==1) {
    $row = mysqli_fetch_assoc($run);
    return($row);
}

To this:

if ($check == 1) {
    $rows = array();
    $rows[] = mysqli_fetch_assoc($run);
    return($rows);
}
Kostas Mitsarakis
  • 4,772
  • 3
  • 23
  • 37
  • Thanks. That throws the header error: Warning: Cannot modify header information - headers already sent by (output started at /home/chrisstolz/public_html/managegigs.com/client/layouts/layout_head.php:8) in /home/chrisstolz/public_html/managegigs.com/client/includes/functions.php on line 33 Line 8 is: ".$title." | ManageGigs"; ?> – Topher Oct 27 '15 at 19:55
  • Have a look here: http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php and here: http://stackoverflow.com/questions/9707693/warning-cannot-modify-header-information-headers-already-sent-by-error – Kostas Mitsarakis Oct 27 '15 at 20:02
0

The code that builds the dropdown is expecting an array of arrays, but instead when it's a single row you're passing it an array of strings. Treat both cases ($check > 0) the same.

function getCustomerBy($column = "",$value = "")
    {
        global $conn;
        if ($value == '')
            {
        return(null);
            }
        $sel = "SELECT 
        customers.*,
        customerStatus.code customerStatus
        FROM customers,
        customerStatus      
        WHERE customer_id
        and  customerStatus.id = customers.customerStatus_id and   ". mysqli_real_escape_string($conn,$column) ."='". mysqli_real_escape_string($conn,$value) ."'";
        //      error_log($sel);
        $run = mysqli_query($conn, $sel);
        $check = mysqli_num_rows($run);

        if($check > 0)
        {
                $rows = array();
                while ($row = mysqli_fetch_assoc($run))
        {
                $rows[] = $row;
        }
                return($rows);
        }
        else
        {
                return(null);
        }
}

I assume there's copy/paste errors in your query because it doesn't look right.

miken32
  • 42,008
  • 16
  • 111
  • 154