Usint PHP, I'm trying to add the username, used in a login form, to the MYSQL table of another form.

Scenario: User "x" logs in (providing his username, "x", and a password). The username and password are stored in a MYSQL table called "users". Once "x" is logged in, he is directed to another form, where he provides his real name and other information and submits that form, and that information is stored in MYSQL table "register".

I want to be able to store "x" in a field of "register", without having "x" supplied in the form.

I think I'm close and have written the following code (but it's not working yet):

$tbl_name="users"; // Table name 

// Define $myusername and $mypassword 
$myusername=$_POST['username']; 
$mypassword=$_POST['password']; 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);


$sql="SELECT username FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$row=mysql_fetch_row($result);
$str=$row[1];
   

    //build and submit query
    $query = <<<HERE
INSERT INTO register VALUES(
null, '$first_name', '$last_name', '$address', '$city', '$state', '$zip_code', 
'$home_phone', '$cell_phone', '2011-01-01', '2011-01-02', '$str', null, null, null, null, 1);

HERE;

Can someone please help?

wait... let me say this to you...you want the php get the previous variables of the previous form right???

well if that is you can get the previous variables value.... this is called "variable passing by reference" for example your previous form has a variable $username... and the current form has $username again or $username_2 let's use $username for the current form's variable.

There are 2 techinques of variable passing by reference. according to your problem the technique used here is in a flow "Passing the Message to all the files of the entire site" this is the first technique.

you can do this way:
form1.html

<form action="form2.php" method="POST">
Username:<input type=text name="username">
<!---note: the "name" in the tag will be pass the value by reference because of the action and method attribute

form2.php

echo"<form action=\"form3.php\" method=\"post\">";
$username=$_POST['username']; //<---this "['username']" gets the value from the html form.

let's say you finished business w/ form2.php and you want to pass your $username value in your form2.php to your form3.php
form3.php

$USERNAME=$_POST['uername']; //<--- this time, the "['username']" here is not the username in html... rather it was the username in the $username of your form2.php

bare this in mind what ever is the variable that you want to copy from your previous form is case sensitive and spelling sensitive but please remove the $ sign when you want to call the value of the previous form's variable, OK.
and another to be remeinded is if you post at the first place then use the post in your entire site when doing this will ya??

u can also set the username in session..

session_start();
$_SESSION=$strUserName;

And get from session like,

session_start();
$strUserName= $_SESSION;

I have a similar question like this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.