Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-23-2013, 10:35 AM   PM User | #1
Foster
Regular Coder

 
Join Date: Nov 2011
Posts: 146
Thanks: 7
Thanked 1 Time in 1 Post
Foster has a little shameless behaviour in the past
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 is offline   Reply With Quote
Old 02-23-2013, 12:55 PM   PM User | #2
Foster
Regular Coder

 
Join Date: Nov 2011
Posts: 146
Thanks: 7
Thanked 1 Time in 1 Post
Foster has a little shameless behaviour in the past
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
Foster is offline   Reply With Quote
Old 02-24-2013, 01:32 AM   PM User | #3
Celtboy
Regular Coder

 
Join Date: May 2002
Location: Virginia, USA
Posts: 620
Thanks: 0
Thanked 6 Times in 6 Posts
Celtboy is an unknown quantity at this point
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.
Celtboy is offline   Reply With Quote
Old 02-24-2013, 11:56 AM   PM User | #4
Foster
Regular Coder

 
Join Date: Nov 2011
Posts: 146
Thanks: 7
Thanked 1 Time in 1 Post
Foster has a little shameless behaviour in the past
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 is offline   Reply With Quote
Old 02-24-2013, 03:18 PM   PM User | #5
nani_nisha06
Regular Coder

 
Join Date: Oct 2012
Location: mother land --india
Posts: 159
Thanks: 37
Thanked 2 Times in 2 Posts
nani_nisha06 is an unknown quantity at this point
Quote:
Originally Posted by Foster View Post
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
nani_nisha06 is offline   Reply With Quote
Old 02-24-2013, 08:51 PM   PM User | #6
Foster
Regular Coder

 
Join Date: Nov 2011
Posts: 146
Thanks: 7
Thanked 1 Time in 1 Post
Foster has a little shameless behaviour in the past
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>
Foster is offline   Reply With Quote
Old 02-25-2013, 04:29 AM   PM User | #7
Celtboy
Regular Coder

 
Join Date: May 2002
Location: Virginia, USA
Posts: 620
Thanks: 0
Thanked 6 Times in 6 Posts
Celtboy is an unknown quantity at this point
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 is offline   Reply With Quote
Old 02-25-2013, 04:33 AM   PM User | #8
Celtboy
Regular Coder

 
Join Date: May 2002
Location: Virginia, USA
Posts: 620
Thanks: 0
Thanked 6 Times in 6 Posts
Celtboy is an unknown quantity at this point
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
   }
}
Celtboy is offline   Reply With Quote
Old 02-25-2013, 09:40 PM   PM User | #9
Foster
Regular Coder

 
Join Date: Nov 2011
Posts: 146
Thanks: 7
Thanked 1 Time in 1 Post
Foster has a little shameless behaviour in the past
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.
Foster is offline   Reply With Quote
Old 02-26-2013, 06:48 AM   PM User | #10
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 597
Thanks: 15
Thanked 67 Times in 67 Posts
Arcticwarrio is on a distinguished road
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']); 
__________________
There are 10 types of people on CodingForums,
Those who understand Binary and those who dont.
Arcticwarrio is offline   Reply With Quote
Old 02-26-2013, 07:18 AM   PM User | #11
Foster
Regular Coder

 
Join Date: Nov 2011
Posts: 146
Thanks: 7
Thanked 1 Time in 1 Post
Foster has a little shameless behaviour in the past
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"); 
Foster is offline   Reply With Quote
Old 02-26-2013, 07:20 AM   PM User | #12
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 597
Thanks: 15
Thanked 67 Times in 67 Posts
Arcticwarrio is on a distinguished road
do users have an auto incremental id in the database?
__________________
There are 10 types of people on CodingForums,
Those who understand Binary and those who dont.
Arcticwarrio is offline   Reply With Quote
Old 02-26-2013, 07:23 AM   PM User | #13
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 597
Thanks: 15
Thanked 67 Times in 67 Posts
Arcticwarrio is on a distinguished road
if so it would simply be:

PHP Code:
$result mysqli_query($con,"SELECT * FROM IGarmy WHERE id = ".$_SESSION['UserID']); 
__________________
There are 10 types of people on CodingForums,
Those who understand Binary and those who dont.
Arcticwarrio is offline   Reply With Quote
Old 02-26-2013, 08:19 AM   PM User | #14
Foster
Regular Coder

 
Join Date: Nov 2011
Posts: 146
Thanks: 7
Thanked 1 Time in 1 Post
Foster has a little shameless behaviour in the past
What if I have it store the username in the session and not the UserID?
Foster is offline   Reply With Quote
Old 02-26-2013, 08:30 AM   PM User | #15
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 597
Thanks: 15
Thanked 67 Times in 67 Posts
Arcticwarrio is on a distinguished road
Quote:
Originally Posted by Foster View Post
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']); 
__________________
There are 10 types of people on CodingForums,
Those who understand Binary and those who dont.
Arcticwarrio is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:50 AM.


Advertisement
Log in to turn off these ads.