2017-05-20 45 views
0

我是新来的PHP,想问我如何访问另一个页面上的变量。 我的代码:如何访问另一个页面上的mysql变量值

<?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”];不管用。

+0

你的第一个文件的名字是什么?你可以在第二个文件中输入'include“firstfile.php”;'。 – Hienz

+0

包括其他页面中的第一个文件。 –

+0

使用'select',而不是'update'。你也开放SQL注入,并应该哈希密码。 – chris85

回答

0

1.使用会话:

Fisrt.php

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

用法在另一页

session_start(); 
echo $_SESSION['username']; //Output: Max 
  • 包括
  • 可以,但在你上的变量包括将覆盖该变量在你的第一个:

    myfirstfile.php

    $name = 'max'; 
    

    mysecondfile.php

    $city = 'xyz'; 
    

    test.php的

    include 'myfirstfile.php'; 
    echo $name; 
    
    include 'mysecondfile.php'; 
    echo $city; 
    

    输出:

    最大

    XYZ

    +0

    我还没有用其他变量覆盖它 –

    +0

    在哪里被覆盖...? –

    +1

    即使被覆盖它仍然可以访问 – julekgwa

    0

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

    global $susername; 
    

    或随着会议

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

    另一页:

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

    两者都不起作用 –

    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 
    
    0

    尝试......

    connects1010101.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"; 
    ?> 
    
    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脚本使用。感谢所有回答的guyz。

    相关问题