FridayRain Posted September 8, 2007 Share Posted September 8, 2007 It just donned on me that I need an error message if someone tries to go to a page with a variable in the URL that doesn't correspond to a database entry. For example, http://www.domain.com/poetry.php?piece=4 does not yet exist. Say I only have three poems in the database right now. What are some ways (or the best way) to make sure the variable leads to content or to otherwise display an error message? Quote Link to comment https://forums.phpfreaks.com/topic/68492-solved-ensure-a-variable-passed-through-through-url-exists/ Share on other sites More sharing options...
jscix Posted September 8, 2007 Share Posted September 8, 2007 Simple.. if your new poetry entries are assigned a unique identifier upon submission, then if someone queues a poetry.php?poem=4, and there is not a poem assigned to that identifier (It should return nothing, or a blank array) then, you simply print out "Poem does not exist" Quote Link to comment https://forums.phpfreaks.com/topic/68492-solved-ensure-a-variable-passed-through-through-url-exists/#findComment-344296 Share on other sites More sharing options...
FridayRain Posted September 8, 2007 Author Share Posted September 8, 2007 Each piece has an id, so it returns nothing, but I have formatting around where the text would go, so it looks tacky. If no piece is selected, basically an isset function, then a message is displayed regarding selecting a piece. But if someone tries an id number that doesn't exist, how is PHP to know to display another message, like Piece does not exist? Quote Link to comment https://forums.phpfreaks.com/topic/68492-solved-ensure-a-variable-passed-through-through-url-exists/#findComment-344324 Share on other sites More sharing options...
FridayRain Posted September 8, 2007 Author Share Posted September 8, 2007 Is there an if statement I could drum up to check if the title and/or text of the piece corresponding to $id is blank? Quote Link to comment https://forums.phpfreaks.com/topic/68492-solved-ensure-a-variable-passed-through-through-url-exists/#findComment-344328 Share on other sites More sharing options...
KevinM1 Posted September 8, 2007 Share Posted September 8, 2007 Are you returning the poems from the database? If so, then something along the lines of: <?php $query = "SELECT poem FROM table WHERE piece = {$_GET['piece']}"; $result = mysql_query($query); if(mysql_num_rows($result) == 0){ echo "The poem you selected does not exitst."; } else{ $poem = mysql_fetch_assoc($result); echo $poem['body']; } ?> should work. EDIT: Keep in mind that you'll need to use the name of your script variables and database table columns and not exactly what I wrote above. I just guessed when naming the variables and columns in the example above. Quote Link to comment https://forums.phpfreaks.com/topic/68492-solved-ensure-a-variable-passed-through-through-url-exists/#findComment-344332 Share on other sites More sharing options...
FridayRain Posted September 8, 2007 Author Share Posted September 8, 2007 Thanks. I tried my own IF statement and it seems to be working. <?php if($content[title] == "") { echo "<p>Poem does not exist. Please choose a title.</p>"; } else { // blah blah } ?> Quote Link to comment https://forums.phpfreaks.com/topic/68492-solved-ensure-a-variable-passed-through-through-url-exists/#findComment-344338 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.