我想复制$ friendname = $ _ POST ['friendname'];如果if语句有效并在其中回显,则返回下一页的值

时间:2019-05-26 07:14:30

标签: php mysql session

我想将朋友名从此页面复制到下一页并在其中回显。 问题是它是showinh undefined vairable。 目标是将相同的朋友名值回显到下一页,即form2.php并在那里回显

<?php

    include("config.php");
    session_start();
    $user=$_POST['username'];//username
    $friendname=$_POST['friendname'];//friendname
    $years=$_POST['years'];
    $nickname=$_POST['nickname'];
    $place=$_POST['place'];//username

    $sql = "INSERT INTO data (username,friendname,years,nickname,place)
                VALUES ('$user', '$friendname', '$years','$nickname','$place')";




    if ($con->query($sql) === TRUE) {
        $_POST['friendname']=$frnd;//username
        header('location: ../form2.php');
    } else {
        echo "Error: " . $sql . "<br>" . $con->error;
    }
    $con->close();
?> 

这是我尝试但无法正常工作的方法 insert.php

<?php


    include("config.php");
    session_start();
    $user=$_POST['username'];//username
    $friendname=$_POST['friendname'];//friendname
    $years=$_POST['years']
    $nickname=$_POST['nickname'];
    $place=$_POST['place'];

    $sql = "INSERT INTO data (username,friendname,years,nickname,place)
            VALUES ('$user', '$friendname', '$years','$nickname','$place')";

    if ($con->query($sql) === TRUE) {
        $_POST['friendname']=$frnd;//username
        header('location: ../form2.php');
    } else {
        echo "Error: " . $sql . "<br>" . $con->error;
    }
    $con->close();
?>

form2.php

<?php
    session_start();
    if (!isset($_SESSION['username'])) {

    }
    if (isset($_GET['logout'])) {

    }
?>




<html>
    <body>
        <p><?php echo $_SESSION['username']; ?></p>
        <h4>Enter some details for your friend <?php echo $frnd;?></h4>
        <form>
        </form>
    </body>
</html>

注意:行号上的未定义索引变量frnd

2 个答案:

答案 0 :(得分:0)

file2.php

<?php
session_start();
$frnd = $_SESSION['friendname'];

?>

<html>
    <body>
        <p><?php echo $_SESSION['username']; ?></p>
        <h4>Enter some details for your friend <?php echo $frnd;?></h4>
        <form>
        </form>
    </body>
</html>

答案 1 :(得分:0)

上述代码中的sql可以广泛用于sql注入-您应该考虑使用prepared statements,因为一旦掌握,它将使工作变得更加简单,代码也更加安全。

以下内容未经测试,但其工作原理如下:

对POST请求数据进行过滤-尽管使用IMO预处理语句并不是严格要求的,但这总是一个好主意。

该sql是prepared用于执行,该sql中的?占位符已绑定到变量。

<?php

    session_start();
    include 'config.php';

    $user=filter_input( INPUT_POST, 'username', FILTER_SANITIZE_STRING );
    $name=filter_input( INPUT_POST, 'friendname', FILTER_SANITIZE_STRING );
    $years=filter_input( INPUT_POST, 'years', FILTER_SANITIZE_NUMBER_INT );
    $nick=filter_input( INPUT_POST, 'nickname', FILTER_SANITIZE_STRING );
    $place=filter_input( INPUT_POST, 'place', FILTER_SANITIZE_STRING );

    $sql='insert into `data` (`username`,`friendname`,`years`,`nickname`,`place`) values (?,?,?,?,?)';
    $stmt=$con->prepare( $sql );

    if( !$stmt )exit('Failed to prepare SQL query');
    $stmt->bind_param('ssiss',$user,$name,$years,$nick,$place);

    $res=$stmt->execute();

    if( $res && $con->affected_rows==1 ){
        $_SESSION['friendname']=$name;
        $_SESSION['username']=$user;
        exit( header('Location: ../form2.php') );
    } else {
        exit('Error');
    }
?>

然后可以像这样修改您的HTML,以将会话变量分配给变量-同样,严格来说也不必这样做

<?php
    session_start();
    $friend = $_SESSION['friendname'];
?>

<html>
    <body>
        <form>
            <p><?php echo $_SESSION['username']; ?></p>
            <h4>Enter some details for your friend <?php echo $friend;?></h4>       
        </form>
    </body>
</html>