doushen2154 2015-09-04 16:13
浏览 36

无法从另一个php文件中获取变量

Here is my code in 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);
}
?>

And main.php

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

I want to get variables from index.php to main.php. I could not find any ways that could help me in interenet there were many includes i tried them but they did not work

  • 写回答

1条回答 默认 最新

  • duandai7601 2015-09-04 16:29
    关注

    A couple of problems here:

    1. In your if conditional you have if (..$username == ""..) however $username is not declared, instead you have $userName, thus the header() never reached

    2. You are using this: header("Location: main.php?/$firstname/$lastname/"); This will redirect to that url correctly, but that wont do anything as you are probably thinking of $_GET , however that url is not a proper get url. you would need something like main.php?firstname=$firstname?lastname=$lastname. Which in return in main.php you would need to retrieve those Get parameters like $fname = $_GET['firstname']


    UPDATE:

    If you really want to do away with the whole redirect idea then you need to change your code a bit.

    Assumming that this is the flow:

    1. User submits a form from someother.php
    2. form sends POST request to main.php

    Then you would need to do the following:

    1. remove the line where header() that resides in index.php
    2. main.php should be something like this:

      <?php
      include "index.php";
      ?>
      <html>
          <head>
              <title><?php echo $firstname; ?></title>
          </head>
          <body>
              <div><?php echo $firstname; ?></div>
          </body>
      </html>
      
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大