GET not SET problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

GET not SET problem

Post by ghadacr »

I keep getting these problems:
Notice: Undefined index: DateHeld in line 9

Notice: Undefined index: UserName line 10
Although i have set the both variables, i'm kinda of lost in which direction to go, i put the following code to see whether the code is being set and the condition is returning true.....

Code: Select all

[if (empty($DateHeld )) {
    echo '$DateHeld  is either 0, empty, or not set at all ';
}
if (empty($UserName )) {
    echo '$UserName  is either 0, empty, or not set at all $UserName';
}/php]
here is the code (Any help will be apperciated):

Code: Select all

<?PHP include 'opendb.php'; ?>

<?PHP include 'header.php'; ?>

<?php 
if(isset($_GET))
{

$DateHeld= $_GET['DateHeld'];
$UserName= $_GET['UserName'];

extract($_GET);



if (empty($DateHeld )) {
    echo '$DateHeld  is either 0, empty, or not set at all ';
}
if (empty($UserName )) {
    echo '$UserName  is either 0, empty, or not set at all $UserName';
}

// Evaluates as true because $var is set
if (isset($DateHeld )) {
    echo '$DateHeld  is set even though it is empty';
}

$daterep = str_replace("/","-",$AvailableFrom);
$datereps = str_replace("/","-",$AvailableTo);
$dat = str_replace("/","-",$DateHeld);

$newdate = date ("d M Y", strtotime ($daterep)); 
$newdates = date ("d M Y", strtotime ($datereps)); 
$newheld = date ("d M Y", strtotime ($dat)); 

$query = mssql_init ("sp_insertClientDetails"); 

mssql_bind($query, "@Surname", $surname, SQLVARCHAR);

mssql_bind($query, "@UserID", $UserName, SQLVARCHAR);

mssql_bind($query, "@DateHeld", $newheld, SQLVARCHAR);

mssql_bind($query, "@ForeName", $forename, SQLVARCHAR);

mssql_bind($query, "@DateConfirmed", $newheld, SQLVARCHAR);

mssql_bind($query, "RETVAL", &$ClientDetailID, SQLINT2);

if (($result = mssql_execute($query)) === false) 
{ 
    die('Could not execute the query, ' . $query ); 
	//echo "$DateHeld, $UserName";
} 

$query2 = mssql_init ("sp_HoldRoom"); 

mssql_bind($query, "@AvailableFrom", $newdate, SQLVARCHAR);

mssql_bind($query, "@AvailableTo", $newdates, SQLVARCHAR);

mssql_bind($query, "@ClientDetailID", $ClientDetailID, SQLINT2);

mssql_bind($query, "@UserName", $UserName, SQLVARCHAR);

mssql_bind($query, "@HotelRoomID", $HotelRoomID, SQLINT2);

if (($results = mssql_execute($query2)) === false) 
{ 
    die('Could not execute the query ' . $query2 ); 
} 


?> 
<?PHP

$count=mssql_num_rows($results);


if ($count == 0)
  {
echo "<p>Sorry, your search returned no results</p><br><input type='button' value='Retry' onClick='history.go(-1)'>";

} else {


$i=0;
while ($i < $count) {

$row=mssql_fetch_array($results);

	
?>
<form action="" method="get"> 
  <table width="95%" border="0">
    <tr> 
     <td width="19%"></td>
	  <td width="12%"><strong>Room Type:</strong></td>
      <td width="15%"><strong>Available From:</strong></td>
      <td width="13%"><strong>Available To:</strong></td>
      <td width="6%"><strong>Notes</strong></td>
      <td width="37%">&nbsp;</td>
    </tr>
    <?php 

	
while ($rows = mssql_fetch_array($results)) 
{ 
    echo '<tr>';
	echo '<td><a href="roomdetails.php?HotelRoomID=' . $rows['HotelRoomID'] . '">More information on hotel room</a></td>'; 
    echo '<td>' . $rows['RoomType' ] . '</td>'; 
	echo '<td>' . $rows['AvailableFrom' ] . '</td>';
	echo '<td>' . $rows['AvailableTo' ] . '</td>';
	echo '<td>' . $rows['Notes' ] . '</td>';
    echo '<td><input type="radio" name="HotelRoomID" value="' . $rows['HotelRoomID'] . '" />Select  ' . $rows['RoomType'] .' to update</td>'; 
    echo '</tr>'; 

}





?>
    <tr> 
      <td colspan="6"><input type="Submit" value="Hold Room"> <INPUT name="button" type="button" onClick="location.href='http://pb-sql/admin.php'" value="Cancel"> 
      </td>
    </tr>
  </table> 
</form> 
<?PHP 
 ?>
<?PHP include 'footer.php'; ?><?PHP }} }
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$_GET is always set. Might be an empty array but it's set.
try

