【问题标题】:getting parse error in php file在php文件中获取解析错误
【发布时间】:2018-06-29 17:48:40
【问题描述】:

我用php写了一些代码。示例代码片段如下:

    include('config.php');
    require_once "variables.php";

    global $uploadID = " " ;    //getting error in this line

    function uploadImage($wtI,$tbln,$pri,$db){
        if(is_array($_FILES)) {
        if(is_uploaded_file($_FILES['image']['tmp_name'])) {
            $sourcePath = $_FILES['image']['tmp_name'];
            $targetFolder = "../upload_images/$wtI/";
            if (!file_exists($targetFolder)) {
                mkdir($targetFolder, 0777, true);
            }
            $targetPath = $targetFolder.$_FILES['image']['name'];
            while(file_exists($targetPath)){
                $targetPath = $targetFolder.uniqid().'-'.$_FILES['image']['name'];
            }
            if(move_uploaded_file($sourcePath,$targetPath)){

                $sql = "UPDATE `$tbln` SET image='".substr($targetPath,3)."' WHERE $pri=$uploadID;";
                $result=mysqli_query($db,$sql);
                return true;
            }
            else return false;
        }
    }
 }

问题是我在运行我的 php 文件时收到以下错误消息:

Parse error:syntax error, unexpected '=', expecting ',' or ';' in C:\wamp64\www\project\php\additem.php on line 6

这个错误有什么解决办法吗?

【问题讨论】:

  • 您不能在全局命名空间中将变量声明为全局in,因为如果它存在,它就已经是全局的了。另外,不要使用全局变量,将值作为函数参数传入。
  • 而且您的代码容易受到SQL injection 攻击。您应该通过mysqliPDO 使用带有绑定参数的预处理语句。 This post 有一些很好的例子。
  • 我学会了厌恶global ...我是当时最酷的孩子之一,认为这样做很棒,同伴压力等等...但后来我明智了.
  • 为什么不能将该变量作为参数传递?就像你对传递的其他参数所做的那样?

标签: php parse-error


【解决方案1】:

global 关键字允许您访问全局变量,而不是创建新变量。只需删除那里的全局关键字。 全局关键字必须放在您将要使用变量的函数中。 查看https://www.w3schools.com/php/php_variables.asp 了解如何使用它。

您的代码的更正是:

include('config.php');
require_once "variables.php";
// Changes start here
$uploadID = " ";    //getting error in this line

function uploadImage($wtI,$tbln,$pri,$db){
    global $uploadID;
    //Changes end here
    if(is_array($_FILES)) {
    if(is_uploaded_file($_FILES['image']['tmp_name'])) {
        $sourcePath = $_FILES['image']['tmp_name'];
        $targetFolder = "../upload_images/$wtI/";
        if (!file_exists($targetFolder)) {
            mkdir($targetFolder, 0777, true);
        }
        $targetPath = $targetFolder.$_FILES['image']['name'];
        while(file_exists($targetPath)){
            $targetPath = $targetFolder.uniqid().'-'.$_FILES['image']['name'];
        }
        if(move_uploaded_file($sourcePath,$targetPath)){

            $sql = "UPDATE `$tbln` SET image='".substr($targetPath,3)."' WHERE $pri=$uploadID;";
            $result=mysqli_query($db,$sql);
            return true;
        }
        else return false;
    }
}

}

我是用手机接电话的,请原谅格式问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多