sanfordss Posted January 14, 2009 Share Posted January 14, 2009 I'm building my first webpage with PHP and have finally ran into an issue I can't find a solution for. When submitting a form from a user, I want to also submit the currently logged in userid into the record. A session is created when the user logs in Can someone shed some light on this? Attached is the code I think you need to see. Let me know if you need more. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/140848-solved-inserting-current-user-into-record-when-form-is-submitted/ Share on other sites More sharing options...
rhodesa Posted January 14, 2009 Share Posted January 14, 2009 haven't looked at the code, but you shouldn't have to submit the current user, as the page you submit to can just start the session up and see who is logged in. putting it in a hidden input is actually a HORRIBLE idea, as anyone can just change that value before submitting and change data for a different user Quote Link to comment https://forums.phpfreaks.com/topic/140848-solved-inserting-current-user-into-record-when-form-is-submitted/#findComment-737211 Share on other sites More sharing options...
sanfordss Posted January 14, 2009 Author Share Posted January 14, 2009 I have a form that the user submits to create a record. I need to have a field that stores which username created the record. It would need to be something where when the form is submitted, the logged in user name gets stored with the other info in the record. Quote Link to comment https://forums.phpfreaks.com/topic/140848-solved-inserting-current-user-into-record-when-form-is-submitted/#findComment-737218 Share on other sites More sharing options...
premiso Posted January 14, 2009 Share Posted January 14, 2009 Use sessions to store the username in/retrieve it. As rhoodesa says, it is a horrible idea since forms are easy to manipulate the data. Quote Link to comment https://forums.phpfreaks.com/topic/140848-solved-inserting-current-user-into-record-when-form-is-submitted/#findComment-737219 Share on other sites More sharing options...
rhodesa Posted January 14, 2009 Share Posted January 14, 2009 A session is created when the user logs in. i assume you put the username (or at least the user id) in the session when they login? retrieve that info when processing the data. make a column in your table for keeping the username (or user id) and add that value to your insert statement Quote Link to comment https://forums.phpfreaks.com/topic/140848-solved-inserting-current-user-into-record-when-form-is-submitted/#findComment-737221 Share on other sites More sharing options...
sanfordss Posted January 14, 2009 Author Share Posted January 14, 2009 I do have the user name being stored in the session. I'm just not sure exactly how to get it from the session data (syntax). I have this in storing the form data. Do I need to set a session variable or is this the right (or wrong) idea? @$pfw_strQuery = "INSERT INTO `rrequest`(`vmake`,`vmodel`,`vin`,`mileage`,`probdescrip`,`userid`)VALUES (\"$Vmake\",\"$vmodel\",\"$vin\",\"$mileage\",\"$probdescrip\",\SESSIONS())" ; Quote Link to comment https://forums.phpfreaks.com/topic/140848-solved-inserting-current-user-into-record-when-form-is-submitted/#findComment-737228 Share on other sites More sharing options...
premiso Posted January 14, 2009 Share Posted January 14, 2009 (\"$Vmake\",\"$vmodel\",\"$vin\",\"$mileage\",\"$probdescrip\",\" . $_SESSION['userid'] . ")" ; Also for queries use ' insetad of " around the value portion. Quote Link to comment https://forums.phpfreaks.com/topic/140848-solved-inserting-current-user-into-record-when-form-is-submitted/#findComment-737234 Share on other sites More sharing options...
rhodesa Posted January 14, 2009 Share Posted January 14, 2009 What key is the username stored in? aka, how did you store the username in the session? if you used 'userid', it would be like so: <?php session_start(); //this goes at the top of the script $userid = mysql_real_escape_string($_SESSION['userid']); @$pfw_strQuery = "INSERT INTO `rrequest`(`vmake`,`vmodel`,`vin`,`mileage`,`probdescrip`,`userid`) VALUES ('$Vmake','$vmodel','$vin','$mileage','$probdescrip','$userid')"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/140848-solved-inserting-current-user-into-record-when-form-is-submitted/#findComment-737238 Share on other sites More sharing options...
sanfordss Posted January 14, 2009 Author Share Posted January 14, 2009 I'm assuming one of these is the key it was saved in. Either logname or fusername. $_SESSION['auth']="yes"; $_SESSION['logname'] = $_POST['fusername']; Quote Link to comment https://forums.phpfreaks.com/topic/140848-solved-inserting-current-user-into-record-when-form-is-submitted/#findComment-737246 Share on other sites More sharing options...
sanfordss Posted January 14, 2009 Author Share Posted January 14, 2009 rhodesa, premiso, you ARE PHP GODS! It works. Thanks a ton!!! Quote Link to comment https://forums.phpfreaks.com/topic/140848-solved-inserting-current-user-into-record-when-form-is-submitted/#findComment-737284 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.