【问题标题】:getting T_ENCAPSED_AND_WHITESPACE error in PHP在 PHP 中出现 T_ENCAPSED_AND_WHITESPACE 错误
【发布时间】:2012-07-05 23:17:53
【问题描述】:

我正在用 php 创建一个应用程序 我在这一行遇到错误#

$sql="INSERT INTO table ( `rollnum`,'sessionid', `class`, `subject`, `theory`, `practical`, `type_of`, `term`) VALUES ('$students['rollnum']', '$session','$class','$subject['id']','$_REQUEST[$subject['id'].'_'.$students['rollnum'].'_th']','$_REQUEST[$subject['id'].'_'.$students['rollnum'].'_pr']', '$examtype', '$examsubtype')";

我不知道这条线有什么问题。 我什至检查了online platform。他们也说#(上)行有错误。 任何可以帮助我的人 谢谢

【问题讨论】:

  • 你能粘贴几行代码吗?可能是您在前一行打开了一个引用,而这一行中的引用正在关闭它,从而混淆了解析器。
  • $_REQUEST 语法有点奇怪?
  • SQL Injection 等待发生。
  • 请不要将mysql_* 函数用于新代码。它们不再被维护,社区已经开始deprecation process。看到red box?相反,您应该了解prepared statements 并使用PDOMySQLi。如果您不能决定,this article 将帮助您选择。如果你想学习,here is a good PDO tutorial
  • 接受的帖子如何回答这个问题?它建议使用与问题完全无关的 IDE,并且无法确定代码中的实际错误是什么。

标签: php whitespace


【解决方案1】:

您可能需要用大括号将变量括在双引号字符串中。

$string = "I want to use {$variable1} and {$variable['thisKey']}";

或者下面的,跑起来快一点;

$string = 'I want to use'.$variable1.'and '.$variable['thisKey'];

所以这应该可以解决您的直接问题,但是您的查询对注入非常开放,这可能非常糟糕,特别是如果您在查询字符串中使用$_REQUEST 时。我建议您在运行查询语句之前先准备好查询语句,并确保所有危险的东西都被转义。

我回答了另一个问题,其中包括对 here with this answer 进行查询的安全方式

【讨论】:

    【解决方案2】:

    如果您的 PHP 环境有这些设置:display_errors = On; error_reporting = E_ALL,您会看到:

    PHP Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)

    就像这个例子:

    $array['value'] = 'test';
    echo "$array['value']";
    

    应该是这样的:

    echo "".$array['value']."";
    echo "{$array['value']}";
    

    无论如何,你可以使用数字键:

    $array[0] = 'test';
    echo "$array[0]";
    

    【讨论】:

      【解决方案3】:

      您需要在字符串中用大括号将变量名括起来:

      $sql="INSERT INTO stdexamrecord ( `rollnum`,'sessionid', `class`, `subject`, `theory`, `practical`, `type_of`, `term`) VALUES (
      '${students['rollnum']}', 
      '$session',
      '$class',
      '${subject['id']}',
      ' " . $_REQUEST[ $subject['id'] . '_' . $students['rollnum'] . '_th'] . "',
      ' " . $_REQUEST[ $subject['id'] . '_' . $students['rollnum'] . '_pr'] . "',
      '$examtype', 
      '$examsubtype')";
      

      【讨论】:

        【解决方案4】:

        尝试使用 Dreamweaver 或 net bean,这可能会帮助您在正确的位置找到错误

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-10-21
          • 2017-06-20
          • 2014-11-08
          • 2017-12-29
          • 1970-01-01
          • 2016-04-10
          • 1970-01-01
          相关资源
          最近更新 更多