2012-05-02 26 views
0

我有一個php文件,說p1.php從另一個php文件獲取數據,說p2.php,它通過$ _GET在p1.php中訪問。現在$ _GET中的數據被保存在一個變量$ totPrice中。 p1.php也有一個引用自身的表單,一些處理是用MySql數據庫完成的。我得到的錯誤:在PHP中使用GET和POST未定義的索引

"Notice: Undefined index: totP in C:\xampp\htdocs\fi\p1.php on line 'where ever $totPrice appears'". 

下面是p1.php代碼: -

<?php 
global $totPrice; 
$totPrice = $_GET['totP']; 
if(!isset($_COOKIE['username'])) 
{ 
    if(isset($_POST['Submit'])) 
{ 
$dbc  = mysqli_connect("localhost","root","","authserver"); 
$username = $_POST['name']; 
$password = $_POST['password']; 
$ccno  = $_POST['ccno']; 

    if(!empty($username) && !empty($password) && !empty($ccno)) 
{ 
    $query = "select * from authserver.fimembers where fName = '$username' AND  password_finmem=SHA('$password') AND CreditCard = $ccno"; 
$result = mysqli_query($dbc,$query); 

if(mysqli_num_rows($result) == 1) 
{ 
$dbc1 = mysqli_connect("localhost","root","","fininsti"); 
$query1 = "select * from fininsti.fimembers where fName = '$username' AND password_finmem=SHA('$password') AND CreditCard = $ccno"; 
    $result1 = mysqli_query($dbc1,$query1); 
$row = mysqli_fetch_array($result1,MYSQL_BOTH); 
setcookie('username',$username,time()+60*60); 
setcookie('ccno',$row[0],time()+60*60); 
echo $totPrice.'<br />'; 
if($totPrice > $row[3]) 
if($_GET['totP'] > $row[3]) 
{ 
    $status = array('stat' => 0); // 0 = Not sufficient funds 
} 
else 
{ 
$status = array('stat' => 1); // 1 = Good To Go! 
$newAmt = $row[3]-$totPrice; 
$query = "update fininsti.fimembers set Credit = $newAmt where CreditCard = $ccno"; 
$result = mysqli_query($dbc1,$query); 
}   
$retMerUrl = "http://localhost/eTrans/site/confirm.php?".http_build_query($status); 
setcookie('username',$username,time()-60*60); 
setcookie('ccno',$row[0],time()-60*60); 
mysqli_close($dbc1); 
mysqli_close($dbc); 
header('Location:'.$retMerUrl);    
} 
else 
    echo "Credentials don't match!"; 
} 
else 
{ 
    echo "Sorry! Fields empty!"; 
} 
setcookie('userId',$username,time()-60*60); 
setcookie('ccno',$row[0],time()-60*60); 
mysqli_close($dbc); 
} 
} 
?> 

請不要打電話給我,如果你有這個問題的任何問題。

+0

只是其他一些建議:使用php會話,從不發送密碼只是由$ _POST –

+1

是totP在它打的頁面的URL?該URL應該看起來像這樣:p1.php?totP = 5 – CastroXXL

+1

你是否用這個代碼嚴格處理信用卡信息? – ThiefMaster

回答

3

你需要修復的前兩行:

global $totPrice; 
$totPrice = $_GET['totP']; 
  1. 刪除的第一行。功能之外不需要global
  2. 與此更換第二行:

    $totPrice = isset($_GET['totP']) ? $_GET['totP'] : 0; 
    
  3. (不涉及這兩條線)在代碼中修復SQL注入的問題!
+0

1)問題是$ totPrice的值來自另一頁。因此,解決方案除了你提出的建議是: -

」method =「post 「> 2)任何修復SQL注入的建議? –

1

根據您收到錯誤消息(S),很明顯的是,totP不被包含在腳本中引用的URL。所以最好的辦法是參考$_GET參數,例如前,包括幾個isset檢查:

$totPrice = (isset($_GET['totP'])) ? $_GET['totP'] : null; 

而且,不知道爲什麼要製作global電話,因爲你不似乎是一個函數內。

+0

事情是$ totPrice的價值來自另一個頁面。因此,解決方案除了你提出的建議是: - 」method =「post 「> –

0

要刪除通知與isset($_GET['totP']會做檢查。如果它位於URL中但未顯示在您的頁面中,請確保沒有重寫發生。

您可以隨時通過var_dump($_GET)查看特定代碼的查詢參數中的所有信息。

它可能會有助於看到它是什麼收到你知道什麼是錯的。