【问题标题】:How can i check user input in db and close connection then echo the input on the next page如何检查数据库中的用户输入并关闭连接,然后在下一页上回显输入
【发布时间】:2016-08-15 16:37:57
【问题描述】:

我在 test1.php 中有输入表单,我想检查数据库中的用户输入,如果用户输入在数据库中不存在,则继续到 test2.php 并在 test2.php 中回显用户输入。否则,重定向回 test1.php 并回显,不可用。

现在,如果输入存在于 sql 中,我可以重定向回上一页,但关闭连接后我无法在 test2.php 中回显用户输入。

这里是 test1.php

<!doctype HTML>
<html>
<head>
</head>
<?php
if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 ) {
    echo '<ul class="err">';
    foreach($_SESSION['ERRMSG_ARR'] as $msg) {
        echo '<li>',$msg,'</li>'; 
    }
    echo '</ul>';
    unset($_SESSION['ERRMSG_ARR']);
}
?>
<form id="loginForm" name="loginForm" method="post" action="test-exec.php">
<table width="300" border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<th>Email </th>
<td><input name="box" type="text" class="textfield" id="box" /></td>
</tr>
<td><input type="submit" name="Submit" value="Check" /></td>
</tr>
</table>
</form>

这里是 test-exec.php

<?php
session_start();

require_once('db/config.php');

$errmsg_arr = array();

$errflag = false;

    $con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
if(!$con) {
    die('Failed to connect to server: ' . mysqli_connect_error());
}

$db = mysqli_select_db($con, DB_DATABASE);
if(!$db) {
    die("Unable to select database");
}

$box = mysqli_real_escape_string($con, $_POST['box']);

if($box != '') {
    $qry = "SELECT * FROM members WHERE box='$box'";
    $result = mysqli_query($con,$qry);
    if($result) {
        if(mysqli_num_rows($result) > 0) {
            $errmsg_arr[] = 'Box already in use';
            $errflag = true;
        }
        @mysql_free_result($result);
    }
    else {
        die("Query failed");
    }
}

if($errflag) {
    $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
    session_write_close();
    header("location: test1.php");
    exit();
}else {
    header("location: test2.php");
    exit();

mysqli_close($con);

}
?>

这里是 test2.php

<!doctype HTML>
<html>
<head>
</head>

<form id="loginForm" name="loginForm" method="post" action="register.php">
<table width="300" border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<th>Email </th>
<td><input name="box" type="text" class="textfield" id="box" value="<?php echo $_POST['box']; ?>" /></td>
</tr>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>

【问题讨论】:

  • 你只是想重定向到另一个 php 页面?也许这对你有帮助:stackoverflow.com/questions/768431/…
  • @Rogier,它们是 2 个 html,1 个 php。其中一个html向php提交表单,php执行输入并在下一页上回显输入。
  • 我已经做到了。还是谢谢。
  • 如果别人遇到同样的问题,你为什么不这样回答你自己的问题,你会帮助他们。
  • @Missy,好的,我会这样做的。

标签: php sql


【解决方案1】:
if($errflag) {
    $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
    session_write_close();
    header("location: test1.php");
    exit();
}else {
    $_SESSION['SESS_box'] = $box;
    session_write_close();
    header("location: test2.php");
    exit();

    mysqli_close($con);
}
?>


then in the html use this:
<?php echo $_SESSION["SESS_box"]; ?> 
to echo the input.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    • 2022-11-02
    • 2015-05-28
    • 2014-02-01
    相关资源
    最近更新 更多