簡體   English   中英

未定義索引錯誤,我該如何解決?

[英]Undefined index error, how can I solve this?

這是我的完整編碼,

<?php

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{
            $error = "Please fillup all required fields!";
    }

}?>

<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的未定義索引

我怎樣才能解決這個問題?

這可能有效。 不要使用2個變量來表示“成功”和“失敗”,而應使用單個變量並相應地放置消息。 全局初始化變量。

<?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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM