简体   繁体   中英

I'm new to PHP and I don't understand why I'm getting this undefined variable error

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>';

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">';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM