1

Hey I'm new to php need help creating anew page per user. I have a user login and registration system already. I also have a profiles.php page but, how can I let the website make an automatic webpage for every new user.

Whenever I try to connect it through $_GET or $_POST I get an Undefined index error.

include ("includes/profiles.dbh.inc.php");

$requested_user = $_POST['mailuid'];

try{
    $stmt2 = $conn2->prepare("SELECT * FROM profile WHERE id = ?");

    $stmt2->execute(array($requested_user));

    $mydata = $stmt2->fetch();               
} catch (Exception $e) {               
    //error with mysql
    die();
}
Qirel
  • 25,449
  • 7
  • 45
  • 62

1 Answers1

0

I've seen this question come up a few times. One of the first things you need to consider is how is the user going to interact with this page.

  1. Are they going to access it via links? http://example.com/index.php?user=1
  2. Or are they going to submit a form via post? http://example.com/index.php

This distinction is important because it changes the way you handle their request.

Let's look at each one:

  1. Via a link: http://example.com/index.php?user=1

This will issue a get request to your server so you need to handle it via $_GET.

if (isset($_GET['user'])) {
   $userId = $_GET['user'];

   // ... query your DB and output result
}
  1. Via a form using post: http://example.com/index.php (body with contain "user=1").

This will issue a post request to your server so you need to handle it via $_POST

if (isset($_POST['user'])) {
   $userId = $_POST['user'];

   // ... query your DB and output result
}

On a side note, it is important to know that an HTML <form> can submit either a post or get request. You can control this via it's method attribute. If you don't specify the method attribute, then it defaults to get.

<form method="post">
   ...
</form>

<form method="get">
   ...
</form>

So, to answer your question. You're likely getting an undefined error because you're trying to access the post array using the key mailuid ($_POST['mailuid']) but that does not exist. It doesn't exist because:

  • you're receiving a get request and the post is empty OR
  • you are receiving a post request but it doesn't contain the key mailuid

To debug, go back to the basics - use $_GET

Change your code to use $_GET['mailuid'] and then make sure you access your page with the corresponding query string - ...?mailuid=1.

Finally, turn on error reporting - https://stackoverflow.com/a/5438125/296555. You should always do this and correct all errors and warnings.

waterloomatt
  • 3,662
  • 1
  • 19
  • 25