PHP - Note: variable Undefined happens twice
I get the following two errors when loading my page:
Notice: Undefined variable: realtor in C:\Program Files\EasyPHP-5.3.9\www\cglst\images\addform.php on line 255
and
Notice: Undefined variable: phone in C:\Program Files\EasyPHP-5.3.9\www\cglst\images\addform.php on line 256
I am actually defining both of these variables, so I don't understand why I am getting these errors. Here is my code:
function addListing() {//if data was provided, insert it into database and confirm
//this will allow everything to be sanitized properly
require_once "sanitize.php";
$submitted = false;
//Checking if values were passed
if (isset($_POST['area']) &&
isset($_POST['price']) &&
isset($_POST['address']) &&
isset($_POST['bedrooms']) &&
isset($_POST['fullbath']) &&
isset($_POST['halfbath']) &&
isset($_POST['sqft']))
//if passed, sanitize and set variables accordingly
{
$area = sanitizeOne(get_post('area'), 'plain');
$price = sanitizeOne(get_post('price'), 'int');
$address = sanitizeOne(get_post('address'), 'plain');
$bedrooms = sanitizeOne(get_post('bedrooms'), 'int');
$fullbath = sanitizeOne(get_post('fullbath'), 'int');
$halfbath = sanitizeOne(get_post('halfbath'), 'int');
$sqft = sanitizeOne(get_post('sqft'), 'int');
$submitted = true;
}
//optional fields
if (isset($_POST['remarks']))
{
$remarks = sanitizeOne(get_post('remarks'), 'plain');
}
else
{$remarks = ' ';}
if (isset($_POST['realtor']))
{
$remarks = sanitizeOne(get_post('realtor'), 'plain');
}
else
{$realtor = "Anne-Marie Pelletier";}
if (isset($_POST['phone']))
{
$remarks = sanitizeOne(get_post('phone'), 'plain');
}
else
{$phone = "201.710.5500";}
if ($submitted) {
$query = 'PREPARE statement FROM "INSERT INTO bix(area, price, address, bedrooms,
fullbath, halfbath, sqft, remarks, realtor, phone) VALUES(?,?,?,?,?,?,?,?,?,?)"';
mysql_query($query);
$query = 'SET
@area = "' . $area . '"' .
'@price = "' . $price . '"' .
'@address = "' . $address . '"' .
'@bedrooms = "' . $bedrooms . '"' .
'@fullbath = "' . $fullbath . '"' .
'@halfbath = "' . $halfbath . '"' .
'@sqft = "' . $sqft . '"' .
'@remarks = "' . $remarks . '"' .
'@realtor = "' . $realtor . '"' . //line 255
'@phone = "' . $phone . '"'; //line 256
mysql_query($query);
$query = 'EXECUTE statement USING @area,@price,@address,@bedrooms,@fullbath,@halfbath,@sqft,@remarks,@realtor,@phone';
mysql_query($query);
$query = 'DEALLOCATE PREPARE statement';
mysql_query($query);
return true;
}
}
function get_post($var)
{
return mysql_real_escape_string($_POST[$var]);
}
It's just adding a record to the database if it was submitted (the page is a form for this)
source to share
Your problem is here, cut'n'paste error,
if (isset($_POST['realtor']))
{
$remarks = sanitizeOne(get_post('realtor'), 'plain');
}
else
{$realtor = "Anne-Marie Pelletier";}
If the realtor is specified as a post parameter, you assign the value to the post variable $remarks
instead of $realtor
.
$phone
has the same problem.
source to share
If a phone value is passed you set the notes variable to the phone content, if not set you set the fixed phone
change:
if (isset($_POST['phone']))
{
$remarks = sanitizeOne(get_post('phone'), 'plain');
}
else
{$phone = "201.710.5500";}
to
if (isset($_POST['phone']))
{
$phone = sanitizeOne(get_post('phone'), 'plain');
}
else
{$phone = "201.710.5500";}
The same for a realtor
To debug the all-null problem, try recording the entry without a realtor or phone, that is, using the defaults in your code. if you keep these two values then the problem is in santizeOne, please provide the code that will help us. Unless it tries to grab the output of all first requests and post it.
source to share