I-AM-OBODO Posted July 23, 2015 Share Posted July 23, 2015 Hi all. I generated a random number and assigned it a variable to be used through out the session but on getting to the next page, the value changes. It is regenerating another number which isnt the intention. I have tried severally but no way! i really cannot figure out why the value changes in my second page. $loan = mt_rand(1000, 9999); $name = "John"; if(isset($_POST['continue'])){ $_SESSION['num'] = $number; $sql = ("INSERT INTO table (name, token_number) VALUES(:name, :token_number) $stmt=$pdo->prepare($sql); $stmt->execute(array( ':name'=>$name; ':token_number'=>$_SESSION['num'] )); if($stmt->rowCount()==1){ header("location: nextpage.php"); }else{ echo "Something went wrong"; } } Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted July 23, 2015 Share Posted July 23, 2015 Sounds to me you most likely need to wrap the code that generates the number in a condition, so it only generates the number when the session variable does not exist. Eg if(!isset($_SESSION['num'])) { // generate new random number } // use random number Quote Link to comment Share on other sites More sharing options...
I-AM-OBODO Posted July 23, 2015 Author Share Posted July 23, 2015 Sounds to me you most likely need to wrap the code that generates the number in a condition, so it only generates the number when the session variable does not exist. Eg if(!isset($_SESSION['num'])) { // generate new random number } // use random number Tried that but still didnt work. still regenerates number Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted July 23, 2015 Share Posted July 23, 2015 Have you made sure you are calling session_start() before the use $_SESSION's Quote Link to comment Share on other sites More sharing options...
I-AM-OBODO Posted July 23, 2015 Author Share Posted July 23, 2015 Have you made sure you are calling session_start() before the use $_SESSION's yes. i have other session value and all is working well except this one. funny enough there's no code generating function on the next page. but why does it regenerate? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted July 23, 2015 Share Posted July 23, 2015 Where are you generating the number and setting it the session and where on the next page are getting the number? Quote Link to comment Share on other sites More sharing options...
iarp Posted July 23, 2015 Share Posted July 23, 2015 The code you posted, is that copy and pasted right from your work or did you modify it for us? It's broken by the looks of it. Maybe it's throwing an error but redirecting to the other page before you see the message. Comment out the header and see what happens. Quote Link to comment Share on other sites More sharing options...
I-AM-OBODO Posted July 23, 2015 Author Share Posted July 23, 2015 There seems to be a little problem with my first post but the problem still lingers.I generate the code on the first page so that it can pass on the value generated to the second page.here's a modification of my code //page one session_start(); //generate the number // i even tried $_SESSION['number'] = mt_rand(1000, 9999); but to no avail $number = mt_rand(1000, 9999); $name = "John Doe"; echo $name; echo "<br>"; echo "Your tally nuber is: ". $number ; if(isset($_POST['submit'])){ //database insert stuff $_SESSION['number'] = $number; header("location: nextpage.php"); }else{ echo "could not complete action"; } //html form cos <form method="post" action=""> <input name="submit" type="submit" value="NEXT" > </form> //page two session_start(); echo $_SESSION['number']; //but it regenerates another number Quote Link to comment Share on other sites More sharing options...
I-AM-OBODO Posted July 23, 2015 Author Share Posted July 23, 2015 The code you posted, is that copy and pasted right from your work or did you modify it for us? It's broken by the looks of it. Maybe it's throwing an error but redirecting to the other page before you see the message. Comment out the header and see what happens. i doubt there's any error Quote Link to comment Share on other sites More sharing options...
I-AM-OBODO Posted July 23, 2015 Author Share Posted July 23, 2015 Where are you generating the number and setting it the session and where on the next page are getting the number? i'm generating the number on the first page, see new code Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted July 24, 2015 Share Posted July 24, 2015 Try this for page one <?php //page one session_start(); if(isset($_POST['submit'])){ $_SESSION['number'] = mt_rand(1000, 9999); //database insert stuff header("location: nextpage.php?session=".$_SESSION['number']."&name=".trim($_POST['name'])); exit; } ?> <form method="post" action=""> <input name="name" type="text" value="" > <input name="submit" type="submit" value="NEXT" > </form> Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted July 24, 2015 Share Posted July 24, 2015 (edited) Changed it around a little more. page one <?php session_start(); if(isset($_POST['submit']) && trim($_POST['name']) != ""){ if(!isset($_SESSION['number'])){ $_SESSION['number'] = session_id(); } if(!isset($_SESSION['name'])){ $_SESSION['name'] = trim($_POST['name']); } //database insert stuff header("location: nextpage.php"); exit; }else{ echo "Enter your name"; } ?> <form method="post" action=""> <input name="name" type="text" value="" > <input name="submit" type="submit" value="NEXT" > </form> page two <?php session_start(); echo "session: ".$_SESSION['number']."<br />"; echo "name: ".$_SESSION['name']; ?> Edited July 24, 2015 by QuickOldCar Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted July 24, 2015 Share Posted July 24, 2015 For the record, you can use the session_id() for that random number. Quote Link to comment Share on other sites More sharing options...
Torrie Posted July 24, 2015 Share Posted July 24, 2015 (edited) header("location: nextpage.php"); [.....truncated] <form method="post" action=""> Quick question: how come you did it that way -- with the header: location next page, instead of the classic way of "<form action="nextpage.php"> I was always taught to use <form action="thepage_I_want.php">. Is this a newer, better way of doing things? Edited July 24, 2015 by Torrie Quote Link to comment Share on other sites More sharing options...
Solution I-AM-OBODO Posted July 24, 2015 Author Solution Share Posted July 24, 2015 (edited) Thanks all. Finally i got it to work. I noticed that the point where the regenerating starts is not the next page but on the first page (reason i don't understand though) I had to create a hidden field and assign the random number to it and then i did an insert with the post value and after that reassigned the value to a session. Viola! it did the magic! PS: The code shown is not the complete code but the modified code/logic that was giving me trouble and that worked the trouble. Here's my code $number = mt_rand(1000, 9999); $name = "John Doe"; echo $name; echo "<br>"; echo "Your tally nuber is: ". $number ; if(isset($_POST['submit'])){ //database insert stuff $_SESSION['numb'] = $_POST['numb']; header("location: nextpage.php"); }else{ echo "could not complete action"; } //html form <form method="post" action=""> <input name="numb" type="hidden" value="<?php echo $number; ?>"> <input name="submit" type="submit" value="NEXT" > </form> //page two session_start(); $number = $_SESSION['numb']; echo $number; Edited July 24, 2015 by Mr-Chidi 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.