未定义的索引错误,我该如何解决?

时间:2014-09-25 04:11:30

标签: indexing

这是我的完整编码,

<?php

如果(isset($ _ POST [&#39;提交&#39;])) {

$ username = $ _POST [&#39;用户名&#39;]; $ password = $ _POST [&#39;密码&#39;];

if($username && $password){
            $insert = mysql_query("INSERT INTO users VALUES('', '$username', '$password')");

            $msg = "User is created successfully";
    }
    else{
            $error = "Please fillup all required fields!";
    }

} ?&GT;

<form class="form-horizontal" action="settings_user.php" method="POST">

                            <div class="form-group"><label class="col-lg-2 control-label">User Name</label>

                                <div class="col-lg-4"><input type="text" name="username" placeholder="" class="form-control" autofocus> 
                                </div>
                            </div>
                            <div class="form-group"><label class="col-lg-2 control-label">Password</label>

                                <div class="col-lg-4"><input type="password" name="password" placeholder="" class="form-control"></div>
                            </div>
                            <div class="form-group">
                                <div class="col-lg-offset-2 col-lg-10">
                                    <button class="btn btn-sm btn-primary" name="submit" type="submit">ADD</button>
                                </div>
                            </div>
                            <?php echo $msg; ?>
                            <?php echo $error; ?>

                    </form>

出现两条错误消息:$ msg和$ error

的未定义索引

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

这可能有用。而不是使用2个变量来实现&#34;成功&#34;和&#34;失败&#34;,使用单个变量并相应地放置消息。全局初始化变量。

<?php
 $msg="";
 if(isset($_POST['submit'])) {


 $username = $_POST['username']; $password = $_POST['password'];
 if($username && $password){
 $insert = mysql_query("INSERT INTO users VALUES('', '$username', '$password')");

 $msg = "User is created successfully";
 }
 else{
 $msg = "Please fillup all required fields!";
 }
 } 

 ?>

 <html>
 <body>
 <form class="form-horizontal" action="settings_user.php" method="POST">
 <div class="form-group"><label class="col-lg-2 control-label">User Name</label>
 <div class="col-lg-4"><input type="text" name="username" placeholder="" class="form-control"         

 autofocus> 
 </div>
 </div>
 <div class="form-group"><label class="col-lg-2 control-label">Password</label>
 <div class="col-lg-4"><input type="password" name="password" placeholder="" class="form-  

 control"></div>
 </div>
 <div class="form-group">
 <div class="col-lg-offset-2 col-lg-10">
 <button class="btn btn-sm btn-primary" name="submit" type="submit">ADD</button>
 </div>
 </div>

 <?php echo $msg; ?>
 </form>
 </body>
 </html>