简体   繁体   中英

Get URL parameter PHP

I am trying to get the parameter from my URL. This is how it looks http://localhost/project1/post.php?subtopic_id=3 . This is how I get the parameter from the URL

$subtopic_id = isset($_GET['subtopic_id']) ? $_GET['subtopic_id'] : 3 ;

When I put a number in the else statement (like I currently have the number 3 in it) I get that number. But when I have the else statement blank the only number that shows up in the DB is 0. I don't know why this is happening even though the parameter in the URL has a value in it.

When the code is like this and I try to add a comment to my post the subtopic_id value is 0 when it's suppose to have a different number

$subtopic_id = isset($_GET['subtopic_id']) ? $_GET['subtopic_id'] : '' ;

It's a common pitfall.

I guess you're using a common form with subtopic_id being one input? Even when the input is empty, it will be send. Therefore you should not only check whether the variable is set but also if the variable is empty:

$subtopic_id = !empty($_GET['subtopic_id']) ? $_GET['subtopic_id'] : 3 ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM