Hi all i have a basic php website where the user logs in and etc. Now i have a small form where when the user logs in they put in an article title and the article itself. Now this works fine, but i also want the users first name and surname along with another variable to go into the database.. as the article was submited by etc etc without the user having to fill the form in with their name whilst their logged in.. do you see what i mean? I have the following in place already. The form page where the session is working fine and is as follows: <? session_start(); if(!session_is_registered(myusername)){ header("location:wronglogin.php"); } ?> PHP: Below is the processing script where the form goes through: <?php // Make a MySQL Connection include "../config.php"; $submission_date = date("D M j Y G:i:s"); // Insert a row of information into the table "example" mysql_query("INSERT INTO article (firstname, surname, username, title, article, submission_date) VALUES('$firstname', '$surname', '$username', '$title', '$article', '$submission_date')") or die(mysql_error()); echo "Data Inserted!"; ?> PHP: Thats all. Now another thing, i sort of cheated so take a look below what i did with the form.. <form action="articleinsert.php" method="post"> title: <input type="text" name="title" /> article: <input type="text" name="article" /> **** THIS WORKS **** <input type="hidden" name="username" value="<?php echo "$myusername"; ?>" /> **** THIS DOSENT **** <input type="hidden" name="firstname" value="<?php echo "$firstname"; ?>" /> <input type="submit" name="register" value="submit" /> </form> HTML: I tried this.. and it works with only the username being submmited into the database as you can see the username variable here is $myusername which is the same as the page session hence it works im guessing.. i then tried many things to get the username in automatically but i failed. I need to put in the session variables i believe..? Any help will be much much appreciated really, and thankyou. P.S: I am a student trying to grasp a few things.. please dont reply if you are wanting something in return i.e. money. If you do help me resolve this i guess the best thing you will get from me is a good praise and my prayers. Thanks!
Hmm.. well, where in the script are you storing the variables into the current session? I don't see anything like that. Also, where is $myusername coming from in the form code? I'm guessing you have register_globals_on or something (though I'm not sure it registers those like that) or you're defining it in another part of the script? I'd honestly say to use the session array itself such as $_SESSION['myusername'] and $_SESSION['firstname'] (if that's what the two session names are). Also, one more thing that could be hurting it, make sure that you have session_start(); at the top of all the scripts that are attempting to access the session vars! Small tidbit: you can do <?=$variable?> in HTML and it'll echo that variable rather than doing <?php echo $variable; ?> or <?php echo "$variable"; ?>. Just an interesting, shorter way of doing it.
Hello Zerxer thankyou for your reply... ive already tried the different $_SESSION['myusername'] and defined them at the top but have not worked.. and currently the only session im using is if(!session_is_registered(myusername)){ Here is my entire code below The PHP HTML form: <? session_start(); if(!session_is_registered(myusername)){ header("location:wronglogin.php"); } ?> <?php //This file is text.php mysql_connect("**************", "******", "******"); //Connect to the mysql server with your host (most likely localhost), username, and password mysql_select_db(****"); //Select your database by name $sql = "SELECT * FROM cms_members WHERE username = '$myusername'"; $query = mysql_query($sql) or die(mysql_error()); //Make the actual query if( mysql_num_rows($query) == 1 ) //Check to see if we found 1 row with that page name { $r=mysql_fetch_assoc($query); //Set a mysql fetching variable for the query } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US"> <head> <title>Forest Gate Online - Submit Local News</title> <link rel="stylesheet" type="text/css" href="../stylesheet.css"/> <META NAME="Keywords" CONTENT="Community Forum, CMS Website, Community Page, Community Portal"> <META NAME="Description" CONTENT="A Community oriented website, come and find out more information about whats happening in your local area!"> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <div id="main"> <div id="banner"> Forest Gate Online<br /> <img src="../logo.gif" border=0 alt="" /> </div> <div id="loggedinbox"> <fieldset><legend><b><font size="2" color="#0066FF" face="verdana">Welcome</font> <font size="2" color="#208A00" face="verdana"><? echo $r["firstname"]; echo " "; echo $r["surname"]; ?></font></b></legend> You have successfully logged in as <center><b><? echo $r["username"]; ?> - <a href="logout.php">Log Out</a></b></center> </fieldset> </div> <div id="timebar">Today's Date is <?php echo date("l, F d, Y G:i T" ,time()); ?></div> <div id="maintitle">Main Title</div> <div id="maincontent"> <form action="articleinsert.php" method="post"> title: <input type="text" name="title" /> article: <input type="text" name="article" /> <input type="hidden" name="username" value="<?php echo "$myusername"; ?>" /> <input type="hidden" name="firstname" value="<?php echo"$firstname"; ?>" /> <input type="submit" name="register" value="submit" /> </form> <br /> <br /> </div> </div> </body> </html> HTML: Here is the article processor <?php // Make a MySQL Connection include "../config.php"; $submission_date = date("D M j Y G:i:s"); // Insert a row of information into the table "example" mysql_query("INSERT INTO article (firstname, surname, username, title, article, submission_date) VALUES('$firstname', '$surname', '$username', '$title', '$article', '$submission_date')") or die(mysql_error()); echo "Data Inserted!"; ?> PHP: Register globals is enabled on the server.Any help would be much appreciated, thanks. Cheers.
I don't see where you are defining the $myusername and $firstname variables? Sorry if I'm missing something obvious here, but this is the situation as I understand it. You have defined two session variables, myusername and firstname correct? Where are you defining these as $myusername and $firstname?
I have only defined myusername not firstname (i dont think). firstname is what the user used to register and is part of their profile so when they view other pages to submit an article i would like it to submit their firstnames also into the database whilst they only fill out article title and article content.. Here it is defined.. this is the login checker.. so when people log in this checks it then they gain entry.. thanks: <?php include "config.php"; $tbl_name="cms_members"; // Table name // Connect to server and select databse. mysql_connect("$dbhost", "$dbuser", "$dbpass")or die("cannot connect"); mysql_select_db("$dbname")or die("cannot select DB"); // username and password sent from signup form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to file "securepage.php" session_register("myusername"); session_register("mypassword"); header("location:securepage.php"); } else { ?> PHP:
I think that's your problem. At some point you need to define $first name for <input type="hidden" name="firstname" value="<?php echo"$firstname"; ?>" /> to work. If the value is stored as a session variable, the code would look something like: $firstname = $_SESSION['firstname']
**[ RESOLVED ]**I would like to thank the following people zerxer jorgy For helping me, much appreciated I would like to espeicially thank kishore415 for resolving this problem for me. The solution was.. all i needed is $firstname=$r["firstname"] under the database connection string and walla it worked and entered firstname into the database. I thankyou all once again. Cheers.
That's as easy as inserting the PHP code: session_start(); somewhere above where you are trying to pull out the session variables by the way.