如何在另一个页面上访问mysql变量值

时间:2017-05-20 18:57:15

标签: php mysql

我是php新手,想知道如何在另一个页面上访问变量。 我的代码:

<?php
    $username = "ADMIN";
    $host = "localhost";
    $password = "ADMIN";
    $database = "USER";
    $sname =  $_POST['signupname'];
    $susername =  $_POST['susername'];
    $spassword =  $_POST['spassword'];
    $gender =  $_POST['gender'];
    $con = mysqli_connect($host, $username, $password, $database);
    if(isset($_POST["signup"])) {
    mysqli_query($con, "INSERT INTO `USER`.`INFO` (`UID`, `NAME`, `USERNAME`, `PASSWORD`, `GENDER`, `BDAY`, `BMONTH`, `BYEAR`, `STATUS`, `LAST-ACTIVE`) VALUES (NULL, '$sname', '$susername', '$spassword', '$gender', '', '', '', '', NULL);");
    echo "<script type='text/javascript'>window.location.replace('../SSTEPS1010101/birthdaycheck1010101.php');</script>";
    }
?>

和其他页面代码: -

<?php
    require '../SIGNUP/connects1010101.php';
    $bday = $_POST["bday"];
    $bmonth = $_POST["bmonth"];
    $byear = $_POST["byear"];
    if (isset($_POST["snext"])) {
    mysqli_query($con, "UPDATE `USER`.`INFO` SET `BDAY` = '$bday', `BMONTH` = '$bmonth', `BYEAR` = '$byear' WHERE `info`.`USERNAME` = '$susername;'");
    }
    echo "$susername";
?>

如何在其他页面上访问$susername? 我正在使用$ _SESSIONS [“susername”];并在第一页上回显它并且它正在工作但在另一页上$ _SESSIONS [“susername”];没有用。

5 个答案:

答案 0 :(得分:0)

  

1.使用会话:

<强> Fisrt.php

session_start();
 $username = "Max";
$username=$_SESSION['username'];
echo $username; //Output: Max

在另一页中使用

session_start();
echo $_SESSION['username']; //Output: Max
  
      
  1. 包括
  2.   

你可以,但你最后一个include中的变量将覆盖你第一个变量:

<强> myfirstfile.php

$name = 'max';

<强> mysecondfile.php

$city = 'xyz';

<强> test.php的

include 'myfirstfile.php';
echo $name;

include 'mysecondfile.php';
echo $city;

<强>输出:

  

最大

     

XYZ

答案 1 :(得分:0)

您可以使用全局关键字或会话

global $susername;

或使用会话

session_start();
$username = "bla bla bla";
$_SESSION['username']=$username;

另一页:

session_start();
$username=$_SESSION['username'];
echo $username; 

答案 2 :(得分:0)

您使用require*include*函数包含文件,当您包含文件时,您需要指定文件的正确路径。

将您的PHP设置为显示错误

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

如何包含文件

// including a file which is in the same directory
require_once ('somefile.php');

 // include a file that is one directories back
require_once ('../somefile.php');
require_once ('../directory/somefile.php');

 // include a file that is two directories back
require_once ('../../somefile.php');
require_once ('../../directory/somefile.php');

// etc

答案 3 :(得分:0)

  

试试这个......

连接1010101.php文件

<?php
//connects1010101.php File


    $username = "ADMIN";
    $host = "localhost";
    $password = "ADMIN";
    $database = "USER";

    $sname =  $_POST['signupname'];
    $susername =  $_POST['susername'];
    $spassword =  $_POST['spassword'];
    $gender =  $_POST['gender'];

    $con = mysqli_connect($host, $username, $password, $database);

    if(isset($_POST["signup"])) {

    mysqli_query($con, "INSERT INTO `USER`.`INFO` (`UID`, `NAME`, `USERNAME`, `PASSWORD`, `GENDER`, `BDAY`, `BMONTH`, `BYEAR`, `STATUS`, `LAST-ACTIVE`) VALUES (NULL, '$sname', '$susername', '$spassword', '$gender', '', '', '', '', NULL)");

    echo "<script type='text/javascript'>window.location.replace('../SSTEPS1010101/birthdaycheck1010101.php');</script>";
    }
