Jump to content

Again , undefined index and variable error


IreneLing

Recommended Posts

Sorry again , but it seems I still have some problem with my $_GET.

 

Here is the error I get:

 

Notice: Undefined variable: id in /opt/lampp/htdocs/EMC/customer-search.php on line 248

Notice: Undefined index: Page in /opt/lampp/htdocs/EMC/customer-search.php on line 255

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /opt/lampp/htdocs/EMC/customer-search.php on line 302

 

And my code for second error:

	$Per_Page = 2;   // Per Page

$Page = isset($_GET["Page"]);
if (!$_GET["Page"])
{
	$Page=1;
}

 

I'm not really sure what's the problem with my first and third error so I will post the whole code here:

<?php

require_once("./include/fg_membersite.php");
require_once("./include/membersite_config.php"); 



$objConnect = mysql_connect("localhost","root","") or die(mysql_error());
$objDB = mysql_select_db("EMC");

$haha = $fgmembersite->UserEmail();
$_SESSION['storedemail']= $haha;

$query4 = mysql_query("SELECT * FROM Register WHERE Email= '$haha'");
while ($row4 = mysql_fetch_array($query4)){

    $id = $row4['ID'];
}
mysql_free_result($query4);

?>

<html>

<body>


<form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>">
  <table width="599" border="1">
    <tr>
      <th>Keyword
      <input name="txtFirstName" type="text" id="txtFirstName" value="<?= isset($_GET["txtFirstName"]) ;?>">
      <input name="txtLastName" type="text" id="txtLastName" value="<?= isset($_GET["txtLastName"]) ;?>">
      <input name="txtCell" type="text" id="txtCell" value="<?= isset($_GET["txtCell"]) ;?>">




<?

     $opselect="SELECT DISTINCT Custgroup FROM UserAddedRecord";

$result = mysql_query ($opselect);

while($nt=mysql_fetch_array($result))
{
?>
<option><?= $nt[Custgroup]?></option>";

<?
}
?>

</select>
      <input type="submit" value="Search">
    </tr>
  </table>
</form>
<?
if (isset($_GET["txtFirstName"]) != "" || isset($_GET["txtLastName"]) != "" || isset($_GET["txtCell"]) != "" || isset($_GET["txtGroup"]) != "")
{
$objConnect = mysql_connect("localhost","root","") or die(mysql_error());
$objDB = mysql_select_db("EMC");
// Search By Name or Email
$strSQL = "SELECT * FROM UserAddedRecord WHERE (FirstName LIKE '%".$_GET["txtFirstName"]."%' and LastName LIKE '%".$_GET["txtLastName"]."%' and Cell LIKE '%".$_GET["txtCell"]."%' and Custgroup LIKE '%".$_GET["txtGroup"]."%')";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mysql_num_rows($objQuery);


$Per_Page = 2;   // Per Page

$Page = isset($_GET["Page"]);
if (!$_GET["Page"])
{
	$Page=1;
}

$Prev_Page = $Page-1;
$Next_Page = $Page+1;

$Page_Start = (($Per_Page*$Page)-$Per_Page);
if($Num_Rows<=$Per_Page)
{
	$Num_Pages =1;
}
else if(($Num_Rows % $Per_Page)==0)
{
	$Num_Pages =($Num_Rows/$Per_Page) ;
}
else
{
	$Num_Pages =($Num_Rows/$Per_Page)+1;
	$Num_Pages = (int)$Num_Pages;
}


$strSQL .=" order  by addedrec_ID DESC LIMIT $Page_Start , $Per_Page";
$objQuery  = mysql_query($strSQL);

?>
<table width="600" border="1">
  <tr>
	<th width="50"> <div align="center">#</div></th>
	<th width="91"> <div align="center">CustomerID </div></th>
	<th width="98"> <div align="center">Name </div></th>
	<th width="198"> <div align="center">Email </div></th>
	<th width="97"> <div align="center">CountryCode </div></th>
	<th width="59"> <div align="center">Budget </div></th>
	<th width="71"> <div align="center">Used </div></th>
  </tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
  <tr>
	<td><div align="center"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?=$objResult["addedrec_ID"];?>"></div></td>
	<td><div align="center"><?=$objResult["addedrec_ID"];?></div></td>
	<td><?=$objResult["FirstName"];?></td>
	<td><?=$objResult["LastName"];?></td>
	<td><div align="center"><?=$objResult["MobileCompany"];?></div></td>
	<td align="right"><?=$objResult["Cell"];?></td>
	<td align="right"><?=$objResult["Workphone"];?></td>
  </tr>
<?
}
?>
</table>
<br>
Total <?= $Num_Rows;?> Record : <?=$Num_Pages;?> Page :
<?
if($Prev_Page)
{
           if($_GET["txtFirstName"] == "")
                      {
	echo " <a href='$_SERVER[sCRIPT_NAME]?Page=$Prev_Page&txtLastName=$_GET[txtLastName]'><< Back</a> ";	
                      }

            else
                      {
	echo " <a href='$_SERVER[sCRIPT_NAME]?Page=$Prev_Page&txtFirstName=$_GET[txtFirstName]'><< Back</a> ";
                      }

}

