March 13, 200917 yr Hey I am wondering if anyone knows how you can add session information into a database. So far I have a session started when a user logs in and from there I have a page set up so they can add additional information such as their real name / email etc... What I want to happen is when a user fills in these boxes the info is sent to the same row? where their username / password is. like this... userid|Username|Password|NAME|EMAIL | 1 |matt123 | passkey |matt |matt@.com| <------ all the session data to be added and stored in a table on the same line. So far i have an input form but it sends the info to the next userid. my userinput code so far is. <?php //check to see if user is logged in session_start(); //Connecting to MySQL $con = mysql_connect("localhost","root","root"); if(!$con){ echo "Error connecting to MySQL"; } //Connecting to Database $db = mysql_select_db("search_site",$con); if(!$db){ echo "Error connecting to Database"; } if(!isset($_SESSION['user'])) { echo "please log in to see this page"; } else{ echo $_SESSION['user'] . " is logged in<br />"; echo '<form action="userinfo.php" method="post"> Name: <input type="text" name="realname"><br /> E-mail: <input type="text" name="email"><br /> <input type="submit" value="send"> </form>'; } $input = true; if ( ($_POST["realname"] == "") || ($_POST["email"] == "") ) { $input = false; } $realname = $_POST["realname"]; $email = $_POST["email"]; if (!$input) { echo('You need to put a Username and Password'); } else { mysql_query("INSERT INTO user (name, email) VALUES ('$realname', '$email')"); echo "Welcome " . $realname; } ?> I would greatly appreciate any help Thanks Matt Also here is some info where i defined the session data earlier this works ok: $username = $_POST["username"]; $_SESSION['user']= $username;
March 13, 200917 yr You have: mysql_query("INSERT INTO user (name, email) VALUES ('$realname', '$email')"); which is fine for the first query since it inserts a new record. On subsequent updates you need to use an UPDATE query like this: mysql_query("UPDATE user SET email='$email' WHERE ???=???"); The question is how you limit the update to just the one row - this is where I've put ???=???. The left side needs to be a unique key for the record to be updated, the right side is the value identifying that row. So if your username column is unique, you could store the $username in the session and do this for example: mysql_query("UPDATE user SET email='$email' WHERE Username=$username"); You can also use the id column for this by including a mysql_last_insert_id() in your code after the initial INSERT, and then storing that in the session, but that's a bit more cumbersome. You'd also have to retrieve it each time an existing user logs in. Probably best to stick with unique usernames.
Create an account or sign in to comment