Jump to content

Recommended Posts

Hi all,

 

I my making a site which to be easier i have  made a querystring statement, but the issue is that when accessing /?action= shows index and ?action=register shows register great!, when accessing root / of the site or index.php and displays Undefined index: action , when clearly i have defined action with $id, so why is php saying it is not. I know there is the option for error_reporting(0), but i want to know why this is happening.

 

Edit: the script is, and runs fine other than the error!

 

<?php 
$id = "action";
$string = $_GET["$id"];

	if ($string == "register") {
		echo 'register';
	} else {
		echo 'index';
}


?>


 

Many Thanks

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/238196-undefined-index-action/
Share on other sites

You should post your code.

 

But are you doing a check if action is set or not?

 

if(isset($_GET['action'])) {
//execute the query
}

//or even
if(!isset($_GET['action']) || $_GET['action'] == "") {
//do not execute the query
} else {
//execute the query
}

Thanks for the reply. When i put the isset statement in the error still appeared but after moving the $string = $_GET["id"] into the isset statement and outside of it, the error has gone away sorted code follows:

 

<?php 
$id = "action";

	if (isset($_GET["$id"])) {

	    $string = $_GET["$id"];

	if ($string == "register") {
		echo 'register';
	} 
}


?>

Many thanks for your help  

Thanks for the reply. When i put the isset statement in the error still appeared but after moving the $string = $_GET["id"] into the isset statement and outside of it, the error has gone away sorted code follows:

 

<?php 
$id = "action";

	if (isset($_GET["$id"])) {

	    $string = $_GET["$id"];

	if ($string == "register") {
		echo 'register';
	} 
}


?>

Many thanks for your help  

excellent...please mark this thread as solved..lower left of the thread..and as a side note..if you are retrieving a variable useing $_GET, you do not need quotes

$_GET[$id]

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.