【问题标题】:Display a success message after inserting value in database在数据库中插入值后显示成功消息
【发布时间】:2015-01-01 05:11:21
【问题描述】:

我有一个表单可以帮助用户在登录后登录,用户被引导到他们的仪表板,在那里他们可以更新他们的个人资料

<form class="form-horizontal" role="form" action="admin_insert_profile.php" enctype="multipart/form-data" method="post">

    <h4>Profile Details </h4>

        <div class="form-group">
            <label class="col-lg-4 control-label">Name</label>
                <div class="col-lg-6">
                    <input class="form-control" value="<?php echo $user_admin_name ;?>" type="text" name="name" >
                </div>
        </div>

        <div class="form-group">
            <label class="col-lg-4 control-label">Email</label>
                <div class="col-lg-6">
                    <input class="form-control" value="<?php echo $login_session ;?>" type="email" name="email" >
                    <p class="help-block"></p>
                </div>
        </div>

        <div class="form-group">
            <label class="col-lg-4 control-label">Password</label>
                <div class="col-lg-6">
                    <input class="form-control" value="<?php echo $user_admin_password ;?>" type="password" name="password">
                </div>
        </div>

        <div class="form-group">
            <label class="col-md-3 control-label"></label>
                <div class="col-md-8">
                    <input class="btn btn-primary" value="Save Changes" type="submit" name="submit">

                    <span></span>
                </div>  
        </div>
    </form>

admin_insert_profile.php

<?php
$con=mysqli_connect("localhost","root","","db");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security
$name = mysqli_real_escape_string($con, $_POST['name']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$password = mysqli_real_escape_string($con, $_POST['password']);


$sql="UPDATE member SET admin_name = '".$name."', admin_email = '".$email."',admin_password = '".$password."' WHERE id='1' ";
    if (!mysqli_query($con,$sql)) 
        {
            die('Error: ' . mysqli_error($con));
        }
    header("Location: admin_login.php");
    exit;
mysqli_close($con);
?>

成功更新他们的个人资料后,他们将注销并重定向到登录页面,在此登录页面上,我希望显示一条消息,说明密码已更新,用户需要再次登录。 我有什么办法可以这样做

【问题讨论】:

  • 设置参数并设置它的值....参考这个http://technet.microsoft.com/en-us/library/cc626303%28v=sql.105%29.aspx
  • 一个简单的解决方案就是在您的标头重定向中进行查询:header("Location: admin_login.php?update=true");
  • 如果您使用的是 php,请使用会话变量,或者在重定向 url 中传递消息
  • @Rasclatt 页面被重定向到 admin_login.php 但消息将如何显示?
  • @amit 你能告诉我如何使用会话存储消息

标签: javascript php sql-server ajax mysqli


【解决方案1】:

是的,您可以借助 $_server$_session 等全局变量来执行此操作。如果成功更新设置$_SERVER['flag']='set'; 并在登录页面检查$_SERVER['flag'] 变量。

if($_SERVER['flag']=='set')
{
    //passwod changed
}

【讨论】:

    【解决方案2】:

    是的!.. 您可以将以下代码放在:

    admin_insert_profile.php页面

    if (!mysqli_query($con,$sql)) 
    {
        die('Error: ' . mysqli_error($con));
    }
    $_SESSION['message']="password has been updated";
    header("Location: admin_login.php");
    exit;
    

    并在您的login.php 页面中为&lt;form&gt; 标签添加代码:

    <?PHP
        if(isset($_SESSION['message'])){
            echo $_SESSION['message'];
            unset($_SESSION['message']);
        }
    ?>
    

    如果你不使用session_start(),那么在两个页面中都使用。

    【讨论】:

    • 我已经在使用 session() 进行登录-注销过程,所以在 admin_insert_profile.php 页面中,我在开头包含了 admin_session 页面,按照我在 login.php 中使用 session_start() 的方式页面,但消息没有显示(我认为当用户被重定向到 admin_login.php 页面时,他基本上被注销了,尽管我可能错了)你能告诉我为什么没有显示味精
    • 在您的注销页面中,您只能销毁可用于登录用户身份验证的会话变量。不要破坏所有会话。在那种情况下,$_SESSION['message'] 也被破坏了。但不要破坏它。
    【解决方案3】:

    一种方法是这样的,更新你的admin_insert_profile.php如下

    ....
    header("Location: admin_login.php?ok=1");
    mysqli_close($con); // Move it to here, its never get called 
    exit;
    

    在 admin_login.php 中

    //Check for success flag in get query param
    $ok = isset($_GET['ok']);
    
    //Insert this code where you wanted to show the msg
    if($ok)  {
         echo '<div class="alert alert-success" role="alert">
                 Password has been updated and the user needs to login again
               </div>';
    }
    

    这里是我用来做flash messaging使用会话的这些漂亮的功能,供您参考

    function addMsg($msg, $type = 'danger') {
        $_SESSION['msg'] = $type .'|'. $msg;
    }
    
    function showMsg($class = 'msg') {
       $msg = @$_SESSION['msg'];
       if(isset($msg) && !empty($msg)) {
        $msgPart = explode('|', $msg);
        $msgText = ($msgPart[1]);
        $msgType = $msgPart[0];
    
        if( !empty($msgText) ) {
            //Remove Flash message
            unset( $_SESSION['msg'] );
            return sprintf('
                <div id="flashMsg" class="alert alert-%s">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                    %s
                </div>
                ', $msgType, $msgText);
          }
       }
     }
    

    如果只是执行以下操作,则使用

    //Start the session at top 
    start_session();
    
    addMsg('Password has been updated and the user needs to login again', 'success');
    
    header("Location: admin_login.php");
    mysqli_close($con); // Move it to here, its never get called 
    exit;
    

    在 admin_login.php 中

    //Start the session at top 
    start_session();
    
    //put where you wanted to show the flash
    echo showMsg();
    

    【讨论】:

    • 我也以正常形式收到此消息,即 www.xyz.com/admin_login.php 而我希望该消息应在特定条件下显示,我面临的另一个问题是我收到的消息是:密码已更新,用户需要再次登录;" } ?> ,虽然没有语法错误
    • 如果你想在特定条件下显示味精,你应该使用if(condition)并显示味精,第二部分显示你当前使用的更新代码
    猜你喜欢
    • 1970-01-01
    • 2019-09-27
    • 1970-01-01
    • 2020-08-10
    • 2011-12-25
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多