Notice : Undefined variable when concatenating

风格不统一 提交于 2019-12-29 09:38:06

问题


I have been making a script to display users and make changes to their admin privileges.

Here is the code:

    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>';
    }   

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.

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?


回答1:


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>';
    // ...


来源:https://stackoverflow.com/questions/10605842/notice-undefined-variable-when-concatenating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!