CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   PHP login redirect to id specific page (http://www.codingforums.com/showthread.php?t=288175)

Foster 02-23-2013 10:35 AM

PHP login redirect to id specific page
 
Hi all,

I am building a website at the moment and would like to redirect users to a page that displays the information relating to their id when they log in. I have the login script already I just cant get it to go to the right page.

Foster

Foster 02-23-2013 12:55 PM

I actually resolved the above issue. I now need to make it so the user can edit the information they are seeing.

Any help if appreciated.

Foster

Celtboy 02-24-2013 01:32 AM

We can't really help you with such generic information, but the basic strategy is this:

- Store current user info in session
- Provide a link to an edit page
- Edit page polls database info, plugs it into existing form fields
- User hits "Update" and the info gets pushed back into the database, where database table info user id = session user id.

Foster 02-24-2013 11:56 AM

Hi,

Thanks for your response.

- Store current user info in session - This I have done
- Provide a link to an edit page - This I have done
- Edit page polls database info, plugs it into existing form fields - This I don't know how to do!
- User hits "Update" and the info gets pushed back into the database, where database table info user id = session user id.

I can get the page to poll the database info and show it to the specific user but not allow them to edit it.

Foster

nani_nisha06 02-24-2013 03:18 PM

Quote:

Originally Posted by Foster (Post 1315592)
Hi,

Thanks for your response.

- Store current user info in session - This I have done
- Provide a link to an edit page - This I have done
- Edit page polls database info, plugs it into existing form fields - This I don't know how to do!
- User hits "Update" and the info gets pushed back into the database, where database table info user id = session user id.

I can get the page to poll the database info and show it to the specific user but not allow them to edit it.

Foster

Foster,

If i have understood your above requirement correctly your looking for below information else ignore.

edit page is nothing like copy your original form page in new file and name it as edit.php?edit=edit

Now select table from db as an array, using while condition echo its value in the respective input field as VALUE (Do not change the name at any cost but just add value="" to input filed) , For example check below.

PHP Code:

<input type="checkbox" name="check_list" value="<? echo $row['Report ID'?>">

do it for every input field and send back information to DB via same back end script you used while creating the record & create one if condition in back end let say if(edit=="edit") use UPDATE command else use INSERT command so, one back end script act good for create & edit data.

but only effort you need to do is updating "VALUES" as per input types in the edit.php?edit=edit page.

Let me know if you need any more information.

Regards,
Nani :cool:

Foster 02-24-2013 08:51 PM

How would I get it to connect to my database? This is my code as it stands.

PHP Code:

<?php

// Inialize session
session_start();

// Check, if username session is NOT set then this page will jump to login page
if (!isset($_SESSION['Comname'])) {
header('Location: login.html');
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="style.css" rel="stylesheet" type="text/css" />
<title>Imperial Army Update Form</title>
</head>


<body>
<div id="container">
  <div id="header">
        
    </div>
    <div id="content">
        <div id="left">
            <div class="menu">
                <div class="menuheader"><h3>Menu</h3></div>
                <div class="menucontent">
                    <ul>
                        <li><a href="Impguard.html">Imperial Army Registration</a></li>
                        <li><a href="regiments.html">View Active Regiments</a></li>
                        <li><a href="igedit.html">Edit a Regiment</a></li>
                        <li><a href="ArmyGen.html">Create a Regiment</a></li>
                        <li><a href="login.html">LogIn/LogOut</a></li>
                        <li><a href="myreg.html">My Regiment</a></li>
                    </ul>
                </div>
                <div class="menufooter"></div>
            </div>
            
        
        
            
        </div>
        
        <div id="middle">
            <div class="post">
                <div class="postheader">
                  <h1>Imperial Guard Army Registration</h1></div>
                <div class="postcontent">
                    <!-- Begin Content -->
<form action="update.php" method="post">
<center>

<table class="table"><tr><td background="
" colspan="2"><font><b>Commanders Information</b></td></tr>
<tr><td width="20%"><font>Commanders Name:&nbsp;&nbsp;&nbsp;</td>
    <td width="80%"><font><INPUT TYPE="text" NAME="Comname" size="50%" value=""></td></tr>

    <tr><td><font>Password:&nbsp;&nbsp;&nbsp;</td>
    <td><font><INPUT TYPE="password" NAME="password" size="50%" value=""></td></tr>
    <tr><td colspan="2"></td></tr>
</table><BR><BR>

<table class="table"><tr><td background="/~newreno/images/background2.gif" colspan="2"><font><b>Imperial Guard Regiment Information</b></td></tr>
<tr>
    <td width="20%"><font> Regiment Name:&nbsp;&nbsp;&nbsp;</td>
    <td width="80%"><INPUT TYPE="text" NAME="name" size="50%" value="<? echo $row['name'?>"></td></tr>
</table>
<BR><BR>


<BR><BR>
<table class="table"><tr><td background="/~newreno/images/background2.gif"><center><font><b>Historical Figures</b></td></tr>
<tr><td align="center"><TEXTAREA NAME="HistoricalFigures" ROWS="10" COLS="100" value="<? echo $row['HistoricalFigures'?>"></TEXTAREA></td></tr></table>
<BR><BR>
<table class="table"><tr><td background="/~newreno/images/background2.gif"><center><font><b>Notable Campaigns</b></td></tr>
<tr><td align="center"><TEXTAREA NAME="campaigns" ROWS="10" COLS="100" value="<? echo $row['campaigns'?>">></TEXTAREA></td></tr></table>

    

<font>
<b>STOP!</b><br>
Please check your information before pressing the submit button below. Looking over your information now will make sure editing later won't be necessary. Thank you.<BR>
<input type="hidden" name="FormSubmit" value="Yes">
<INPUT TYPE="submit" value="Update Regiment"> &nbsp; <INPUT TYPE="reset" value="Reset Fields">
</td></tr></table>

<!--  End Content  -->
          </div><div class="post"><div class="postcontent">
                </div>
                <div class="postfooter"></div>
            </div>
        </div>
    </div>
    <div id="footer">
            </div>    
</div>
</body>
</html>


Celtboy 02-25-2013 04:29 AM

Well I can't give you a verbatim set of directions because I don't know your database schema or what you're using to access it (Native methods, an ORM, PDO, etc), but... I've tossed in some pseudo code and removed irrelevant parts and replaced them with "..."

PHP Code:


<?php

// Inialize session
session_start();

// Check, if username session is NOT set then this page will jump to login page
if (!isset($_SESSION['Comname'])) {
   
header('Location: login.html');
} else {
   
// Get Current User ID from $_SESSION

   // query your database


   // get an array/object with appropriate values
   // e.g.
   //    $user->regiment_name = mysqlres["name"];
   //    $user->commander_name = mysqlres["CommanderName"];
   //    $user->historical = mysqlres["HistoricalFigures"];
   //    etc


   // you've got the right idea for your Regiment Name. You won't be looping 
   // through a set though (with multiple rows) because you only have a single 
   // row returned from your query
   //
   // *NB* for <TEXTAREA>, you don't set the 'value' attribute, you just put the data between the open and closing tags.

}

?>
...

                <div class="postcontent">
                    <!-- Begin Content -->
<form action="update.php" method="post">
<center>

<table class="table"><tr><td background="
" colspan="2"><font><b>Commanders Information</b></td></tr>
<tr><td width="20%"><font>Commanders Name:&nbsp;&nbsp;&nbsp;</td>
    <td width="80%"><font><INPUT TYPE="text" NAME="Comname" size="50%" value="<?=$user->commander_name?>"></td></tr>

    <tr><td><font>Password:&nbsp;&nbsp;&nbsp;</td>
    <td><font><INPUT TYPE="password" NAME="password" size="50%" value=""></td></tr>
    <tr><td colspan="2"></td></tr>
</table><BR><BR>

<table class="table"><tr><td background="/~newreno/images/background2.gif" colspan="2"><font><b>Imperial Guard Regiment Information</b></td></tr>
<tr>
    <td width="20%"><font> Regiment Name:&nbsp;&nbsp;&nbsp;</td>
    <td width="80%"><INPUT TYPE="text" NAME="name" size="50%" value="<?=$user->regiment_name?>"></td></tr>
</table>
<BR><BR>


<BR><BR>
<table class="table"><tr><td background="/~newreno/images/background2.gif"><center><font><b>Historical Figures</b></td></tr>
<tr><td align="center"><TEXTAREA NAME="HistoricalFigures" ROWS="10" COLS="100"><?= $user->historical ?></TEXTAREA></td></tr></table>
<BR><BR>
<table class="table"><tr><td background="/~newreno/images/background2.gif"><center><font><b>Notable Campaigns</b></td></tr>
<tr><td align="center"><TEXTAREA NAME="campaigns" ROWS="10" COLS="100" value="<? echo $row['campaigns'?>">></TEXTAREA></td></tr></table>

    

<font>
<b>STOP!</b><br>
Please check your information before pressing the submit button below. Looking over your information now will make sure editing later won't be necessary. Thank you.<BR>
<input type="hidden" name="FormSubmit" value="Yes">
<INPUT TYPE="submit" value="Update Regiment"> &nbsp; <INPUT TYPE="reset" value="Reset Fields">
</td></tr></table>

<!--  End Content  -->
      ...


Celtboy 02-25-2013 04:33 AM

then, for your update.php,
Code:

// Query through your form data
// if (isset($_POST["fieldx"])) {
//      update database
// }

or:
Code:

$myFields = array("name","regiment_name","Comname","HistoricalFigures","campaigns");
foreach ($myFields as $field) {
  if ( (isset($_POST[$field]) && $($_POST[$field] != '') ) {
      //update database
  }
}


Foster 02-25-2013 09:40 PM

Ok, I've got it working to populate the text box based on a specific id. I now need to do it based on the data stored from a session.

Arcticwarrio 02-26-2013 06:48 AM

in your login script try setting the page id there instead:
PHP Code:

$_SESSION['Username'] = $Username;
            
setcookie("Username"$Username,time()+60*60*24*30);
            
$_SESSION['UserID'] = $row['UserID'];
            
$_SESSION['logged_in'] = true

then something like

PHP Code:


header
('Location: mainpage.php?ID=' $_SESSION['UserID']); 


Foster 02-26-2013 07:18 AM

Hi Arcticwarrio,

I have that already it's when selecting the database that I have the problem. The code below selects ID 1 but I want it to select according the the username of the person logged in.

PHP Code:

$result mysqli_query($con,"SELECT * FROM IGarmy WHERE id = 1"); 


Arcticwarrio 02-26-2013 07:20 AM

do users have an auto incremental id in the database?

Arcticwarrio 02-26-2013 07:23 AM

if so it would simply be:

PHP Code:

$result mysqli_query($con,"SELECT * FROM IGarmy WHERE id = ".$_SESSION['UserID']); 


Foster 02-26-2013 08:19 AM

What if I have it store the username in the session and not the UserID?

Arcticwarrio 02-26-2013 08:30 AM

Quote:

Originally Posted by Foster (Post 1316001)
What if I have it store the username in the session and not the UserID?

PHP Code:

$result mysqli_query($con,"SELECT * FROM IGarmy WHERE username = ".$_SESSION['Username']); 



All times are GMT +1. The time now is 05:04 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.