Jump to content

[SOLVED] Assign a session variable from a posted form?


frobak

Recommended Posts

Hi, im making a simple user control panel, the user logs in by entering a user id and passowrd, which is checked in a mysql database before displaying the control panel. from this control panel i want the user to be able to change user details, sign up for bits, register for stuff etc. When the user clicks on a link to update their user details for example, i want to use the user id originally posted with the login form.

 

Is it possible to register a session variable from a posted field in a form, and then retreive and use throughout my control panel? Ive tried many different ways to pass variables but its not happening?

 

Any ideas?

Link to comment
Share on other sites

session_start();

if (isset($_POST['userid']) && isset($_POST['password'])) {

 

$table_name = "customers";

 

include '../functions/db_connect.inc.php';

 

$userid = $_POST['userid'];

$password = $_POST['password'];

 

  // check if the user id and password combination exist in database

  $sql = "SELECT * FROM customers WHERE userid= '$userid'";

 

  $result = mysql_query($sql)

            or die('Query failed. ' . mysql_error());

 

  if (mysql_num_rows($result) == 1) {

      // the user id and password match,

      // set the session

      $_SESSION['db_is_logged_in'] = true;

  $_SESSION[$userid] = $userid;

      // after login we move to the main page

      header('Location: ../pages/control_panel.php?$userid;');

      exit;

  } else {

      $errorMessage = 'Sorry, wrong user id / password';

  }

 

}

 

How do i get the posted value of userid to $_SESSION[$userid]?

 

and then how do i call it in another script?

Link to comment
Share on other sites

This doesnt work though?

 

$sql = "SELECT * FROM customers WHERE userid= $_SESSION['userid']";

 

it says: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Inetpub\vhosts\frobak.co.uk\httpdocs\pages\show_cust_details.php on line 18

Link to comment
Share on other sites

  • 2 weeks later...

$sql = "UPDATE customers SET

cust_firstname = '$_POST[cust_firstname]',

cust_surname = '$_POST[cust_surname]',

house_no = '$_POST[house_no]',

streetname = '$_POST[streetname]',

postcode = '$_POST[postcode]',

emailaddress = '$_POST[emailaddress]',

homephone = '$_POST[homephone]',

mobphone = '$_POST[mobphone]',

WHERE username = {$_SESSION['username']}";

 

why am i being told i have an sql error near my where clause?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.