简体   繁体   English

带有GET和POST的PHP中的未定义索引

[英]Undefined Index in php with GET and POST

I have a php file say p1.php that is getting data from another php file say p2.php which is accessed in p1.php via $_GET. 我有一个说p1.php的php文件,它从另一个说p2.php的php文件中获取数据,该文件通过$ _GET在p1.php中访问。 Now the data in $_GET is being saved in a variable $totPrice. 现在,$ _ GET中的数据将保存在变量$ totPrice中。 p1.php also has a form that is referencing to itself and some processing is done with a MySql database. p1.php也具有引用自身的形式,并且对MySql数据库进行了一些处理。 I am getting an error of: 我收到以下错误:

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

Here's the code for p1.php:- 这是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);
}
}
?>

Please do get back to me if you have any problem with the question. 如果您对此问题有任何疑问,请务必与我联系。

You need to fix the first two lines: 您需要修复前两行:

global $totPrice;
$totPrice = $_GET['totP'];
  1. Remove the first line. 删除第一行。 You do not need global outside of functions. 您不需要函数之外的global变量。
  2. Replace the second line with this: 将第二行替换为:

     $totPrice = isset($_GET['totP']) ? $_GET['totP'] : 0; 
  3. (not related to these two lines) Fix the SQL injection issues in your code!!! (与这两行无关) 解决了代码中的SQL注入问题!!!

Based on the error message(s) you are receiving, it's obvious that the totP is not being included in the URL the script is referencing. 根据收到的错误消息,很明显,脚本引用的URL中未包含totP So your best bet is to include a few isset checks before referencing $_GET parameters, for example: 因此,最好的选择是在引用$_GET参数之前包括一些isset检查,例如:

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

Also, not sure why you are making a global call, as you don't appear to be within a function. 同样,不确定您为什么要进行global调用,因为您似乎不在函数内。

To remove the notice a check with isset($_GET['totP'] would do. If it is in the URL but not showing up in your page, make sure there is no rewriting happening. 要删除该通知,请使用isset($_GET['totP']进行检查。如果该通知位于URL中但未在您的页面中显示,请确保没有重写。

You could always do var_dump($_GET) to see all of the information in the query params for the specific code. 您总是可以执行var_dump($_GET)来查看特定代码的查询参数中的所有信息。

It might be helpful to see what it is receiving for you to know whats wrong. 查看接收到的内容可能会有所帮助,让您知道出了什么问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM