Dexlin Posted June 2, 2011 Share Posted June 2, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/238196-undefined-index-action/ Share on other sites More sharing options...
fugix Posted June 2, 2011 Share Posted June 2, 2011 your $id var might be returning empty...hard to say without seeing some code Quote Link to comment https://forums.phpfreaks.com/topic/238196-undefined-index-action/#findComment-1224016 Share on other sites More sharing options...
QuickOldCar Posted June 2, 2011 Share Posted June 2, 2011 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 } Quote Link to comment https://forums.phpfreaks.com/topic/238196-undefined-index-action/#findComment-1224018 Share on other sites More sharing options...
spiderwell Posted June 2, 2011 Share Posted June 2, 2011 $id = "action"; $string = $_GET["$id"]; god know what you are trying to do there. use this $string = isset($_GET['action']) ? $_GET['action'] : 'index'; that says if there is an 'action' variable, pass its value to $string, else $string = 'index' Quote Link to comment https://forums.phpfreaks.com/topic/238196-undefined-index-action/#findComment-1224023 Share on other sites More sharing options...
fugix Posted June 2, 2011 Share Posted June 2, 2011 $id = "action"; $string = $_GET["$id"]; what is the logic behind this? Quote Link to comment https://forums.phpfreaks.com/topic/238196-undefined-index-action/#findComment-1224026 Share on other sites More sharing options...
Dexlin Posted June 2, 2011 Author Share Posted June 2, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/238196-undefined-index-action/#findComment-1224027 Share on other sites More sharing options...
teynon Posted June 2, 2011 Share Posted June 2, 2011 Holy code injection, Batman! That's ugly. You should validate all variables that can be set via post or get. Quote Link to comment https://forums.phpfreaks.com/topic/238196-undefined-index-action/#findComment-1224028 Share on other sites More sharing options...
fugix Posted June 2, 2011 Share Posted June 2, 2011 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] Quote Link to comment https://forums.phpfreaks.com/topic/238196-undefined-index-action/#findComment-1224029 Share on other sites More sharing options...
Dexlin Posted June 2, 2011 Author Share Posted June 2, 2011 sorry, i trying to get to grips with php Quote Link to comment https://forums.phpfreaks.com/topic/238196-undefined-index-action/#findComment-1224030 Share on other sites More sharing options...
Dexlin Posted June 2, 2011 Author Share Posted June 2, 2011 Thanks for the tip , i will clean the code up Quote Link to comment https://forums.phpfreaks.com/topic/238196-undefined-index-action/#findComment-1224033 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.