注意:串联时未定义的变量 [英] Notice : Undefined variable when concatenating
问题描述
我一直在制作脚本来显示用户并更改其管理员权限.
I have been making a script to display users and make changes to their admin privileges.
这是代码:
while ($row= mysql_fetch_assoc($query)) {
$uname= $row['username'];
$fname= $row['first_name'];
$lname= $row['last_name'];
$email= $row['email'];
$admin= $row['admin'];
$insert .= '<tr>
<td>' .$uname. '</td>
<td>' .$fname. '</td>
<td>' .$lname. '</td>
<td>' .((isset($email)) ? $email:'No email set.'). '</td>
<td>'.(($admin == 'y') ? 'Admin':'User').'</td>
<td><input type="checkbox" name="' .$uname. '" value="'.(($admin == 'y')?'n':'y').'"/>'.(($admin == 'y')?'Make a user':'Make an admin user').'</tr>';
}
然后在html中稍后使用$ insert变量生成带有用户数据的所有表行.
The $insert variable is then used later in the html to generate all the table rows with the user data.
问题在于,当我运行脚本时,会显示此消息
The problem is that when I run the script this message appears
Notice: Undefined variable: insert in C:\wamp\www\...\user_edit.php on line 33
尽管该脚本仅在页面中间显示此消息,但脚本仍然可以正常工作.
The script still works correctly though with only this message showing in the middle of the page.
当我拿的时候.从$ insert.='部分消失,消息消失了,但是脚本仅在表中显示一个用户.
When I take the . off from the $insert .= ' part the message disappears but the script only displays one user in the table.
有什么原因可能会发生这种情况吗?而且我的问题有解决方案吗,这样我才能使脚本正常工作并且消息不出现?
Is there any reason why this may be happening? And is there a solution to my problem so I can have the script working and the message not showing up?
推荐答案
在使用变量之前,只需将其设置为空字符串即可.您不能在尚不存在的变量上使用.=
:
Just set the variable to an empty string before you use it. You can't use .=
on a variable that does not exist yet:
$insert = "";
while($row= mysql_fetch_assoc($query)) {
// ...
$insert .= '<tr>';
// ...
这篇关于注意:串联时未定义的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!