簡體   English   中英

帶有GET和POST的PHP中的未定義索引

[英]Undefined Index in php with GET and POST

我有一個說p1.php的php文件,它從另一個說p2.php的php文件中獲取數據,該文件通過$ _GET在p1.php中訪問。 現在,$ _ GET中的數據將保存在變量$ totPrice中。 p1.php也具有引用自身的形式,並且對MySql數據庫進行了一些處理。 我收到以下錯誤:

"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);
}
}
?>

如果您對此問題有任何疑問,請務必與我聯系。

您需要修復前兩行:

global $totPrice;
$totPrice = $_GET['totP'];
  1. 刪除第一行。 您不需要函數之外的global變量。
  2. 將第二行替換為:

     $totPrice = isset($_GET['totP']) ? $_GET['totP'] : 0; 
  3. (與這兩行無關) 解決了代碼中的SQL注入問題!!!

根據收到的錯誤消息,很明顯,腳本引用的URL中未包含totP 因此,最好的選擇是在引用$_GET參數之前包括一些isset檢查,例如:

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

同樣,不確定您為什么要進行global調用,因為您似乎不在函數內。

要刪除該通知,請使用isset($_GET['totP']進行檢查。如果該通知位於URL中但未在您的頁面中顯示,請確保沒有重寫。

您總是可以執行var_dump($_GET)來查看特定代碼的查詢參數中的所有信息。

查看接收到的內容可能會有所幫助,讓您知道出了什么問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM