设置php变量并将其显示在另一页面上

时间:2013-09-14 18:05:38

标签: php html mysql

我想运行这个脚本来更新数据库,然后检查它是否确实更新,然后设置一个变量说明并重定向回上一页并回显成功更新或不成功更新页。目前它运行完美,但不会回应成功或不成功的部分。我该怎么做才能解决这个问题?

<?php
include_once('../../../_php/connect.php');

$update_e_id = $_POST['error_id'];

//Update unresolved to resolved
mysql_query("UPDATE error SET resolution='resolved' WHERE id=" . $update_e_id . " 
AND resolution='unresolved'");

//Check if it updated
$checkE_update = "SELECT * FROM error WHERE id=" . $update_e_id . " AND 
 resolution='resolved'";
$results = mysql_query($checkE_update);
$nr = mysql_num_rows($results);

while($row = mysql_fetch_assoc($results)) {
    $checkResolution = $row['resolution'];
}
if($checkResolution = 'resolved') {
    $successfullyUpdated = "<p>The error is now resolved.</p>";
    header("Location: ../lib_errors.php");
}   else {
        $didnotUpdateError = "<p>The update was unsuccessful.</p>";
        header("Location: ../lib_errors.php");
    }
 ?>

4 个答案:

答案 0 :(得分:0)

使用会话...确保在php标签的每个页面的顶部都有这个;

session_start();

然后做

$_SESSION['name'] = $varName;

然后在另一个页面上你可以回显会话

echo $_SESSION['name'];

答案 1 :(得分:0)

其他答案中提到的$_SESSION方式是一种方法。另一个可能是存储有关$_GET变量成功或失败的消息,并将其传递给被调用的URI:

// ...
if($checkResolution = 'resolved') {
    $status = 'success';
    $message = "<p>The error is now resolved.</p>";
} else {
    $status = 'error;
    $message = "<p>The update was unsuccessful.</p>";
}
$uri = '../lib_errors.php?status='.$status.'&message='.$message;
header('Location: '.$uri);

然后在lib_errors.php中,您可以使用:

$status  = $_GET['status'];
$message = $_GET['message'];

答案 2 :(得分:0)

if($checkResolution = 'resolved') {
    $successfullyUpdated = "<p>The error is now resolved.</p>";
    header("Location: ../lib_errors.php?msg=The error is now resolved.");
}   else {
        $didnotUpdateError = "<p>The update was unsuccessful.</p>";
        header("Location: ../lib_errors.php?msg=The update was unsuccessful.?msg=");
    }
 ?>

并在您的重定向页面上回显此消息:

<?php echo $_GET['msg']; ?>

答案 3 :(得分:0)

我不知道你为什么再次执行选择查询。从更新查询本身,您将获得成功或失败

//Update unresolved to resolved
$rs = mysql_query("UPDATE error SET resolution='resolved' WHERE id=".$update_e_id." AND resolution='unresolved'");
if($rs) {
    header("Location: ../lib_errors.php?msg=The error is now resolved");
}
else {
    header("Location: ../lib_errors.php?msg=The update was unsuccessful");
}

在lib_error.php中,您将收到$_GET['msg'];消息,您可以随时随地回复