Jump to content

Adding to database?


gk20

Recommended Posts

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!!!
Link to comment
https://forums.phpfreaks.com/topic/5035-adding-to-database/
Share on other sites

[!--quoteo(post=355333:date=Mar 15 2006, 08:10 AM:name=gk20)--][div class=\'quotetop\']QUOTE(gk20 @ Mar 15 2006, 08:10 AM) [snapback]355333[/snapback][/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.
Link to comment
https://forums.phpfreaks.com/topic/5035-adding-to-database/#findComment-17807
Share on other sites

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!!!
Link to comment
https://forums.phpfreaks.com/topic/5035-adding-to-database/#findComment-17842
Share on other sites

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.


[code]
<?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();
}
}
}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/5035-adding-to-database/#findComment-17886
Share on other sites


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

GOOD LUCK.
[code]
<?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
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/5035-adding-to-database/#findComment-17891
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.