I have been given some code whereby the script is parsing the data for insert into a a database. This is protected from sql injection attacks (see code below)
I am processing all the $_POST vars with it, then storing them in session vars so that the form handler has them available, it's a multi-page form.
The errors Im am getting are undefined indexes/varialbe errors and I am having a tough time getting my head round this as the code is more advanced and dynamic than I have handled in the past. I have had other issues which have been resolved but im trying to isoloate the problem to understand it better.
formhandler page at end of multipage form:
<?php
// at the top of each form processing page you need this little bit of code
require_once('common_functions.php'); // includes the quote_smart function
if (isset($_POST['val'])) {
foreach ($_POST as $key=>$val) {
// build the column name list
$_SESSION['cols'] .= $key . ',';
// and the values list
$_SESSION['vals'] .= quote_smart($val) . ',';
// and you MUST quote_smart to get the correct quotes around the values or the final query will fail
}
}
$strcols = rtrim($_SESSION['cols'], ',');
$strvals = rtrim($_SESSION['vals'], ',');
//now echo them just for debugging
echo $strcols;
echo $strvals;
//remove the echos once it is working
$sql = "INSERT INTO nottsvcs (" . $strcols . ") VALUES (" . $strvals . ")";
echo $sql;
?>
The error message here is:
Notice: Undefined variable: _SESSION in /disk1/home3/mindseye/public_html/nottsvcs_short_1_4_formhandler.php on line 20
Notice: Undefined variable: _SESSION in /disk1/home3/mindseye/public_html/nottsvcs_short_1_4_formhandler.php on line 21
INSERT INTO nottsvcs () VALUES ()
common_functions.php
<?php
error_reporting(E_ALL);
// Quote variable to make safe
function quote_smart($value)
{
// Stripslashes
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
// Quote if not integer
if (!is_numeric($value)) {
$value = "'" . mysql_real_escape_string($value) . "'";
}
return $value;
}
// Connect
$link = mysql_connect('*******', '****', '*****')
OR die(mysql_error());
// Make a safe query
$query = sprintf("SELECT * FROM users WHERE user=%s AND password=%s",
quote_smart($_POST['username']),
quote_smart($_POST['password']));
mysql_query($query);
?>
The error message is:
Notice: Undefined index: username in /disk1/home3/mindseye/public_html/common_functions.php on line 23
At the top of each page of my multipage form is this code which stores $_POST into a session var
<?php
// at the top of each form processing page you need this little bit of code
require_once('common_functions.php'); // includes the quote_smart function
if (isset($_POST['submit'])) {
foreach ($_POST as $key=>$val) {
// build the column name list
$_SESSION['cols'] .= $key . ',';
// and the values list
$_SESSION['vals'] .= quote_smart($val) . ',';
// and you MUST quote_smart to get the correct quotes around the values or the final query will fail
}
}
?
In this case the message reads:
Notice: Undefined variable: _SESSION in /disk1/home3/mindseye/public_html/nottsvcs_short_1_4_b.php on line 11
I have had some fantastic help already with similar problems in other threads and I thought I knew what was going on but when I keep getting the same errors I fear I am missing something simple.