Code: Select all

if( isset($_GET['DateHeld'], $_GET['UserName']) )
{

$DateHeld= $_GET['DateHeld'];
$UserName= $_GET['UserName'];
instead.
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Post by ghadacr »

Nice try, but it didnt work, i'm still getting the same errors:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Unlikely.
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Post by ghadacr »

Volka, lets not start this again..... I'm not liaring... :lol:


here is the code:

Code: Select all

[<?PHP include 'opendb.php'; ?>

<?PHP include 'header.php'; ?>

<?php 
extract($_GET);
if( isset($_GET['DateHeld'], $_GET['UserName']) ) 
{ 

$DateHeld= $_GET['DateHeld']; 
$UserName= $_GET['UserName'];
}




if (empty($DateHeld )) {
    echo '$DateHeld  is either 0, empty, or not set at all ';
}
if (empty($UserName )) {
    echo '$UserName  is either 0, empty, or not set at all $UserName';
}

// Evaluates as true because $var is set
if (isset($DateHeld )) {
    echo '$DateHeld  is set even though it is empty';
}

$daterep = str_replace("/","-",$AvailableFrom);
$datereps = str_replace("/","-",$AvailableTo);
$dat = str_replace("/","-",$DateHeld);

$newdate = date ("d M Y", strtotime ($daterep)); 
$newdates = date ("d M Y", strtotime ($datereps)); 
$newheld = date ("d M Y", strtotime ($dat)); 

$query = mssql_init ("sp_insertClientDetails"); 

mssql_bind($query, "@Surname", $surname, SQLVARCHAR);

mssql_bind($query, "@UserID", $UserName, SQLVARCHAR);

mssql_bind($query, "@DateHeld", $newheld, SQLVARCHAR);

mssql_bind($query, "@ForeName", $forename, SQLVARCHAR);

mssql_bind($query, "@DateConfirmed", $newheld, SQLVARCHAR);

mssql_bind($query, "RETVAL", &$ClientDetailID, SQLINT2);

if (($result = mssql_execute($query)) === false) 
{ 
    die('Could not execute the query, ' . $query ); 
	//echo "$DateHeld, $UserName";
} 

$query2 = mssql_init ("sp_HoldRoom"); 

mssql_bind($query, "@AvailableFrom", $newdate, SQLVARCHAR);

mssql_bind($query, "@AvailableTo", $newdates, SQLVARCHAR);

mssql_bind($query, "@ClientDetailID", $ClientDetailID, SQLINT2);

mssql_bind($query, "@UserName", $UserName, SQLVARCHAR);

mssql_bind($query, "@HotelRoomID", $HotelRoomID, SQLINT2);

if (($results = mssql_execute($query2)) === false) 
{ 
    die('Could not execute the query ' . $query2 ); 
} 


?> 
<?PHP

$count=mssql_num_rows($results);


if ($count == 0)
  {
echo "<p>Sorry, your search returned no results</p><br><input type='button' value='Retry' onClick='history.go(-1)'>";

} else {


$i=0;
while ($i < $count) {

$row=mssql_fetch_array($results);

	
?>
<form action="" method="get"> 
  <table width="95%" border="0">
    <tr> 
     <td width="19%"></td>
	  <td width="12%"><strong>Room Type:</strong></td>
      <td width="15%"><strong>Available From:</strong></td>
      <td width="13%"><strong>Available To:</strong></td>
      <td width="6%"><strong>Notes</strong></td>
      <td width="37%">&nbsp;</td>
    </tr>
    <?php 

	
while ($rows = mssql_fetch_array($results)) 
{ 
    echo '<tr>';
	echo '<td><a href="roomdetails.php?HotelRoomID=' . $rows['HotelRoomID'] . '">More information on hotel room</a></td>'; 
    echo '<td>' . $rows['RoomType' ] . '</td>'; 
	echo '<td>' . $rows['AvailableFrom' ] . '</td>';
	echo '<td>' . $rows['AvailableTo' ] . '</td>';
	echo '<td>' . $rows['Notes' ] . '</td>';
    echo '<td><input type="radio" name="HotelRoomID" value="' . $rows['HotelRoomID'] . '" />Select  ' . $rows['RoomType'] .' to update</td>'; 
    echo '</tr>'; 

}





?>
    <tr> 
      <td colspan="6"><input type="Submit" value="Hold Room"> <INPUT name="button" type="button" onClick="location.href='http://pb-sql/admin.php'" value="Cancel"> 
      </td>
    </tr>
  </table> 
</form> 
<?PHP 
 ?>
<?PHP include 'footer.php'; ?><?PHP }} 
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Maybe you get a new error message. But there is no way that you get the same error.
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Post by ghadacr »

volka wrote:Maybe you get a new error message. But there is no way that you get the same error.
$DateHeld is either 0, empty, or not set at all $UserName is either 0, empty, or not set at all $UserName
Notice: Undefined variable: DateHeld in on line 31
Apart from the UserName error not appearing(Which is not set, because i checked and that is not being passed through the URL)

That is the error i'm getting..... Actually alhtough i noticed before both variables are not being passed in the URL!!!!

:?: :?:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

ghadacr wrote:Volka, lets not start this again..... I'm not liaring... :lol:
well well.
Ok. let's take a look at the new warning.
line 31
$dat = str_replace("/","-",$DateHeld);
$DateHeld is set only in one place
$DateHeld= $_GET['DateHeld'];
And that only happens if $_GET['DateHeld'] is set. Otherwise there is no $DateHeld (yet).
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Post by ghadacr »

volka wrote:
ghadacr wrote:Volka, lets not start this again..... I'm not liaring... :lol:
well well.
Ok. let's take a look at the new warning.
line 31
$dat = str_replace("/","-",$DateHeld);
$DateHeld is set only in one place
$DateHeld= $_GET['DateHeld'];
And that only happens if $_GET['DateHeld'] is set. Otherwise there is no $DateHeld (yet).
That bit of code is onlying replacing the characters within the date string, and i have called other variables in using the same string replace code and not getting that problem....

Code: Select all

$datefrm = $_GET['datefrm'];
$dateto = $_GET['dateto'];

$daterep = str_replace("/","-",$datefrm);
$datereps = str_replace("/","-",$dateto);


$newdate = date ("d M Y", strtotime ($daterep)); 
$newdates = date ("d M Y", strtotime ($datereps));
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Then those variables are set, $DateHeld is not when php complains.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Ok, time for a little logic.

Code: Select all

<?php
// This checks for querystring vars called DateHeld and UserName. If they are 
// present, then the if gets executed. If not, then the $DateHeld and $UserName 
// vars are unset/undefined
if( isset($_GET['DateHeld'], $_GET['UserName']) )
{
  $DateHeld= $_GET['DateHeld'];
  $UserName= $_GET['UserName'];
}

// If this fires, then your querystring did not pass the DateHeld var
if (empty($DateHeld )) {
    echo '$DateHeld  is either 0, empty, or not set at all ';
}

// If this fires, then your querystring did not pass the UserName var
if (empty($UserName )) {
    echo '$UserName  is either 0, empty, or not set at all $UserName';
}

// Evaluates as true because $var is set
// THIS CAN STILL EVALUATE TO TRUE IF UserName WAS PASSED
// IN THE QUERYSTRING BECAUSE YOU SET DateHel AND UserName 
// AT THE SAME TIME. DateHeld COULD BE NULL, WHICH MEANS $DateHeld
// IS SET, BUT IS STILL EMPTY
if (isset($DateHeld )) {
    echo '$DateHeld  is set even though it is empty';
}
?>
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Post by ghadacr »

Thanks Everah will try it tommorow. Let you know how it went............... Hopefully the clarity will sort out the clutter......
Post Reply