for($i=1; $i<=$Num_Pages; $i++){
	if($i != $Page)
	{

		echo "[ <a href='$_SERVER[sCRIPT_NAME]?Page=$i&txtFirstName=$_GET[txtFirstName]'>$i</a> ]";	

	}
	else
	{
		echo "<b> $i </b>";
	}
}
if($Page!=$Num_Pages)
{
              if($_GET["txtFirstName"] == "" && $_GET["txtLastName"] == "" && $_GET["txtCell"] == "")
                           {
	        echo " <a href ='$_SERVER[sCRIPT_NAME]?Page=$Next_Page&txtGroup=$_GET[txtGroup]'>Next>></a> ";
                           }
              else if($_GET["txtFirstName"] == "" && $_GET["txtLastName"] == "")
                           {
	        echo " <a href ='$_SERVER[sCRIPT_NAME]?Page=$Next_Page&txtCell=$_GET[txtCell]'>Next>></a> ";	
                           }
              else if($_GET["txtFirstName"] == "" )
                           {
		echo " <a href ='$_SERVER[sCRIPT_NAME]?Page=$Next_Page&txtLastName=$_GET[txtLastName]'>Next>></a> ";	
                           }
              else
                           {
	        echo " <a href ='$_SERVER[sCRIPT_NAME]?Page=$Next_Page&txtFirstName=$_GET[txtFirstName]'>Next>></a> ";	}
                   }

mysql_close($objConnect);


}	

