reply “Welcome [fname] to my website.” ????
I am a newbie. I have been working on this problem for a few days and I cannot find the answer. To explain the problem, I have set up a very simple test website. I am using WAMPSERVER 2.1 which includes PHP ver 5.3.5 and MySQL ver 5.5.8.
I have set up a database table with the table “testtable”, a php form “frmTEST.php” for the user to input his first name “fname” and last name “lname”, and a welcome “testOK.php” to reply “Welcome [fname] to my website.” Sounds simple but I cannot get “testOK.php” page to print the correct first name from the table field [fname].
I am using Dreamweaver CS5 to post the users to the database. The posting to the database table “testable” works. After posting the user to the table, the code from DW takes the user to the “testOK”.php page.
Now the problem. At line 8 of my code where I am trying to set the $_SESSION['user_fname'] = $_POST['fname']; I get an error that reads “Undefined index: fname in C:\wamp\www\frmTest.php on line 8”.
1 What am I doing wrong? It appears to me that the code that DW uses, does not make the $_POST[‘fname’] variable usable.
2. Is there a better way to do what I wish to do using DW’s code to POST the users to the table? I want to use DW’s code vs hand coding my own INSERT statement, because I assume that DW will keep up to date with any problems with sanitizing variables and browser incompatibility such as the code block “if (PHP_VERSION < 6)”.
=========== start code for frmTEST.php =============
<!--// this code is inserted by Dreamweaver.-->
<?php require_once('Connections/ndc_olcs.php'); ?>
<?php //code I added
session_start(); // I added this to start the session. I could not find in DW's where a session started.
echo session_id(); //debug to see if session id is same as testOK.php page. It WORKS!
// ***************************************
$_SESSION['user_fname'] = $_POST['fname']; // This does not work get error
// Undefined index: fname in C:\wamp\www\frmTest.php on line 8
// *****************************************
?>
<?php
// this code is inserted by Dreamweaver. *************
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "frmTEST")) {
$insertSQL = sprintf("INSERT INTO testtable (fname, lname) VALUES (%s, %s)",
GetSQLValueString($_POST['fname'], "text"),
GetSQLValueString($_POST['lname'], "text"));
mysql_select_db($database_ndc_olcs, $ndc_olcs);
$Result1 = mysql_query($insertSQL, $ndc_olcs) or die(mysql_error());
$insertGoTo = "testOK.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
// End of code inserted by Dreamweaver
//************************************
?>
<!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=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="<?php echo $editFormAction; ?>" method="POST" name="frmTEST">
<p>
<label for="fname">First Name</label>
<input type="text" name="fname" id="fname" tabindex="10" />
</p>
<p>
<label for="lname">Last Name</label>
<input type="text" name="lname" id="lname" tabindex="20" />
</p>
<p>Sumit Form
<input type="submit" name="sumit" id="sumit" value="Submit" tabindex="30" />
</p>
<p> </p>
<input type="hidden" name="MM_insert" value="frmTEST" />
</form>
</body>
</html>
=========== end code for frmTEST.php ===========
============= start code for testOK.php =============
<?php
require_once('Connections/ndc_olcs.php'); // I do not think I need this on this page
session_start();
echo "Your session identification number is ".session_id(); // to debug This Workes
echo ' - - - Welcome ' . $_SESSION['user_fname']; // the real test WORKS!!!
?>
<!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=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<h1>Welcome <?php echo $_SESSION['user_fname']?> to my website. </h1>
</body>
</html>
