frobak Posted September 18, 2008 Share Posted September 18, 2008 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? Quote Link to comment Share on other sites More sharing options...
.josh Posted September 18, 2008 Share Posted September 18, 2008 Show what you've tried. You can assign a posted var to a session var no problem. Do you have session_start() at the top of all the pages that use it (including where you set it)? Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 18, 2008 Share Posted September 18, 2008 Yes, you can use session_start() and then add a variable called $_SESSION['username'] or something. If that doesn't help much, include your code. Quote Link to comment Share on other sites More sharing options...
frobak Posted September 18, 2008 Author Share Posted September 18, 2008 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? Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 18, 2008 Share Posted September 18, 2008 Replace $_SESSION[$userid] = $userid; with $_SESSION['userid'] = $userid; Quote Link to comment Share on other sites More sharing options...
.josh Posted September 18, 2008 Share Posted September 18, 2008 $_SESSION[$userid] = $userid; needs to be $_SESSION['userid'] = $userid; ...unless you intended to make the session var name the same as the userid number, which I don't think you did. Then from another script you'd just do: session_start(); echo $_SESSION['userid']; Quote Link to comment Share on other sites More sharing options...
frobak Posted September 18, 2008 Author Share Posted September 18, 2008 So how would i use the variable say to update a table, ie UPDATE customers WHERE userid = ????????? Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 18, 2008 Share Posted September 18, 2008 UPDATE customers WHERE userid = $_SESSION['userid']; Quote Link to comment Share on other sites More sharing options...
frobak Posted September 18, 2008 Author Share Posted September 18, 2008 excellent, cheers for your help guys! Quote Link to comment Share on other sites More sharing options...
frobak Posted September 18, 2008 Author Share Posted September 18, 2008 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 Quote Link to comment Share on other sites More sharing options...
.josh Posted September 18, 2008 Share Posted September 18, 2008 $sql = "SELECT * FROM customers WHERE userid= {$_SESSION['userid']}"; Quote Link to comment Share on other sites More sharing options...
frobak Posted September 18, 2008 Author Share Posted September 18, 2008 cheers Quote Link to comment Share on other sites More sharing options...
frobak Posted September 28, 2008 Author Share Posted September 28, 2008 $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? Quote Link to comment Share on other sites More sharing options...
.josh Posted September 28, 2008 Share Posted September 28, 2008 probably because of the comma right before it Quote Link to comment Share on other sites More sharing options...
frobak Posted September 28, 2008 Author Share Posted September 28, 2008 cheers Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.