herghost Posted April 12, 2009 Share Posted April 12, 2009 How would I put the new data into sessions? <?php //Start session session_start(); //Include database connection details require_once('include/database.php'); //Array to store validation errors $errmsg_arr = array(); //Validation error flag $errflag = false; //Function to sanitize values received from the form. Prevents SQL injection function clean($str) { $str = @trim($str); if(get_magic_quotes_gpc()) { $str = stripslashes($str); } return mysql_real_escape_string($str); } //Sanitize the POST values $banddataid = $_SESSION['SESS_USERID']; $bandname = $_SESSION['SESS_BANDNAME']; $genre = clean($_POST['genre']); $formed = clean($_POST['formed']); $member0 = clean($_POST['member0']); $member1 = clean($_POST['member1']); $member2 = clean($_POST['member2']); $member3 = clean($_POST['member3']); $member4 = clean($_POST['member4']); $member5 = clean($_POST['member5']); $position0 = clean($_POST['position0']); $position1 = clean($_POST['position1']); $position2 = clean($_POST['position2']); $position3 = clean($_POST['position3']); $position4 = clean($_POST['position4']); $position5 = clean($_POST['position5']); //Input Validations if($formed == '') { $errmsg_arr[] = 'Year Formed is Missing'; $errflag = true; } $sql = mysql_query("SELECT * FROM banddata WHERE userid = '$banddataid'"); if(mysql_num_rows($sql) == 0) { //Create INSERT query $qry = "INSERT INTO banddata (userid, bandname, genre, formed, position0, member0, position1, member1, position2, member2, position3, member3, position4, member4, position5, member5) VALUES ('$banddataid','$bandname','$genre', '$formed', '$position0', '$member0','$position1', '$member1', '$position2', '$member2','$position3', '$member3','$position4', '$member4', '$position5', '$member5')"; } else { //Create update query $qry = "UPDATE banddata SET banddataid = '$banddataid, bandname = '$bandname', genre = '$genre', formed = '$formed', position0 = '$position0', member0 ='$member0', position1 = '$position1', member1= '$member1', position2 = '$position2', member2 = '$member2', position3 = '$position3', member3 ='$member3', position4 = '$position4', member4 ='$member4', position5 = '$position5', member5 = '$member5' WHERE userid = '$banddataid'"; } $result = mysql_query($qry); //Check whether the query was successful or not if($result) { header("location: member_home.php"); exit(); }else { die(mysql_error()); } ?> I want to create a session to store the following banddataid = '$banddataid, genre formed position0 member0 position1 member1 position2 member2 position3 member3 position4 member4 position5 member5 Link to comment https://forums.phpfreaks.com/topic/153747-solved-creating-sessions/ Share on other sites More sharing options...
wildteen88 Posted April 12, 2009 Share Posted April 12, 2009 Use the $_SESSION superglobal array to add/retrieve data to/from your session Assign new session variable $_SESSION]'new_var'] = 'new value'; // OR $_SESSION]'new_var'] = $some_var; Retrieve session variable $some_var = $_SESSION]'new_var']; Just make sure you call session_start(); at the top of each page which uses sessions. Link to comment https://forums.phpfreaks.com/topic/153747-solved-creating-sessions/#findComment-807987 Share on other sites More sharing options...
herghost Posted April 12, 2009 Author Share Posted April 12, 2009 Thanks mate, so something like this? $_SESSION['SESS_GENRE'] = 'genre'; where would I place this in my code? I am assuming before the header redirect! Link to comment https://forums.phpfreaks.com/topic/153747-solved-creating-sessions/#findComment-807992 Share on other sites More sharing options...
wildteen88 Posted April 12, 2009 Share Posted April 12, 2009 where would I place this in my code? How would I know where to place that code. You've not explained what you're trying to do/what your script does. I am assuming before the header redirect! I guess. Link to comment https://forums.phpfreaks.com/topic/153747-solved-creating-sessions/#findComment-807998 Share on other sites More sharing options...
herghost Posted April 12, 2009 Author Share Posted April 12, 2009 Sorry mate, its a form that updates fields in a table, and then redirects back to a page, I want to store this updated fields in a session, is that enough info? Link to comment https://forums.phpfreaks.com/topic/153747-solved-creating-sessions/#findComment-808011 Share on other sites More sharing options...
wildteen88 Posted April 12, 2009 Share Posted April 12, 2009 If its all the form data ($_POST) then you could do $_SESSION = $_POST However this will remove existing session variables. Otherwise you can do something like the following // list all field you want to be added to your session $fields = array('genre', 'formed', 'position0', 'member0' /* etc */); // here we define the session variable for each field foreach($fields as $var) { if(isset($_POST[$var])) $_SESSION[$var] = $_POST[$var]; } Place this code before you call header(); Link to comment https://forums.phpfreaks.com/topic/153747-solved-creating-sessions/#findComment-808025 Share on other sites More sharing options...
herghost Posted April 12, 2009 Author Share Posted April 12, 2009 Thanks mate, Am I right in saying to then echo one of these sessions I would just call it by what ever its called? Ie the var for genre woudl be genre so I would just echo <?php echo $_SESSION['genre'];?><br> Link to comment https://forums.phpfreaks.com/topic/153747-solved-creating-sessions/#findComment-808043 Share on other sites More sharing options...
wildteen88 Posted April 12, 2009 Share Posted April 12, 2009 Correct. Link to comment https://forums.phpfreaks.com/topic/153747-solved-creating-sessions/#findComment-808044 Share on other sites More sharing options...
herghost Posted April 12, 2009 Author Share Posted April 12, 2009 Many Thanks! Works perfectly Link to comment https://forums.phpfreaks.com/topic/153747-solved-creating-sessions/#findComment-808227 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.