-1

This is the relevant part of my code and the variable issue arises with results_string. I will strong/emphasis the exact line but I wanted to give the full scope of it as well.

Edit: Apparently I can't strong/emphasis code so the three *** will have to do to signify where the error happens.

It is really annoying me that I can't figure out this simple issue. I really appreciate any help that is given here.

$database = Database_Factory::create(1);
$database->getConnection($_SESSION['url'],$_SESSION['username'],$_SESSION['password'],$_SESSION['database']);
$results_string;

if(isset($_SESSION['query'])){
    $_SESSION['results'] = $database->run_query($_SESSION['query']);
    if(!is_string($_SESSION['results'])){
        $results = $_SESSION['results'];
        $field_count = $results->field_count;
        $fields = $results->fetch_fields();

         $results_string = '<table border=1><tr style="color:red; background-color: #b0c4de">';
        foreach($fields as $field){
            $results_string .= '<td>'.$field->name.'</td>';
        }
        $results_string .= '</tr>';
        while($row = $results->fetch_row()){
            $results_string .= '<tr style="color:orange; background-color: #e0ffff">';
            for($i = 0; $i < $field_count; $i++){
                $results_string .= '<td>'.$row[$i]."</td>";
            }
            $results_string .= '</tr>';
        }
        $results_string .= '</table>';
    }else{
       $results_string = "<h2 style='color:red;'>".$_SESSION['results']."</h2>";
    }
    unset($_SESSION['query']);
}else if(isset($_SESSION["update"])){
    if(preg_match('/^insert into shipments/',strtolower($_SESSION['update'])) || preg_match('/^update shipments/',strtolower($_SESSION['update']))){ 
        $supplier_snum;
        $updateSuppliers = false;    

       $first = strpos($_SESSION['update'],"(")+1;
        $last = strpos($_SESSION['update'],")");


        $temp = substr($_SESSION['update'],$first,$last);
        $temp = preg_replace('/\)/', '',$temp);
        $temp = preg_replace('/\;/','',$temp);
        $temp = preg_replace('/\'/','',$temp);
        $temp = preg_replace("/\s+/","",$temp);
        $values = explode(",",$temp);
        foreach($values as $val){
            if($val >= 100){
                $updateSuppliers = true;
            }else if(preg_match('/^S/',$val)){
                $supplier_snum = $val;
            }
        }
    }


    $_SESSION['results'] = $database->run_update($_SESSION['update']);
    if(is_numeric($_SESSION['results'])){
        $results_string .= '<h2 style="color:green">'.$_SESSION['results'].' number of rows have been successfully updated!</h2>';
        if($updateSuppliers){
            $snums = $database->run_query("select DISTINCT(suppliers.snum) from suppliers join shipments on suppliers.snum = shipments.snum and shipments.quantity >= 100");
            $csl_snums .= "'".$supplier_snum."'";
            while($row = $snums->fetch_row()){
                for($i = 0; $i < $snums->field_count; $i++){
                    $csl_snums .= ",'".$row[0]."'";
                }
            }
            $blah = $database->run_update("UPDATE suppliers set status = (status+ 5) where snum IN (".$csl_snums.")");
            $results_string .='<br /><h2 style="color:green"> Business Logic Dectected! '.$blah.'</h2>';
        }
    }else{
        ***$results_string .= '<h2 style="color:red">'.$_SESSION['results'].'</h2>';***
    }
    unset($_SESSION['update']);
}else if(isset($_POST['logout'])){
        session_destroy();
        header('Location:index.php');
}

The error is

[Sun Aug 02 04:29:47.726659 2015] [:error] [pid 8108:tid 748] [client ::1:63388] PHP Notice: Undefined variable: results_string in C:\Apache24\htdocs\Prototype\New Working Prototype\QueryResults.php on line 119

The error happens on this line (As I said at the start, I went ahead and put strong/emphasis to make it clear in the code above):

$results_string .= '<h2 style="color:red">'.$_SESSION['results'].'</h2>';
Euno17
  • 11
  • 1
  • 1
    try changing $results_string; to $results_string=''; and see if it works – Satya Aug 02 '15 at 08:56
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Clive Aug 02 '15 at 09:02
  • Thanks -- I just had to do one other thing to get it to work. – Euno17 Aug 02 '15 at 09:15

1 Answers1

1

You are defining results_string inside the if(!is_string($_SESSION['results'])) block. If that if ends up being false, results_string will not be defined in the else block. I'd suggest moving the following line before the if block:

$results_string = '<table border=1><tr style="color:red; background-color: #b0c4de">';
Oliver
  • 3,981
  • 2
  • 21
  • 35
  • I apologize but that results string runs actually fine. It's the second results string for update -- **$results_string = '';** that doesn't want to seem to run. (pressing enter sends a comment which is annoying). And again me being a noob with php but in any event, if that idea still applies for the second one. By moving up, wouldn't that mess up what I I want to use result_string for other purposes? like for when there is a successful result? Or maybe I should just use two variables in that case?
    – Euno17 Aug 02 '15 at 09:04
  • @Euno17 My answer still holds though (still the same problem). Change the third line to `$results_string = "";` and you should be fine. – Oliver Aug 02 '15 at 09:06
  • kk -- sorry I'm new to this place and I didn't realize pressing enter would send the comment. I tried to put more in before you replied. Alright that makes sense. – Euno17 Aug 02 '15 at 09:10
  • I ended up doing if(!(is_numeric($_SESSION['results']))){ $results_string .= '

    '.$_SESSION['results'].'

    '; and then having the good results in the else. Is that good practice or should I just do it your way?
    – Euno17 Aug 02 '15 at 09:19
  • @Euno17 First off, you don't need to put `is_numeric` inside parentheses. `!is_numeric()` works fine. Secondly, if that is the only line you changed, then **you will still run in the same problem** if `$_SESSION['results']` is numeric. – Oliver Aug 02 '15 at 09:23
  • Okay, thanks for all of your help. Seriously, thanks. I won't waste any more of your time! You have a good day =). – Euno17 Aug 02 '15 at 09:31