...

View Full Version : Undefined variables error or e_notice



DEDI
09-15-2011, 10:21 PM
I can't get this code to work. What am i doing wrong? All the variations don't work.


<?php
// This page allows a logged-in user to add a profile.

require_once ('includes/config.inc.php');
$page_title = 'Add a profile';
include ('includes/header.html');

// check if form is submitted already.
if (!isset($_POST['submitted']));// handle the form
else{


require_once ('mysqli_connect.php');

// Trim all the incoming data:
$trimmed = array_map('trim', $_POST);

// Assume invalid values:
$en = $rn = $b = $f = $r = $pc = $cn = $c = $po = $e = $pn = FALSE;


//validate the form data
//check for a establsihment name:

if (!isset($_POST['est_name']))
// name is entered
@$en = $_POST['est_name'];
else{ $en = '' ;
}



//check for a road name:
if (!empty($_POST['road_name'])) //road name is entered
@$rn = $_POST['road_name'];
else {$rn = '';}


//check for a building:
if (!isset($_POST['building']))
$b = $_POST['building'];
else {$_POST['building'] = NULL;}


//check for a floor:
if (!empty($_POST['floor']))
$f = $_POST['floor'];
else {$_POST['floor'] = NULL;}

//check for a room:
if (!empty($_POST['room']))
$r = $_POST['room'];
else {$_POST['room'] = NULL;}

//check for a postal code:
if (!empty($_POST['postal_code']))
$pc = $_POST['postal_code'];
else {$_POST['postal_code'] = NULL;}

//check for a city name:
if(!empty($_POST['city_name']))
$cn = $_POST['city_name'];
else {$_POST['city_name'] = NULL;}



//check for a country name:
if (!empty($_POST['country']))
$c = $_POST['country'];
else {$_POST['country'] != NULL;}



//check for email:
if (!empty($_POST['po_box']))
$p = $_POST['po_box'];
else {$_POST['po_box'] = NULL;}


//check for a phone number:
if (!empty($_POST['email']))
$e = $_POST['email'];
else {$_POST['email'] != NULL;}



//check for a establsihment name:
if (!empty($_POST['phone_number']))
$pn = $_POST['phone_number'];
else {$_POST['phone_number'] != NULL;}


//End of main conditional
}

// Add profile to database.


// line 99. $q = "INSERT INTO addresses (user_id, est_name, road_name, building, floor, room, postal_code, city_name, country, po_box, email, phone_number) VALUES ('$_SESSION[user_id]', '$en', '$rn', '$b', '$f', '$r', '$pc', '$cn', '$c', '$pb', '$e', '$pn')";

$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

error_reporting(E_ALL ^ E_NOTICE);

if (mysqli_affected_rows($dbc) == 1); // If it ran OK.
else{ // If it did not run OK.
echo '<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>';

mysqli_close ($dbc);
} // End of main submit conditional.
?>

i get this error:

An error occurred in script '/home/content/83/8042383/html/add_profile.php' on line 99: Undefined variable: en
Date/Time: 9-15-2011 14:55:59

By the way, line 99 is the query part!

tangoforce
09-15-2011, 11:12 PM
What on earth are you doing here?



//*** if name is NOT entered *** - ! means not
if (!isset($_POST['est_name']))
// name is entered
@$en = $_POST['est_name']; // Set $en to a value that doesn't exist because isset() returned false ?!?!?!?!?!
else{ $en = '' ;
}


Get rid of the ! there before the isset for a start. Then remove @.

Also consider taking a look at the coding styles link in my signature below.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum