Why is this code issuing undifined varialbe error?

Why is this code giving “undifined variable” error at line no 40,41,42,43 and at line no 46 ? please help me to correct it .

[php]

<?php $con=mysql_connect('localhost','root',… mysql_select_db("ydms")or die("cannot select DB"); if ($con) { echo "connected"; } else { echo (error()); } // assigning values to the variables if(isset($_POST['customerID'])){$custo… if(isset($_POST['customerName'])){$cus… if(isset($_POST['shopName'])){$shopNam… if(isset($_POST['custAdd1'])){$cust_ad… if(isset($_POST['custAdd2'])){$cust_ad… if(isset($_POST['custAdd3'])){$cust_ad… if(isset($_POST['custCity'])){$cust_ad… if(isset($_POST['contactperson'])){$cu… if(isset($_POST['contactNo1'])){$cust_… if(isset($_POST['contactNo2'])){$cust_… if(isset($_POST['routeNumber'])){$rout… if(isset($_POST['customerState'])){$st… if(isset($_POST['customerRemarks'])){$… echo "done"; //inserting values in to the database $sql = "INSERT INTO `customer`(`cust_id`, `cust_name`, `cust_shop_name`, `cust_add_hnumber`, `cust_add_line1`, `cust_add_line2`, `cust_add_city`, `cust_contact_name`, `cust_contact1`, `cust_contact2`, `route_no`, `state`, `cust_remark`) // line number 40 start from here....................................… VALUES ('" . $customerName . "', '" . $shopName . "','" . $cust_add_hnumber . "', '" . $cust_add_line1 . "','" . $cust_add_line2 . "', '" . $cust_add_city . "', '" . $cust_contact_name . "', '" . $cust_contact1 . "', '" . $route_no . "', '" . $state . "','" . $cust_remark . "')"; // line no 46 is here....................................… $result = mysql_query($order) or die (mysql_error()); //verification if($result){ echo "Successful"; } else { echo "ERROR"; } ?>

[/php]

hard to tell when half the code is missing, but line numbers in general are just guidelines, doesn’t mean the error is actually at that spot.

If there’s really 4 lines separating the values from the original sql statement, that could be an issue and you don’t need all those quotes.

VALUES (’$customerName’, ‘$shopName’, ‘$cust_add_hnumber’, ‘$cust_add_line1’, ‘$cust_add_line2’, ’ $cust_add_city’, ‘$cust_contact_name’, ‘$cust_contact1’, ‘$route_no’, ‘$state’, ‘$cust_remark’)";

That’s all you need for that.

In general terms, php is looking at variables that don’t have anything assigned to them. If its happening when you first enter the page, then all those variables that use POST content will throw that error because they won’t be filled until the button is submitted.

2 ways of fixing these.
1 - default each variable ($name = “” or $name = NULL) or
2 - wrap each variable in if(isset()). Not the best way of doing it, can and will cause problems if you’re not careful.

Thank you so much for your kind support but it’s still giving the same error. Please help me. Here is the full code except the html part.

[php]

<?php $con=mysql_connect('localhost','root',''); mysql_select_db("ydms")or die("cannot select DB"); if ($con) { echo "connected"; } else { echo (error()); } // assigning values to the variables if(isset($_POST['customerID'])){$customerID=$_POST['customerID'];} if(isset($_POST['customerName'])){$customerName=$_POST['customerName'];} if(isset($_POST['shopName'])){$shopName=$_POST['shopName'];} if(isset($_POST['custAdd1'])){$cust_add_hnumber=$_POST['custAdd1'];} if(isset($_POST['custAdd2'])){$cust_add_line1=$_POST['custAdd2'];} if(isset($_POST['custAdd3'])){$cust_add_line2=$_POST['custAdd3'];} if(isset($_POST['custCity'])){$cust_add_city=$_POST['custCity'];} if(isset($_POST['contactperson'])){$cust_contact_name=$_POST['contactperson'];} if(isset($_POST['contactNo1'])){$cust_contact1=$_POST['contactNo1'];} if(isset($_POST['contactNo2'])){$cust_contact2=$_POST['contactNo2'];} if(isset($_POST['routeNumber'])){$route_no=$_POST['routeNumber'];} if(isset($_POST['customerState'])){$state=$_POST['customerState'];} if(isset($_POST['customerRemarks'])){$cust_remark=$_POST['customerRemarks'];} echo "done"; //inserting values in to the database $sql = "INSERT INTO `customer`(`cust_id`, `cust_name`, `cust_shop_name`, `cust_add_hnumber`,`cust_add_line1`, `cust_add_line2`, `cust_add_city`, `cust_contact_name`, `cust_contact1`, `cust_contact2`, `route_no`, `state`, `cust_remark`) VALUES (' $customerID ' , ' $customerName ' , ' $shopName ' ,' $cust_add_hnumber ' , ' $cust_add_line1 ' ,' $cust_add_line2 ' , ' $cust_add_city ' , ' $cust_contact_name ' , ' $cust_contact1 ' , ' $route_no ' , ' $state ' ,' $cust_remark ')"; $result = mysql_query($order) or die (mysql_error()); //verification if($result){ echo "Successful"; } else { echo "ERROR"; } ?>

[/php]

Do you have anything that detects when the submit button was pressed? not having it could cause it.
[php]
if(isset($_POST[‘submit’])) {
// assigning values to the variables
$customerID=$_POST[‘customerID’];
$customerName=$_POST[‘customerName’];
$shopName=$_POST[‘shopName’];
$cust_add_hnumber=$_POST[‘custAdd1’];
$cust_add_line1=$_POST[‘custAdd2’];
$cust_add_line2=$_POST[‘custAdd3’];
$cust_add_city=$_POST[‘custCity’];
$cust_contact_name=$_POST[‘contactperson’];
$cust_contact1=$_POST[‘contactNo1’];
$cust_contact2=$_POST[‘contactNo2’];
$route_no=$_POST[‘routeNumber’];
$state=$_POST[‘customerState’];
$cust_remark=$_POST[‘customerRemarks’];
echo “done”;

//inserting values in to the database
$sql = “INSERT INTO customer(cust_id, cust_name, cust_shop_name, cust_add_hnumber,cust_add_line1, cust_add_line2, cust_add_city, cust_contact_name, cust_contact1, cust_contact2, route_no, state, cust_remark) VALUES (’$customerID’, ‘$customerName’ , ‘$shopName’, ‘$cust_add_hnumber’, ‘$cust_add_line1’, ‘$cust_add_line2’, ‘$cust_add_city’, ‘$cust_contact_name’, ‘$cust_contact1’, ’ $route_no ', ‘$state’, ‘$cust_remark’)”;

$result = mysql_query($order) or die (mysql_error());
//verification

if($result){
echo “Successful”;
} else {
echo “ERROR”;
}
}
?>[/php]

Something like that should work

I’m only hazarding a guess here, but this line looks odd to me:
[php]if(isset($_POST[‘contactperson’])){$cust_contact_name=$_POST[‘contactperson’];}[/php]
at first glance you may wonder why, as it is actually written correct, but when you look at the code around it:

[php]if(isset($_POST[‘custAdd1’])){$cust_add_hnumber=$_POST[‘custAdd1’];}
if(isset($_POST[‘custAdd2’])){$cust_add_line1=$_POST[‘custAdd2’];}
if(isset($_POST[‘custAdd3’])){$cust_add_line2=$_POST[‘custAdd3’];}
if(isset($_POST[‘custCity’])){$cust_add_city=$_POST[‘custCity’];}[/php]
You see the variables use camelCase and the first line i posted does not…
Just a hunch I have and I may be wrong but you should check and let me know if it helps.

Cheers,
Red :wink:

Thank you so much for this kind support. I changed the program as the way you guys shown but the result is still the same…

Then the problem isn’t where its indicating. If you added the submit button detection, then its impossible for it to see that code block until the submit button is pressed. Go through all your code that’s running when the page loads and see if you have any more variables that rely on $_POST or $_GET calls for information.

Also check to make sure that the variables match with the cases. To php, $a and $A are 2 different variables, I think that’s what Redscouse was trying to get you to realize.

Sponsor our Newsletter | Privacy Policy | Terms of Service