Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

The following code works well when the correct URL is used like... http://example.com/user.php?id=joe

Where I need help is where do I add an if statement and how should it be written to provide either a default landing page or better, an input field so the visitor can retype the user name and submit it to have the correct page load.

<?php
$pdo = new PDO('mysql:host=localhost;dbname=users', 'root', '');

// retrieve member's data
$ps = $pdo->prepare("SELECT * FROM members WHERE id = ?");
    if(isset($_GET["id"])) {
    $ps->execute(array($_GET["id"]));
    }

$result = $ps->fetch(PDO::FETCH_ASSOC);

extract($result);

echo $First_Name . ' - ' . $Email; 

Any help?

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted
if(!empty($_GET['id'])) {
  // YOUR PREVIOUS CODE HERE
} else {
  // REDIRECT CODE HERE
}
share|improve this answer
 
This is in the right direction and it answers the question that I asked. However, if the id is misspelled like using "joee" instead of "joe", I get "Warning: extract() expects parameter 1 to be array, boolean given". It even works if I remove the "id=". –  CubicalWeb Mar 7 at 15:17
 
Well you have to check if you have a valid result set after executing your query. I recommend opening another question for this if you cant figure it out on your own. –  Merlin Denker 16 hours ago
 
Thanks Merlin. I created another if statement for the $results array where the else provides a text input field to retype the id. The page does what I need it to do now. –  CubicalWeb 13 hours ago
 
You're welcome! –  Merlin Denker 12 hours ago
add comment

I assume you'd want to wrap all the code with the if statement so it doesn't run unless an id is present.

<?php
    if( isset( $_GET['id'] ) ) {
        //retrieve member data here and render normal page
    }
    else {
        //render landing page
    }
?>

It might be wise to put the code for rendering the two different pages into different functions to help with organization as well.

share|improve this answer
 
I was trying this originally, but it resulted in errors. The "!empty" was the ticket. –  CubicalWeb Mar 7 at 15:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.