繁体   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