我是PHP的新手,我不明白为什么我得到这个未定义的变量错误

时间:2015-08-02 08:53:42

标签: php

这是我的代码的相关部分,并且results_string出现了变量问题。我会强调/强调确切的路线,但我也想给出它的全部范围。

编辑:显然我不能强调/强调代码,因此三个***必须表明错误发生的位置。

我无法弄清楚这个简单的问题。我非常感谢这里给出的任何帮助。

$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');
}

错误是

  

[Sun Aug 02 04:29:47.726659 2015] [:error] [pid 8108:tid 748] [client :: 1:63388] PHP注意:未定义的变量:C:\ Apache24 \ htdocs \ Prototype \中的results_string第119行的新工作原型\ QueryResults.php

错误发生在这一行上(正如我在开始时说的那样,我继续并强调/强调在上面的代码中明确说明):

$results_string .= '<h2 style="color:red">'.$_SESSION['results'].'</h2>';

1 个答案:

答案 0 :(得分:1)

您正在results_string块中定义if(!is_string($_SESSION['results']))。如果if最终为假,则results_string块中将不会定义else。我建议移动以下行之前 if块:

$results_string = '<table border=1><tr style="color:red; background-color: #b0c4de">';