Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Using information that a user has already provided in their registration (and which is now in an SQL database), when the user signs in and then clicks to go to a form, I want some of the form to already be pre-populated. The log-in form is as such:
- <?php
- session_start();
- // check for required fields from the form
- if ((!isset($_POST['email'])) || (!isset($_POST['password']))) {
- header("Location: userlogin.html");
- exit;
- }
- //connect to server and select database
- $mysqli = mysqli_connect("localhost", "user", "password", "database")
- or die(mysqli_error());
- // use mysqli_real_escape_string to clean the input
- $email = mysqli_real_escape_string($mysqli, $_POST['email']);
- $password = mysqli_real_escape_string($mysqli, $_POST['password']);
- // Create and issue the query
- $sql = "SELECT username FROM frmak_form_1 WHERE
- email = '".$email."' AND
- password = '".$password."'";
- $result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
- //get the number of rows in the result set; should be 1 if a match.
- if (mysqli_num_rows($result) == 1 ) {
- // if authorized, get value of username
- while ($info = mysqli_fetch_array($result)) {
- $username = stripslashes($info['username']);
- }
- $_SESSION['user']= $info['username'];
- $_SESSION['login_until'] = time() + 60 * 60; // login expires in 1 hour
- $display_block = "<p>Welcome, ".$username."!</p>
- <br /> <br />
- <a href='submitReview.php'>Click Here to Submit a Review!</a>";
- }
- else {
- header("Location: userlogin.html");
- exit;
- }
- mysqli_close($mysqli);
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>User Login REDIRECT!!!</title>
- </head>
- <body>
- <?php echo $display_block; ?>
- </body>
- </html>
- Then the "submitReview.php" file is:
- <?php
- session_start();
- $s = &$_SESSION; // Login status is ON and has NOT yet expired
- $isLoggedIn = isset($s['login_until']) && $s['login_until'] > time();
- if($isLoggedIn):
- $username = $s['user'];
- $s['login_until'] = time() + 60 * 60; // extend login expiration to an hour from now.
- else
- unset($s['login_until'], $['user']);
- $username = null;
- endif;
- exit;
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- </head>
- <body>
- <p>
- <label>username</label>
- <br />
- <input name="usrnm" id="usrnm" type="text" value="<?php echo $username; ?>"/>
- <br />
- </p>
- </form>
- </body>
- </html>
- In the example above, I'm only trying to display the user name in the form, because that's the only part that I want pre-populated as just a text field. The others are a radio button and a check box field, and I know those will take an even more complicated argument, so I'm just trying to make the simplest one work first.
- If what I've done is not even remotely what should be done, could someone please just direct me toward a good tutorial for such things? I'm trying to teach myself PHP and MySQL, so sometimes it is a bit overwhelming. Thanks!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement