我想复制 $friendname=$_POST['friendname'];

我想把这个页面的朋友名复制到下一个页面并在那里回显。问题是它是 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();

?>

表单2.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


慕尼黑5688855
浏览 192回答 2
2回答

Cats萌萌

文件2.php<?phpsession_start();$frnd = $_SESSION['friendname'];?><html>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <p><?php echo $_SESSION['username']; ?></p>&nbsp; &nbsp; &nbsp; &nbsp; <h4>Enter some details for your friend <?php echo $frnd;?></h4>&nbsp; &nbsp; &nbsp; &nbsp; <form>&nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; </body></html>

摇曳的蔷薇

上述代码中的 sql 对 sql 注入是开放的——你应该考虑使用prepared statementsas,一旦掌握,将使生活更简单,你的代码更安全。以下内容未经测试,但基本上按如下方式工作:POST 请求数据被过滤 - 尽管在使用准备好的语句时这不是绝对必要的,但 IMO 总是一个好主意。sqlprepared用于执行,?sql 中的占位符绑定到变量。<?php&nbsp; &nbsp; session_start();&nbsp; &nbsp; include 'config.php';&nbsp; &nbsp; $user=filter_input( INPUT_POST, 'username', FILTER_SANITIZE_STRING );&nbsp; &nbsp; $name=filter_input( INPUT_POST, 'friendname', FILTER_SANITIZE_STRING );&nbsp; &nbsp; $years=filter_input( INPUT_POST, 'years', FILTER_SANITIZE_NUMBER_INT );&nbsp; &nbsp; $nick=filter_input( INPUT_POST, 'nickname', FILTER_SANITIZE_STRING );&nbsp; &nbsp; $place=filter_input( INPUT_POST, 'place', FILTER_SANITIZE_STRING );&nbsp; &nbsp; $sql='insert into `data` (`username`,`friendname`,`years`,`nickname`,`place`) values (?,?,?,?,?)';&nbsp; &nbsp; $stmt=$con->prepare( $sql );&nbsp; &nbsp; if( !$stmt )exit('Failed to prepare SQL query');&nbsp; &nbsp; $stmt->bind_param('ssiss',$user,$name,$years,$nick,$place);&nbsp; &nbsp; $res=$stmt->execute();&nbsp; &nbsp; if( $res && $con->affected_rows==1 ){&nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['friendname']=$name;&nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['username']=$user;&nbsp; &nbsp; &nbsp; &nbsp; exit( header('Location: ../form2.php') );&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; exit('Error');&nbsp; &nbsp; }?>然后你可以像这样修改你的 HTML 以将会话变量分配给一个变量 - 虽然再次不是绝对必要的<?php&nbsp; &nbsp; session_start();&nbsp; &nbsp; $friend = $_SESSION['friendname'];?><html>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <form>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><?php echo $_SESSION['username']; ?></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h4>Enter some details for your friend <?php echo $friend;?></h4>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; </body></html>
打开App,查看更多内容
随时随地看视频慕课网APP