Jump to content

Recommended Posts

Hi all following is my code. In this from the page 1 I'm trying to store data in the database and it will go to the next page which will store data and redirect user back to the page 1. But when user gets redirected it asks to enter the username & password again. I think I'm not able to manage session. Please Help me!!

 

Page1.php

 

<?php


       $username = $_POST['uname'];
       $password = $_POST['pass'];
       $_SESSION['uname'] = $username;


       if ($username && $password) {

           $con = mysql_connect("localhost", "root", "pranit") or die("Couldn't Connect");
           mysql_select_db("rk_bookings") or die("Can't select DB");
           $query = mysql_query("SELECT * FROM users WHERE uname = '$username'");
           $numrows = mysql_num_rows($query);


           if ($numrows != 0) {
               while ($row = mysql_fetch_assoc($query)) {
                   $dbusername = $row['uname'];
                   $dbpassword = $row['pass'];
               }


               if ($username == $dbusername && $password == $dbpassword) {

                   ?>
                   <html>
                       <head>
                           <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                           <title></title>
                           <link rel="stylesheet" href="main.css" type="text/css">
                       </head>
                       <body>
                       <center>                            
                           <div id="store">
                               <p><img src="media/rk_logo2.png" height="150" width="320"></p>
                               <form action="storeSeat.php" method="post">                                    
                                   <table>
                                       <tr>
                                           <th>Enter Date</th>
                                           <td><input type="text" name="ddate" id="ddate"></td>
                                       </tr>
                                       <tr>
                                           <th>Company</th>
                                           <td><input type="text" name="company" value="Patel"></td>
                                       </tr>
                                       <tr>
                                           <th>Amenities</th>
                                           <td><input type="text" name="aminities" value="Seating, NON AC"></td>
                                       </tr>
                                       <tr>
                                           <th>Depart</th>
                                           <td><input type="text" name="depart" value="11:30"></td>
                                       </tr>
                                       <tr>
                                           <th>Arrive</th>
                                           <td><input type="text" name="arrive" value="06:00"></td>
                                       </tr>
                                       <tr>
                                           <th>Duration</th>
                                           <td><input type="text" name="duration" value="06:30"></td> 
                                       </tr>
                                       <tr>
                                           <th>Price</th>
                                           <td><input type="text" name="price" value="300"></td>
                                       </tr>
                                       <tr>
                                           <th>Start</th>
                                           <td><input type="text" name="start" value="Malegaon"></td>
                                       </tr>
                                       <tr>
                                           <th>End</th>
                                           <td><input type="text" name="end" value="Pune"></td>
                                       </tr>
                                       <tr>
                                           <th></th>
                                           <td><button type="submit" name="store">Store</button></td>
                                       </tr> 
                                   </table>                                                                        
                               </form>            
                           </div>                        


                   <?php
               }
               else
                   echo "<b style='color:white; font-size:60px; font-family:sans-serif;'>Incorrect Password</b>";
           }
           else
               die("That User Doesn't exist");
       }
       else
           die("Please Enter Username or password");
       ?>

 

Page 2.php

 

?php


session_start();
$host = "localhost";
$uname = "root";
$pass = "pranit";
$db_name = "rk_bookings";
$tbl_name = "bus_details";
$company = $_POST['company'];
$amenities = $_POST['aminities'];
$deaprt = $_POST['depart'];
$arrive = $_POST['arrive'];
$duration = $_POST['duration'];
$price = $_POST['price'];
$start = $_POST['start'];
$destination = $_POST['end'];


if (isset($_SESSION['uname'])) {

   mysql_connect("$host", "$uname", "$pass") or die("cannot connect");
   mysql_select_db("$db_name") or die("cannot select DB");
   $seat_no = 1;
   $no_of_times = 1;
   $url = "Location: login.php";
   echo "User : " . $_SESSION['uname'];
   do {
       $sql = "insert into $tbl_name(company, aminities, depart, arrive, duration, price, start, destination, seat_no) values ('$company','$amenities','$deaprt','$arrive','$duration','$price','$start','$destination',$seat_no)";
       $result = mysql_query($sql);
       $seat_no++;
       $no_of_times++;
   } while ($no_of_times <= 42);


   if ($result) {
       echo "<b style = 'font-family: sans-serif; font-size: 20px; color: #00ADC5'>Thank you for your feedback / query. We will get back to you soon!</b>";
   } else {
       echo "Error" . mysql_error();
   }


   header($url);
}
?>

 

Please tell me where am going wrong?

Link to comment
https://forums.phpfreaks.com/topic/274714-need-help-managing-sessions/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.