-1

i m working on php, so while running code.. i m getting these errors

Notice: Undefined index: re_cnbid in C:\xampp\htdocs\cnb\DB_UserData.php on line 16

Notice: Undefined variable: flag in C:\xampp\htdocs\cnb\DB_UserData.php on line 26 null

<?php
 
$host='localhost';
 
$uname='root';
 
$pwd='';
 
$db="cnb";
 

$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
 
mysql_select_db($db,$con) or die("db selection failed");
 
$recnbid = $_REQUEST['re_cnbid'];
 
$r=mysql_query("select * from header where re_cndid = '$recnbid' order by dt desc",$con);
 
while($row=mysql_fetch_array($r))
{
    $flag[]=$row;
}
 

print(json_encode($flag));
 
mysql_close($con);
 
?>
Community
  • 1
  • 1
Shilpa S
  • 1
  • 1

2 Answers2

0

These error are coming because it's not set:

Notice: Undefined index: re_cnbid => because its not getting $_REQUEST['re_cnbid']

Notice: Undefined variable: flag => because you have not initialized flag array

<?php

$host='localhost';

$uname='root';

$pwd='';

$db="cnb";


$con = mysql_connect($host,$uname,$pwd) or die("connection failed");

mysql_select_db($db,$con) or die("db selection failed");

$recnbid = 0;
if(isset($_REQUEST['re_cnbid'])) {
  $recnbid = $_REQUEST['re_cnbid'];
}


if($recnbid != 0) {
  $r=mysql_query("select * from header where re_cndid = '$recnbid'   order by dt desc",$con);
  $flag = array();
  while($row=mysql_fetch_array($r))
  {
    $flag[]=$row;
  }
  print(json_encode($flag));
}

mysql_close($con);

?>
Community
  • 1
  • 1
Dhara Parmar
  • 8,021
  • 1
  • 16
  • 27
0

try this added check if variable exists then run

$flag = array();

if (isset($_REQUEST['re_cnbid']))
{
      $recnbid = $_REQUEST['re_cnbid'];

      $r = mysql_query("select * from header where re_cndid = '$recnbid' order by dt desc", $con);

      while ($row = mysql_fetch_array($r))
      {
            $flag[] = $row;
      }
}
Maninderpreet Singh
  • 2,569
  • 2
  • 17
  • 31