PHP replacing拒绝用变量的值替换变量,尽管代码中的值已经更新

t0ybt7op  于 2023-03-11  发布在  PHP
关注(0)|答案(1)|浏览(123)

我正在写一个PHP脚本来处理我的提交系统的表单验证。但是有一个错误,我一直在得到,我不能绕我的头周围如何工作。基本上:有一个名为$resourceContent的变量,它会根据提交的类型进行更新(意思是如果是文件,内容就是文件名,如果是链接,内容就是链接)。现在,$resourceContent肯定已经更新了。如果我在echo $submission_success_html行之前运行一个echo,则值显示为正确的。$submission_success_html是一串html代码,用来构建成功页面,这是html的样子。

$submission_success_html = "
<div class='submission_result'>
<h2>Your submission has been received!</h2>
<div class='resource_infopanel'>
       <b>Resource Name</b><span class='resource_property'>$resourceName</span>
       <br>
       <b>Resource Description</b><span class='resource_property'>$resourceDescription</span>
       <br>
       <b>Resource Type</b><span class='resource_property' id='resourceType'>$resourceType</span>
       <br>
       <b>Resource Content</b><span class='resource_property'>$resourceContent</span>
</div>
<br>
<span class='resource_infoflair'>Your submission will now be processed and reviewed. You can view the status for ongoing reviewals at the <a
        href='../html/reviewals.html'>resource reviewals page</a>.</span>
<hr>
<span class='resource_infoflair'>Your resource reference code is <b>$referenceCode</b></span>
</div>
";

注意:$resourceName和其他内容被正确替换为它们的实际值。但是$resourceContent$referenceCode被替换为它们的默认值,前者为“”,后者为“unknown”。
我是不是犯了什么错?
下面是该脚本的其余代码:

<?php

include "connection.php";

// Posted submission data
$resourceName = $_GET['resourceName'];
$resourceDescription = $_GET['resourceDescription'];
$resourceType = $_GET['resourceType'];
$resourceLink = $_GET['resourceLink'];
$resourceFile = $_GET['resourceFile'];
$resourceContent = "";
$referenceCode = "unknown";

// Submission control
$allowSubmit = true;
$encounteredErrors = false;
$error_descriptions = "";
$error_array = array();

// Database connection
$dblink = "";

// HTML

$submission_success_html = "
<div class='submission_result'>
<h2>Your submission has been received!</h2>
<div class='resource_infopanel'>
       <b>Resource Name</b><span class='resource_property'>$resourceName</span>
       <br>
       <b>Resource Description</b><span class='resource_property'>$resourceDescription</span>
       <br>
       <b>Resource Type</b><span class='resource_property' id='resourceType'>$resourceType</span>
       <br>
       <b>Resource Content</b><span class='resource_property'>$resourceContent</span>
</div>
<br>
<span class='resource_infoflair'>Your submission will now be processed and reviewed. You can view the status for ongoing reviewals at the <a
        href='../html/reviewals.html'>resource reviewals page</a>.</span>
<hr>
<span class='resource_infoflair'>Your resource reference code is <b>$referenceCode</b></span>
</div>
";

$submission_failure_html = "
<div class='submission_result'>
       <h2>Your submission could not be processed!</h2>
       <h3>Here's what we think went wrong:</h3>
       <div class='resource_infopanel'>
              <ul>
              $error_descriptions
              </ul>
       </div>
       <br>
       <span class='resource_infoflair'>Your submission will now be processed and reviewed. You can view the status for ongoing reviewals at the 
            <a href='../html/reviewals.html'>resource reviewals page</a>.
       </span>
       <hr>
       <span class='resource_infoflair'>Your resource reference code is <b>$referenceCode</b></span>
</div>
";



echo "<head><link href='../css/responsepages.css' rel='stylesheet'><title>Processing...</title></head>";
process();




// Post the entry to the submission database
function submit(){
    global $referenceCode;
    global $resourceName;
    global $resourceDescription;
    global $resourceContent;
    global $allowSubmit;
    global $encounteredErrors;

    if ($allowSubmit) {
        $sql = "[unrelated]";
        $result = queryDatabase($sql);
        if ($result >= 0) {
            $encounteredErrors = false;
        }else{
            $encounteredErrors = true;
        }
    }
    finalize();
}

// Check the posted data to make sure it's all there
function checkData(){

    global $resourceName;
    global $resourceDescription;
    global $resourceType;
    global $resourceFile;
    global $resourceLink;
    global $resourceContent;

    global $encounteredErrors;
    global $error_array;
    global $allowSubmit;

    if (trim($resourceName) == ""){
        $error_array[] = "unset-name";
    }
    if (trim($resourceDescription) == ""){
        $error_array[] = "unset-desc";
    }
    if (trim($resourceType) == ""){
        $error_array[] = "unset-type";
    }
    if ((trim($resourceType) == "file" || trim($resourceType) == "both") && trim($resourceFile) == ""){
        $error_array[] = "unset-file";
    }
    if ((trim($resourceType) == "link" || trim($resourceType) == "both") && trim($resourceLink) == ""){
        $error_array[] = "unset-link";
    }

    // Validate data
    $resourceName = validateString($resourceName);
    $resourceDescription = validateString($resourceDescription);
    $resourceType = validateString($resourceType);
    $resourceContent = validateString($resourceContent);
    $resourceLink = validateString($resourceLink);
    $resourceFile = validateString($resourceFile);

    // Conclude

    if (count($error_array) > 0){
        $encounteredErrors = true;
        $allowSubmit = false;
        parseErrors();
        finalize();
    }else{
        $encounteredErrors = false;
        $allowSubmit = true;

        // Set the resource content to whatever it should be depending on the type
        if ($resourceType == "link"){
            $resourceContent = "<b>" . $resourceLink . "</b>";
        }else{
            $resourceContent = "File: <b>" . $resourceFile . "</b>";
        }
        generateReferenceCode();
        submit();
    }
}

// Strip the malicious shit from the strings.
function validateString($inputString){
    $inputString = trim($inputString);
    $inputString = stripslashes($inputString);
    $inputString = htmlspecialchars($inputString);
   return strip_tags($inputString);
}

// Check the error array and put the required descriptions for them into the description array.
function parseErrors(){
    global $error_array;
    global $error_descriptions;

    foreach ($error_array as $error){
        switch ($error){
            case "unset-name":
                $error_descriptions .= "<br><b>There's no name provided for the resource.</b>";
                break;
            case "unset-desc":
                $error_descriptions .= "<br><b>There's no description provided for the resource.</b>";
                break;
            case "unset-type":
                $error_descriptions .= "<br><b>An error prevented us from determining the type of your submission.</b>";
                break;
            case "unset-file":
                $error_descriptions .= "<br><b>A file was required by the selected submission type, but was not provided by you.</b>";
                break;
            case "unset-link":
                $error_descriptions .= "<br><b>A link was required by the selected submission type, but was not provided by you.</b>";
                break;
        }
    }

    finalize();
}

function generateReferenceCode(){
    global $referenceCode;
    global $encounteredErrors;
    global $error_descriptions;
    global $allowSubmit;

    $sql = "[unrelated]";
    $rowCount = queryDatabase($sql);
    $rowCount = $rowCount[0];

    if ($rowCount != null){
        if ($rowCount <= 9){
            $referenceCode = "#000" . strval($rowCount + 1);
        }else{
            if ($rowCount <= 99){
                $referenceCode = "#00" . strval($rowCount + 1);
            }else{
                if ($rowCount <= 999){
                    $referenceCode = "#0" . strval($rowCount + 1);
                }else{
                    $referenceCode = "#" . strval($rowCount + 1);
                }
            }
        }
    }else{
        $encounteredErrors = true;
        $allowSubmit = false;
        $error_descriptions .= "<br><b>The system was unable to generate a reference number due to an internal error.</b>";
    }

}

// Start the overall processing of the submission.
function process(){
    checkData();
}

// Echo out the correct html and finalize the operation.
function finalize(){
    global $encounteredErrors;
    global $submission_success_html;
    global $submission_failure_html;
    global $resourceType;
    global $resourceLink;
    global $resourceFile;
    global $resourceContent;
    global $referenceCode;

    if ($resourceType == "link"){
      $resourceContent = "<b>" . $resourceLink . "</b>";
    }else{
      $resourceContent = "<b>File: " . $resourceFile . "</b>";
    }

    if ($encounteredErrors){
      echo $submission_failure_html;
    }else{
      echo "ASS: $referenceCode";
      echo $submission_success_html;
    }
}

注:我已经删除了一些不相关的东西,如SQL查询和queryDatabase() helper函数,因为它们不以任何方式使用这些变量,也不与它们有任何关系。

omqzjyyz

omqzjyyz1#

谢谢大家的帮助!我不是一个PHP高手,我只是用它来连接数据库和其他东西,因为这是我唯一知道和学会使用的方法。
对我的问题的修复如下:我已经把变量声明移到了唯一真正需要它们的地方:导入到finalize()方法中,所以现在创建的变量有了正确的值。另外,我学到了关于PHP的一个新东西。
编辑:哎呀,不知怎么的,一些无意的自我表扬溜了进来。事实上,我不是一个PHP大师。

相关问题