2012-03-27 134 views
1

我收到此错误:注意:使用未定义的常量

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] . ")" 

这是最后一行那给我悲愤! 任何帮助将是伟大的!

+2

使用'$ _SESSION [USER_ID]'应该是'$ _SESSION [ 'user_ID的']'。解析器将检查'[]'中的值是否是一个定义的常量(如果它未包含在引号中)。这就是为什么它提出了一个通知。在编写生产应用程序之前,请阅读一本书或遵循一些权威性教程。你也必须注意SQL注入。 – Shef 2012-03-27 21:58:52

+0

以下是手册条目:[为什么*'$ foo [bar]'*错误?](http://www.php.net/manual/en/language.types.array.php#language.types.array。 don) – cmbuckley 2012-03-27 22:14:32

回答

0

应该

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

注意到周围的元素“USER_ID”

你也应该把周围的第二个呼叫引号的VAR查询报价

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的,而不是更有道理:)

+1

你是如何得出结论:user_id应该是变量而不是字符串/键? :) – Shef 2012-03-27 22:02:41

+0

是的,我写的答案太快了,它绝对是一个字符串:) – 2012-03-27 22:05:03

0

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

$_SESSION['user_id'] 

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

1

编辑错误注意:未定义的不断

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'] . ")"; 
}