zer0uk Posted March 8, 2020 Share Posted March 8, 2020 Hi Can anyone help please. I have a web site that users fill in data about rides and post the to a mysql db I am using $_SESSION to store the user that is logged in. what I want to do is any records posted need to posted with the username stored and also when I retrieve the record I only want to SELECT the records that the lodge in user has posted. below is the php page I am using to add records ... Really appreciate any help as I am stuck on how to go about doing this ... <?php // database connect include('../db_connect.php'); session_start(); // blank fields $var_course_name = $var_r_time = $var_r_date = $var_bike = $var_comments =''; $errors = array('course' => '', 'r_time' =>'', 'r_date' =>'', 'bike' =>'', 'comments' =>''); //-------- Check Data ----------- //Check if Data is sent & validate if(isset($_POST['name_submit'])){ if(empty($_POST['name_submit'])){ Echo 'No Course passed'; } else { $var_course_id = mysqli_real_escape_string($conn, $_POST['course_id']); $var_r_time = mysqli_real_escape_string($conn, $_POST['r_time']); $var_r_date = mysqli_real_escape_string($conn, $_POST['r_date']); $var_bike = mysqli_real_escape_string($conn, $_POST['bike']); $var_comments = mysqli_real_escape_string($conn, $_POST['comments']); //Create sql $sql = "INSERT INTO tbl_rides(course_id, r_time, r_date) VALUES ('$var_course_id', '$var_r_time', '$var_r_date')"; //Save to DB and check if(mysqli_query($conn, $sql)){ } else { echo 'query error: ' . mysqli_error($conn); } } } //end of checking ?> <!DOCTYPE html> <html> <head> </head> <!-- Compiled and minified CSS --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css"> <!-- Compiled and minified JavaScript --> <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script> <!--Import Google Icon Font--> <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> <?php if($_SESSION["name"]) { ?> <div class="grey" > Welcome <?php echo $_SESSION["name"]; ?> </div> <?php }else echo "<h1>Please login first .</h1>"; ?> <header> <title>My TimeKeeper - Log Race</title> <body bgcolor="#bdbdbd"> </header> <body> <nav class="nav-wraper grey "> <div class="container"> <a href="../index_logged_in.php" class="brand-logo"> My TimeKeeper <i class="material-icons right">watch_later</i></a> <a href="#" class="sidenav-trigger" data-target="mobile-links"> <i class="material-icons">menu</i> </a> <ul class="right hide-on-med-and-down"> <li><a href="../login/logout.php">Logout <i class="material-icons right">close</i> </a></li> <li><a href="#">Statistics <i class="material-icons right">trending_up</i> </a></li> <li><a href="#">Forum <i class="material-icons right">forum</i> </a></li> </ul> </div> </nav> <ul class="sidenav" id="mobile-links"> <li><a href="../login/logout.php">Logout <i class="material-icons right">close</i> </a></li> <li><a href="#">Statistics <i class="material-icons right">trending_up</i> </a></li> <li><a href="#">Forum <i class="material-icons right">forum</i> </a></li> </ul> <div class="container"> <form class="grey-text text-lighten-2" action="add.php" method="POST"> <div class="row"> <div class="centre"> <h2 class="grey-text text-lighten-2">Log a race</h2> </div> <br> <div class="input-field"> <i class="material-icons prefix">date_range</i> <input type="text" name="r_date" class="datepicker"> <label for="r_date">Choose the date you raced</label> </div> <div class="input-field"> <i class="material-icons prefix">watch_later</i> <input value="hh:mm:ss"type="time" name="r_time"> <label for="r_time">Please enter your race time</label> </div> <div class="input-field"> <i class="material-icons prefix">directions_bike</i> <input type="text" name="bike"> <label for="bike">Please enter the bike used</label> </div> <div class="input-field"> <i class="material-icons prefix">comment</i> <input type="text" name="comments"> <label for="comments">Please enter any comments</label> </div> <div> <label for="course_id">Choose the course raced:</label> <select name="course_id"> <?php // query to create course_id dropdown $resultset = $conn->query("SELECT course_id, course_name FROM tbl_courses"); while($row = $resultset->fetch_assoc()) { echo "<option value='{$row[course_id]}'>{$row['course_name']}</option>"; } ?> </select> <div> <input class="btn grey darken-2" type="submit" name="name_submit" value="submit"> </div> </div> </form> </div> <section class="container section" id="photo's"> <div class="row"> <div class="col s12 l14"> <image src="../images/tony_m.jpeg" alt="" class="responsive-img" height=1000 px> </div> </section> <!-- Javacript --> <script src=https://code.jquery.com/jquery-3.4.1.min.js></script> <!-- Compiled and minified JavaScript --> <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script> <script> $(document).ready(function(){ $('.sidenav').sidenav(); $('select').formSelect(); $('.datepicker').datepicker(); }); </script> </body> <footer> <div class=>© Copyright 2020 My TimeKeeper</div> <!-- Javacript --> <script src=https://code.jquery.com/jquery-3.4.1.min.js></script> <!-- Compiled and minified JavaScript --> <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script> <script> $(document).ready(function(){ $('.sidenav').sidenav(); }) </script> </footer> </html> Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted March 8, 2020 Share Posted March 8, 2020 data related to the user should use the user's id (auto-increment integer primary index from the 'user' table') to relate it back to the user it belongs to. this will result in the least amount of data storage, result in the fastest queries, and allow the user's information (name, username, ...) to be edited without breaking the relationship in the data. you would also store the user's id in the session variable, not the user's name/username, when the user logs in, as this will also support editing the user's information, in addition to supporting the current operation you are asking about. to get any of the other user's information, you would query for it on any page that needs it. tbl_rides needs a user_id column that you would store the $_SESSION['user_id'] value in. also, for the operation you are asking about, if the current visitor is not logged in, you would not display the form, nor run any of the form processing code. Quote Link to comment Share on other sites More sharing options...
zer0uk Posted March 8, 2020 Author Share Posted March 8, 2020 Thanks for your reply so tbl_rides has a column "username_id" which has a FK to a table tbl_accounts.id the table holds the id, username, password, email tbl_account was the table used for registration and $_SESSION using column username Quote Link to comment Share on other sites More sharing options...
zer0uk Posted March 9, 2020 Author Share Posted March 9, 2020 Figured this out now, was actually a lot easier than I thought .... learning 🙂 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.