?>

<强> AnotherFile.php

<?php

    include  '../SIGNUP/connects1010101.php';  //connects1010101.php include 
    global $susername;

    $bday = $_POST["bday"];
    $bmonth = $_POST["bmonth"];
    $byear = $_POST["byear"];

    if (isset($_POST["snext"])) {

    mysqli_query($con, "UPDATE `USER`.`INFO` SET `BDAY` = '$bday', `BMONTH` = '$bmonth', `BYEAR` = '$byear' WHERE `USERNAME` = '$susername' ");
    }

    echo "$susername";
?>

答案 4 :(得分:0)

谢谢大家。主要问题是重定向。 我的旧代码: -

<?php
    $username = "ADMIN";
    $host = "localhost";
    $password = "ADMIN";
    $database = "USER";
    $sname =  $_POST['signupname'];
    $susername =  $_POST['susername'];
    $spassword =  $_POST['spassword'];
    $gender =  $_POST['gender'];
    $con = mysqli_connect($host, $username, $password, $database);
    if(isset($_POST["signup"])) {
    mysqli_query($con, "INSERT INTO `USER`.`INFO` (`UID`, `NAME`, `USERNAME`, `PASSWORD`, `GENDER`, `BDAY`, `BMONTH`, `BYEAR`, `STATUS`, `LAST-ACTIVE`) VALUES (NULL, '$sname', '$susername', '$spassword', '$gender', '', '', '', '', NULL);");
    echo "<script type='text/javascript'>window.location.replace('../SSTEPS1010101/birthdaycheck1010101.php');</script>";
    }
?>

其他页面: -

<?php
    require '../SIGNUP/connects1010101.php';
    $bday = $_POST["bday"];
    $bmonth = $_POST["bmonth"];
    $byear = $_POST["byear"];
    if (isset($_POST["snext"])) {
    mysqli_query($con, "UPDATE `USER`.`INFO` SET `BDAY` = '$bday', `BMONTH` = '$bmonth', `BYEAR` = '$byear' WHERE `info`.`USERNAME` = '$susername;'");
    }
    echo "$susername";
?>

我的新首页代码: -

<?php
session_start();
$username = "ADMIN";
$host = "localhost";
$password = "chmuhammadsohaib123";
$database = "USER";
$sname =  $_POST['signupname'];
$susername =  $_POST['susername'];
$spassword =  $_POST['spassword'];
$gender =  $_POST['gender'];
$con = mysqli_connect($host, $username, $password, $database);
if(isset($_POST["signup"])) {
mysqli_query($con, "INSERT INTO `USER`.`INFO` (`UID`, `NAME`, `USERNAME`, `PASSWORD`, `GENDER`, `BDAY`, `BMONTH`, `BYEAR`, `STATUS`, `LAST-ACTIVE`) VALUES (NULL, '$sname', '$susername', '$spassword', '$gender', '', '', '', '', NULL);");
$snusername = $_SESSION["susername"] = $susername;
}
if (isset($_POST["signup"])) {
    echo "<script type='text/javascript'>window.location.replace('../SSTEPS1010101/birthdaycheck1010101.php');</script>";
}?>

其他页面: -

<?php
session_start();
require_once ('../SIGNUP/connects1010101.php');
$bday = $_POST["bday"];
$bmonth = $_POST["bmonth"];
$byear = $_POST["byear"];
$snusername = $_SESSION["susername"];
if (isset($_POST["snext"])) {
mysqli_query($con, "UPDATE `USER`.`INFO` SET `BDAY` = '$bday', `BMONTH` = '$bmonth', `BYEAR` = '$byear' WHERE `info`.`USERNAME` = '$snusername;'");
}?>

主要问题出在我使用的JS脚本中。感谢所有回答的家伙。