Jump to content


Photo

Adding to database?


  • Please log in to reply
4 replies to this topic

#1 gk20

gk20
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 15 March 2006 - 01:10 PM

I have a php file where I allow users that are logged in to add to the database.I am using session_start() and session_checker() to see of the user is logged in before allowing them access to this page. That works fine. But when I add their info to the database I also want to add their username with it, to associate this informtion with them without asking them for it again??? Is there anyway I could use their loginname from login.php in the placead.php file?
Heres login.php

$login = $_POST['login'];
$pass = $_POST['pass'];
...
...
session_register('login');
$_SESSION['loginname'] = $login;

session_register('pass');
$_SESSION['password'] = $pass;

heres placead.php

$itemname = $_POST['itemname'];
$description = $_POST['description'];
$category = $_POST['category'];
$cost = $_POST['cost'];
$location = $_POST['location'];

when I'm storing these values can I how can I store the username of that person with the information??
Please help, I've tried looking in to sessions, but I can't seem to understand it!!!

#2 komquat

komquat
  • Members
  • PipPipPip
  • Advanced Member
  • 130 posts
  • LocationHoughton, MI

Posted 15 March 2006 - 01:50 PM

[!--quoteo(post=355333:date=Mar 15 2006, 08:10 AM:name=gk20)--][div class=\'quotetop\']QUOTE(gk20 @ Mar 15 2006, 08:10 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I have a php file where I allow users that are logged in to add to the database.I am using session_start() and session_checker() to see of the user is logged in before allowing them access to this page. That works fine. But when I add their info to the database I also want to add their username with it, to associate this informtion with them without asking them for it again??? Is there anyway I could use their loginname from login.php in the placead.php file?
Heres login.php

$login = $_POST['login'];
$pass = $_POST['pass'];
...
...
session_register('login');
$_SESSION['loginname'] = $login;

session_register('pass');
$_SESSION['password'] = $pass;

heres placead.php

$itemname = $_POST['itemname'];
$description = $_POST['description'];
$category = $_POST['category'];
$cost = $_POST['cost'];
$location = $_POST['location'];

when I'm storing these values can I how can I store the username of that person with the information??
Please help, I've tried looking in to sessions, but I can't seem to understand it!!!
[/quote]

You start placead.php with

Session_start()

Then you can call up the variable $_SESSION['loginname'] anytime after that and it will give you that value.

#3 gk20

gk20
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 15 March 2006 - 04:08 PM

Sorry, I have tried this. In my placead.php file i have this at top

<?php include 'db.inc.php'; ?>
<?php
session_start();
session_checker();
?>
.......
.......
session_register('loginname');
$itemowner = $_SESSION['loginname'];

so when I'm adding to database: insert into item set ..... itemowner = '$itemowner'
but it still leaves itemowner field blank!

would it have anything to do with db.inc.php file??
......
function session_checker()
{
if(!session_is_registered('loginname'))
{
if(!session_is_registered('loginpass'))
{
echo "<p>You must be logged in to view this page</p>";
echo "<b><br><br/><a href=login.php?loginuser=1>Login</a></b>";
exit();
}
}
}

Its really bugging me, it should be easier!!!


#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 15 March 2006 - 06:13 PM

Try this way ok.


If session_start () is not at the very top of a page with no text above it should work, i say agin no text above it.

Also chage this ok
include ('db.inc.php');

To this save page to database.php
include ('database.php');




good luck.


<?php session_start();


session_checker();

// this needs to be saved as database.php not what you had.
include ('database.php');


//session_register('loginname');
$itemowner = $_SESSION['loginname'];

//so when I'm adding to database: insert into item set ..... 
$itemowner = '$itemowner' 
//but it still leaves itemowner field blank!

//would it have anything to do with db.inc.php file??

function session_checker()
{ 
if(!session_is_registered('loginname'))
{
if(!session_is_registered('loginpass')) 
{ 
echo "<p>You must be logged in to view this page</p>";
echo "<b><br><br/><a href=login.php?loginuser=1>Login</a></b>"; 
exit(); 
}
}
}
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 15 March 2006 - 06:29 PM


PHP FREAKS EXAMPLE FOR YOU
JUST CHANGE THE DATABASE TO YOUR SETTINGS OK

GOOD LUCK.
<?php 


################ 
#  Simple login session          
#  Ben M Wasley          
#  BenWConsulting.com    
################ 

function login($statment) 
    { 
print" <form action="myscript.php" method="POST">n"; 
print"<div align="center"><b>".$statment."</b></div><br />n"; 

    //Your login vars user &amp;amp; password need to be passed no form tags though look above &amp;amp; below 

    //Close form 
print"</form>n"; 
    } 

//Duh start the session redundant comments are funny 
session_start(); 

if(!isset($user) | !isset($password)) 
                 { 
//Login message here 

$statment = "Hi, Please login below"; 
login($statment); 
exit(); 
} 

session_register("user"); 
session_register("password"); 

//However you connect to your DB 
include("db.php"); 

$sql = mysql_query("SELECT user, password FROM users WHERE user = '$user'"); 
$result = mysql_fetch_array($sql)or die ("Doh! no DB Connection"); 
$numrows = mysql_num_rows($sql); 

if($numrows != "0" &amp; $password == $result["password"]) 
            { 
            $valid_user = 1; 
            } 
            else 
            { 
            $valid_user = 0; 
            } 
mysql_close($db); 

if (!($valid_user)) 
   { 

//Boot em!!!  IF !valid 
session_unset(); 
session_destroy(); 



//Give an error message 
$statment = "Somthing is wrong with the info you submitted.n"; 

login($statment); 

//Stop script 
exit(); 

  } 
//Include session_start(); on the top of all pages or in your header.php include 
session_start(); 

//Wecome to the the protected area!!! Put HTML, include, whatever you want here. 

//Session will end when client closes browser. 
//If you want then to have the ability to logout use: 

//     --->           session_start(); 
//     --->           session_unset(); 
//     --->           session_destroy(); 
//@ the top of any page or as a seperate script i.e logout.php 
?> 

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users