简体   繁体   English

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

[英]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.这是我的代码的相关部分,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 [Sun Aug 02 04:29:47.726659 2015] [:error] [pid 8108:tid 748] [client ::1:63388] PHP 注意:C:\\Apache24\\htdocs\\Prototype\\New Working Prototype 中的未定义变量:results_string \\QueryResults.php 第 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(!is_string($_SESSION['results']))块中定义results_string If that if ends up being false, results_string will not be defined in the else block.如果if最终为 false,则results_string将不会在else块中定义。 I'd suggest moving the following line before the if block:我建议在 if 块之前移动以下行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我是 php 的新手,我收到此错误“PHP Parse error: syntax error, unexpected '$sum' (T_VARIABLE)” - I'm new to php I'm getting this error "PHP Parse error: syntax error, unexpected '$sum' (T_VARIABLE)" 我在使用 Symfony 的 PhpStorm 中遇到一个我不明白的错误 - I'm getting an error I don't understand in PhpStorm using Symfony 我是PHP新手,但出现此错误: - I'm new in PHP and I'm getting this error: 我收到“语法错误,意外的T_VARIABLE”错误。 我不明白我做错了什么? - I'm getting a “syntax error, unexpected T_VARIABLE” error. I don't see what I'm doing wrong? 无法理解为什么我会收到 substr 错误 - Unable to understand why I'm getting a substr error 不明白为什么我收到 codecept 错误 - Can't understand why I'm receiving an error with codecept 为什么我在此php代码中获得未定义的偏移量:0 - Why i'm getting Undefined offset: 0 in this php code 我有一个PHP错误。 但是我不明白为什么 - I have a php error. But I don't understand why 我在php中遇到多维数组的未定义索引错误 - I'm getting undefined index error for multidimensional array in php PHP的新手,出现了我似乎无法解决的错误 - New to PHP and I'm getting an error that I can't seem to solve
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM