user13477176
user13477176

Reputation: 119

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'] : '' ;

Upvotes: 0

Views: 675

Answers (1)

maio290
maio290

Reputation: 6732

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 ;

Upvotes: 1

Related Questions