【问题标题】:Notice: Use of undefined constant注意:未定义常量的使用
【发布时间】:2012-11-18 09:29:54
【问题描述】:

我收到此错误:

Notice: Use of undefined constant user_id - assumed 'user_id'
in C:\xampp\htdocs\euisample\language.php on line 44

我以为我是这样定义的:

$id= " . $_SESSION[user_id] . ";

这是SQL语句;

$sql_insert = "INSERT into `language`
(`native`,`other`,`other_list`,`other_read`, `other_spokint`
,`other_spokprod`,`other_writ`  )
VALUES
('$native','$other','$other_list','$other_read','$other_spokint','$other_spokprod',
'$other_writ')  WHERE id= " . $_SESSION[user_id] . ")"

这是让我悲伤的最后一行! 任何帮助都会很棒!

【问题讨论】:

  • $_SESSION[user_id] 应该是 $_SESSION['user_id']。解析器将检查[] 中的值是否是一个已定义的常量,如果它没有用引号括起来。这就是它发出通知的原因。在编写生产应用程序之前,请阅读一本书或遵循一些权威教程。您也必须注意 SQL 注入。
  • 这是手动输入:Why is $foo[bar] wrong?

标签: php mysql


【解决方案1】:

您在查询的最后一行忘记了 $,就在 user_id 之前。

$sql_insert = "INSERT into `language`
(`native`,`other`,`other_list`,`other_read`, `other_spokint`
,`other_spokprod`,`other_writ`  )
VALUES
('$native','$other','$other_list','$other_read','$other_spokint','$other_spokprod',
'$other_writ')  WHERE id= " . $_SESSION[$user_id] . ")"

编辑: 'user_id' 而不是 $user_id 更有意义:)

【讨论】:

  • 您如何得出user_id 应该是变量而不是字符串/键的结论? :)
  • 是的,我写的太快了,肯定是字符串:)
【解决方案2】:

编辑错误提示:未定义常量的使用

if(isset($_SESSION['user_id']) && isset($native) && isset($other)&&............){
$sql_insert = "INSERT into `language`
(`native`,`other`,`other_list`,`other_read`, `other_spokint`
,`other_spokprod`,`other_writ`  )
VALUES
('$native','$other','$other_list','$other_read','$other_spokint','$other_spokprod',
'$other_writ')  WHERE id= " . $_SESSION['user_id'] . ")";
}

【讨论】:

    【解决方案3】:

    应该是

    $id= " . $_SESSION['user_id'] . ";
    

    注意元素“user_id”周围的引号

    您还应该在查询中对 var 的第二次调用加上引号

    【讨论】:

      【解决方案4】:

      你的语法的问题是你需要引用user_id;

      $_SESSION['user_id']
      

      你会发现下一个问题是SQL语句INSERT INTO xx WHERE yy不存在。您应该删除 WHERE 部分。

      【讨论】:

        猜你喜欢
        • 2018-06-30
        • 1970-01-01
        • 2016-04-02
        • 2021-03-07
        相关资源
        最近更新 更多