Jump to content

Various query string values..


ROCKSTAR

Recommended Posts

Hey guys,

 

I'm new to PHP, recently evolved from ASP.

 

I'm trying to get to grips with some of the differences and have come across a problem. I've a few area's on a page that display (or don't) depending on the query string but I keep getting errors. For example, if a record is updated in a database I return to the page with something like ?add=ok so that the page can display the message "Record Updated" which works fine, however when I simply visit the page without updating I get the following error:

 

Notice: Undefined index: add in e:\yada yada yada\index.php on line 152

 

I know in ASP it would simply ignore the fact that it was empty and wouldn't display anything (the result I was hoping for here) but this doesnt seem to like the fact its not been given a value - I've tried a few tutorial sites but none of them go into this type of detail regarding IF statements or the QueryString. The code I'm using is;

 

<?

if($_GET['add'] == "ok")

{

   

    echo "<span class='Text-Red'>- Record Added</span>";

}

?>

 

Can someone point me in the right direction?

 

Thanks in advance.

 

Link to comment
Share on other sites

Thanks for the ridiculously quick reply!  PHP really is better in all areas! :)

 

Would I be right in thinking that the below would work for another section of the page I'm using the querystring that checks for an id (for editing records, using the id in a lookup);

 

if( isset($_GET['id'] && $_GET['id'] != "")

 

 

So, if it gets an id then I'll do a lookup.

 

Thanks again for the quicky reply and baring with my level of n00bness..

 

Link to comment
Share on other sites

I'd have to see exactly what it is you're doing, but:

 

if( isset($_GET['id'] && $_GET['id'] != "")

 

Should be:

 

if( isset($_GET['id']) && $_GET['id'] != "" )

{

}

 

However, instead of using != "" it might be more appropriate to determine if it's an integer or not. It depends on your data model, but make sure you are forcing people to play by your rules so that you can avoid being haxored.

Link to comment
Share on other sites

Cheers, I've got it working although the page doesnt always like me using this multiple times on the same page. The page has add, update and an image upload script on it, each one with a little snip of code just to say "record added" or "updated" or "uploaded".

 

The common error I get is "," or ")" expected but I'm sure all the code is closed off correctly. I managed a work-around anyway so thanks for all the help but if there is a different way for doing this sort of thing multiple times please let me know?

 

Thanks again.

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.