在数据库中插入值后显示成功消息

时间:2014-11-05 04:52:52

标签: javascript php sql-server ajax mysqli

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

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

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

3 个答案:

答案 0 :(得分:4)

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

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页面中,将代码改为<form>标记:

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

如果您不使用session_start(),请在两个页面中使用。

答案 1 :(得分:1)

是的,你可以借助全球变量$_server$_session来做到这一点。如果成功更新集$_SERVER['flag']='set';,则在登录页面上检查$_SERVER['flag']变量。

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

答案 2 :(得分:1)

执行此操作的一种方法是,更新您的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();