2011-11-20 45 views
0

我想在2個PHP頁面中使用2個變量的內容,但是我無法使它工作。php/mysql變量

$connect = mysql_connect('localhost', 'username', 'password'); 

if(!$connect) 

{ 
    die('Could not connect to the database: ' . mysql_error()); 
} 

mysql_select_db("database", $connect); 


$id = $lastid; // in the page I created this variable i have: $lastid = mysql_insert_id() 

$code = $random; // in the page I created this variable i have: $random = rand(123456789,98765432); 

if($id&&$code) 
{ 
    $check= mysql_query('SELECT * FROM members WHERE id= "$id" AND random = "$code"'); 
    $checknum = mysql_num_rows('$check'); 

    if($checknum == '1') // run query to activate the account 
    { 
     $acti= mysql_query('UPDATE members SET activation = "1" WHERE id= "$id"'); 

     die('Your account has been activated. You may now log in!'); 

    }else{ 

     die('Invalid id or activation code.'); 
    } 

}else{ 

    die('Could not either find $id or $code!'); 
} 

?> 

我會被罰款,如果我能我的新網頁上使用mysql_insert_id(),但mysql_insert_id()如果我不改變數據庫無法正常工作。

+0

你究竟在哪裏設置了'$ lastid'和'$ random'? – JJJ

+0

究竟是不是工作?你有什麼錯誤嗎? – knittl

+1

你在找[Sessions](http://php.net/manual/en/book.session.php)嗎? – str

回答

3

您需要使用$_SESSION存儲來使它們在兩頁中保持不變。 Read the PHP manual on session storage.

// The first page... 
session_start(); 
// Store variables in `$_SESSION` 
$_SESSION['id'] = mysql_insert_id(); 
$_SESSION['code'] = rand(123456789,98765432); 


// The next page: 
session_start(); 
// Retrieve $id and $code from $_SESSION 
$id = $_SESSION['id']; 
$code = $_SESSION['code']; 

// etc... the rest of your code... 
if($id&&$code) 
{ 
    $check= mysql_query('SELECT * FROM members WHERE id= "$id" AND random = "$code"'); 
    $checknum = mysql_num_rows('$check'); 

    if($checknum == '1') // run query to activate the account 
    { 
     $acti= mysql_query('UPDATE members SET activation = "1" WHERE id= "$id"'); 
     die('Your account has been activated. You may now log in!'); 
    }else{ 
     die('Invalid id or activation code.'); 
    } 
}else{ 
    die('Could not either find $id or $code!'); 
} 

總之,呼籲session_start()在上,你需要訪問會話變量任何腳本的頂部。然後將值存儲到$_SESSION超全局數組中或訪問先前存儲的值。

1

如果你有兩個單獨的頁面(可以說page1.php中)包含[作爲示例】:

<?php 
$lastid = mysql_insert_id(); 
$random = rand(123456789,98765432); //you should really use mt_rand() with a proper number 
?> 

你想傳遞給使page2.php的值,但要當值將丟失使page2.php 您需要將它們存儲在一個會話像這樣:

<?php 
session_start(); 
$_SESSION['lastid'] = mysql_insert_id(); 
$_SESSION['random'] = rand(123456789,98765432); 
?> 

然後你就可以像訪問它們左右的時間內使page2.php

<?php 
session_start(); 
$connect = mysql_connect('localhost', 'username', 'password')or die('Could not connect to the database: '.mysql_error()); 
mysql_select_db("database", $connect); 

$id = $_SESSION['lastid']; 
$code = $_SESSION['random']; 

if(isset($id)&&isset($code)){ 
    $check= mysql_query(' 
    SELECT * 
    FROM members 
    WHERE id="'.mysql_real_escape_string($id).'" AND random="'.mysql_real_escape_string($code).'"'); 


    if(mysql_num_rows($check) == '1') // run query to activate the account 
    { 
     mysql_query('UPDATE members SET activation = "1" WHERE id= "'.mysql_real_escape_string($id).'"'); 
     //Really you should use the header to redirect to the login page, or set user as logged in 
     die('Your account has been activated. You may now log in!'); 
    }else{ 
     //Really you should use the header to redirect to a warning 
     die('Invalid id or activation code.'); 
    } 

}else{ 
    //Really you should not tell the user about codes or ids and just redirect them to home 
    die('Could not either find $id or $code!'); 
} 

?> 

另請注意語法更改。