无法从其他php文件中获取变量

时间:2015-09-04 16:13:36

标签: php

这是我在index.php中的代码

include_once "connect.php";

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$userName = $_POST['username'];
$password = $_POST['pwd1'];
$userName = stripslashes($userName);
$password = stripslashes($password);
$email = $_POST['email'];

if (isset($_POST['Murad'])) {
  if ($firstname =="" || $lastname =="" || $username =="" || $password =="" || $email ==""){
    include "all.php";
    echo "";
    exit();
  }
 if (strlen($userName) < 3 || strlen($userName) > 16) {
       echo '<strong id="KIARTLAFF">3 - 16 characters please</strong>';
       exit();
} header("Location: main.php?/$firstname/$lastname/");}
?>

connect.php

<?
$link = mysqli_connect('localhost', 'root', '123');
if (!$link) {
    die('Could not connect: ' . mysqli_error());
}
if(isset($_POST['Murad'])){
$db_selected = mysqli_select_db($link,'websiteusers' );
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$userName = $_POST['username'];
$password = $_POST['pwd1'];
$email = $_POST['email'];

$sqlb = "INSERT INTO websiteusers (fullname,lastname,userName,pass,email)
VALUES ('$firstname', '$lastname', '$userName', '$password', '$email')";  
if(mysqli_query($link,$sqlb)){

}else {
    echo mysqli_error($link);
}
 mysqli_close($link);
}
?>

和main.php

<?php 
include_once "connect.php";
?>
<title ><?php echo $firstname." ".$lastname;?></title>

我想从index.php获取变量到main.php。 我找不到任何方法可以帮助我在interenet有很多包括我试过但他们没有工作

1 个答案:

答案 0 :(得分:2)

这里有几个问题:

  1. if有条件的情况下,if (..$username == ""..) $username未声明$userName,而是header(),因此header("Location: main.php?/$firstname/$lastname/");从未到达< / p>

  2. 您使用的是:$_GET这将正确地重定向到该网址,但是您不会做任何事情,因为您可能正在考虑main.php?firstname=$firstname?lastname=$lastname,但该网址不是正确的网址。你需要像$fname = $_GET['firstname']这样的东西。作为main.php的回报,你需要检索那些获取参数,如someother.php

  3. <强>更新

    如果您真的想要取消整个重定向的想法,那么您需要稍微更改一下代码。

    假设这是流程:

    1. 用户从main.php
    2. 提交表单
    3. 表单向header()
    4. 发送POST请求

      然后您需要执行以下操作:

      1. 删除位于index.php
      2. main.php的行
      3. <?php include "index.php"; ?> <html> <head> <title><?php echo $firstname; ?></title> </head> <body> <div><?php echo $firstname; ?></div> </body> </html> 应该是这样的:

        @interface TripV1 : NSObject
        
        @property (nonatomic) int detail;