else 
{

$strSQL = "SELECT * FROM UserAddedRecord WHERE user_id='$id'";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mysql_num_rows($objQuery);

$Per_Page = 2;   // Per Page

$Page = isset($_GET["Page"]);
if (!$_GET["Page"])
{
	$Page=1;
}

$Prev_Page = $Page-1;
$Next_Page = $Page+1;

$Page_Start = (($Per_Page*$Page)-$Per_Page);
if($Num_Rows<=$Per_Page)
{
	$Num_Pages =1;
}
else if(($Num_Rows % $Per_Page)==0)
{
	$Num_Pages =($Num_Rows/$Per_Page) ;
}
else
{
	$Num_Pages =($Num_Rows/$Per_Page)+1;
	$Num_Pages = (int)$Num_Pages;
}


$strSQL .=" order  by id DESC LIMIT $Page_Start , $Per_Page";
$objQuery  = mysql_query($strSQL);





?>
<table width="600" border="1">
  <tr>
	<th width="50"> <div align="center">#</div></th>
	<th width="91"> <div align="center">ID </div></th>
	<th width="198"> <div align="center">First Name </div></th>
	<th width="198"> <div align="center">Last Name </div></th>
	<th width="250"> <div align="center">Mobile Company </div></th>
	<th width="100"> <div align="center">Cell </div></th>
	<th width="100"> <div align="center">Workphone </div></th>
	<th width="100"> <div align="center">Group </div></th>
  </tr>
<?

echo "<form name='form1' method='post' action=''>";

while($objResult = mysql_fetch_array($objQuery))
{


echo "<tr>";
   echo "<td align='center'><input name=\"checkbox[]\" type=\"checkbox\" id=\"checkbox[]\" value=\"$objResult[addedrec_ID]\"></td>";
   echo "<td>$objResult[addedrec_ID] </td>";
   echo "<td>$objResult[FirstName]</td>";
   echo "<td>$objResult[LastName] </td>";
   echo "<td>$objResult[MobileCompany] </td>";
   echo "<td>$objResult[Cell] </td>";
   echo "<td>$objResult[WorkPhone] </td>";
   echo "<td>$objResult[Custgroup] </td>";

echo "</tr>";

}


echo "<td colspan='7' align='center'><input name=\"delete\" type=\"submit\" id=\"delete\" value=\"Delete\">";

if(isset($_POST['delete'])) // from button name="delete"
 {
$checkbox = $_POST['checkbox']; //from name="checkbox[]"
	 $countCheck = count($_POST['checkbox']);

for($d=0;$d<$countCheck;$d++)
	 {
		 $del_id  = $checkbox[$d];

$sql = "DELETE from UserAddedRecord where addedrec_ID = $del_id";

$result2=mysql_query($sql);

	 }
		 if($result2)
	 {	
			 header('Location: customer-adddata.php');
		 }
		 else
		 {
echo "Error: ".mysql_error();
		 }
 }

echo "</form>";

?>
</table>
<br>
Total <?= $Num_Rows;?> Record : <?=$Num_Pages;?> Page :
<?
if($Prev_Page)
{
           if($_GET["txtFirstName"] == "")
                      {
	echo " <a href='$_SERVER[sCRIPT_NAME]?Page=$Prev_Page&txtLastName=$_GET[txtLastName]'><< Back</a> ";	
                      }

            else
                      {
	echo " <a href='$_SERVER[sCRIPT_NAME]?Page=$Prev_Page&txtFirstName=$_GET[txtFirstName]'><< Back</a> ";
                      }

}

for($i=1; $i<=$Num_Pages; $i++){
	if($i != $Page)
	{

		echo "[ <a href='$_SERVER[sCRIPT_NAME]?Page=$i&txtFirstName=$_GET[txtFirstName]'>$i</a> ]";	

	}
	else
	{
		echo "<b> $i </b>";
	}
}
if($Page!=$Num_Pages)
{
              if($_GET["txtFirstName"] == "" && $_GET["txtLastName"] == "")
                           {
	        echo " <a href ='$_SERVER[sCRIPT_NAME]?Page=$Next_Page&txtCell=$_GET[txtCell]'>Next>></a> ";	
                           }
              else if($_GET["txtFirstName"] == "" )
                           {
		echo " <a href ='$_SERVER[sCRIPT_NAME]?Page=$Next_Page&txtLastName=$_GET[txtLastName]'>Next>></a> ";	
                           }
              else
                           {
	        echo " <a href ='$_SERVER[sCRIPT_NAME]?Page=$Next_Page&txtFirstName=$_GET[txtFirstName]'>Next>></a> ";	}
                   }

mysql_close($objConnect);



}


?>
</body>
</html>

 

 

And one more error , when I tried to search data and typed some keywords in the textbox , three of the textbox will keep return me value "1" ,

 

2121.jpg

 

I really tried to solve the error but still there....can anyone help me for this?

Thanks in advanced.

 

Link to comment
Share on other sites

An undefined index or undefined variable error means that you are trying to use a function on, or trying to use a variable / array key that doesn't exist.

 

Here is your first snippet of code:

 

<?php
$Page = isset($_GET["Page"]);
if (!$_GET["Page"])
{
	$Page=1;
}
?>

 

The logic here is incorrect. $_GET['Page'] only exists if a value for Page is passed through the url. The first function that you use is isset(). If no Page parameter exists in the url then $_GET['Page'] is a reference to nothing and your isset() function is producing the error. You are using this funtion in the wrong way. It is not a function thats return value should be assigned to a variable. It is used to test the existence of a variable or array key. Here is a refactored version:

 

<?php
if(!isset($_GET['Page'])) 
{
$Page = 1;
}
else {
$Page = $_GET['Page'];
}
?>

 

You could also write this like:

 

<?php
$Page = isset($_GET['Page']) ? $_GET['Page'] : 1; 
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.