Jump to content

php error handling


spanner206
Go to solution Solved by MDCode,

Recommended Posts

hi ive encountered around 15 problems at once but just about all of them are on the same row

<!DOCTYPE HTML> 
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body> 
 
<?php
$con = mysqli_connect("localhost","root","","nib");
// Check connection
if (mysqli_connect_errno())
{
	echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// define variables and set to empty values

$companyname = $firstname = $address1 = $address2 = $area = $city = $postcode = $email = $website = $clubphone = $homephone = $mobilephone = $typeofbusiness = "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
 
   if (empty($_POST["companyname"]))
     {$errors['companyname'] = "please enter your companys name";}
   else
     {$companyname = test_input($_POST["companyname"]);}
   
   if (empty($_POST["typeofbusiness"]))
     {$errors['typeofbusiness'] = "please enter your TypeofBusiness";}
   else
     {$typeofbusiness = test_input($_POST["typeofbusiness"]);}	 
    
	if (empty($_POST["firstname"]))
     {$errors['firstname'] = "Please Enter Your First Name";}
   else
     {$firstname = test_input($_POST["firstname"]);}
     
   if (empty($_POST["address1"]))
     {$errors['address1'] = "Please Enter Address 1";}
   else
     {$address1 = test_input($_POST["address1"]);}
   
    if (empty($_POST["address2"]))

     {$address2 = test_input($_POST["address2"]);}
 
     if (empty($_POST["area"]))
     {$errors['area'] = "Please enter Area";}
   else
     {$area = test_input($_POST["area"]);}
	 
     if (empty($_POST["city"]))
     {$errors['city'] = "Please Enter City";}
   else
     {$city = test_input($_POST["city"]);}
    
	if (empty($_POST["postcode"]))
     {$errors['postcode'] = "Please enter your PostCode";}
   else
     {$postcode = test_input($_POST["postcode"]);}
	 
	if (empty($_POST["email"]))
     {$errors['email'] = "Please enter your Email";}
   else
     {$email = test_input($_POST["email"]);}
	  
     if (empty($_POST["Website"]))
     {$errors['Website'] = "Please Enter your Website";}
   else
     {$Website = test_input($_POST["Website"]);}
	
	if (empty($_POST["ClubNumber"]))
     {$errors['clubnumber'] = "Please enter your club number";}
   else
     {$website = test_input($_POST["clubphone"]);}
	   
	if (empty($_POST["HomeNumber"]))
     {$errors['homenumber'] = "Please enter your home number";}
   else
     {$website = test_input($_POST["homephone"]);}
	 
	if (empty($_POST["MobileNumber"]))
     {$errors['mobilenumber'] = "Please enter your mobile number";}
   else
     {$website = test_input($_POST["mobilephone"]);}
	 }
		function test_input($data)
{
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
}
?>
 
<h2>Add Leads</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
  <table border = "0">
	<tr>
		<td>Company Name:</td><td> <input type="text" name="companyname"></td>
		<td><span class="error">* <?php if (isset($errors['companyname'])) echo $errors['companyname']; ?></span></td>
	</tr>
	<tr>
		<td>type of business:</td><td> <input type="text" name="typeofbusiness"></td>
		<td><span class="error">* <?php if (isset($errors['companyname'])) echo $errors['companyname']; ?></span></td>
	</tr>
    <tr>
		<td>First Name:</td><td> <input type="text" name="firstname"></td>
		<td><span class="error">* <?php if (isset($errors['firstname'])) echo $errors['firstname']; ?></span></td>

	</tr>
	<tr>
		<td>Address 1:</td><td> <input type="text" name="address1"></td>
		<td><span class="error">* <?php if (isset($errors['address1'])) echo $errors['address1']; ?></span></td>
   </tr>
   <tr>
		<td>Address 2:</td><td><input type="text" name="address2"></td>
	
   </tr>
   <tr>
		<td>Area:</td> <td><input type="text" name="area"></td>
		<td><span class="error">* <?php if (isset($errors['area'])) echo $errors['area']; ?></span></td>
   </tr>
   <tr>
		<td>PostCode:</td> <td><input type="text" name="postcode"></td>
		<td><span class="error">* <?php if (isset($errors['postcode'])) echo $errors['postcode']; ?></span></td>
   </tr>
   <tr>
		<td>City:</td> <td><input type="text" name="city"></td>
		<td><span class="error">* <?php if (isset($errors['city'])) echo $errors['city']; ?></span></td>
	</tr>
	<tr>
		<td>Email:</td> <td><input type="text" name="email"></td>
		<td><span class="error">* <?php if (isset($errors['email'])) echo $errors['email']; ?></span></td>
	</tr>
	<tr>
		<td>Website:</td> <td><input type="text" name="website"></td>
		<td><span class="error">* <?php if (isset($errors['website'])) echo $errors['website']; ?></span></td>
	</tr>
	<tr>
		<td>Club phone:</td> <td><input type="text" name="clubphone"></td>
		<td><span class="error">* <?php if (isset($errors['clubphone'])) echo $errors['clubphone']; ?></span></td>
	</tr>
		<tr>
		<td>home phone:</td> <td><input type="text" name="homephone"></td>
		<td><span class="error">* <?php if (isset($errors['homephone'])) echo $errors['homephone']; ?></span></td>
	</tr>
	</tr>
		<tr>
		<td>mobile phone:</td> <td><input type="text" name="mobilephone"></td>
		<td><span class="error">* <?php if (isset($errors['mobilephone'])) echo $errors['mobilephone']; ?></span></td>
	</tr>
	<tr>
   <td><input type="submit" name="submit" value="Submit"></td>
   </tr>
<?php

if (count($errors)==0) 

{

  $con = mysqli_connect("localhost","root","","nib");
  // Check connection
  if (mysqli_connect_errno())
  {
	echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  $sql="INSERT INTO tbl_club_contacts (CompanyName, FirstName, Address1, Address2, Area, City, PostCode, Email, Website, ClubPhone, HomePhone, MobilePhone ) VALUES ('$_POST[companyname]','$_POST[firstname]','$_POST[address1]','$_POST[address2]','$_POST[area]','$_POST[city]','$_POST[postcode]','$_POST[email]','$_POST[website]','$_POST[clubphone]','$_POST[homephone]','$_POST[mobilephone]','$_POST[typeofbusiness]')";

  if (!mysqli_query($con,$sql))
  {
   die('Error: ' . mysqli_error($con));
   echo "record added";
  }
 
 
  mysqli_close($con);

} // end if $errors == 0

?>
   </form>
 
 
 
</body>
</html>

and the errors

( ! ) Notice: Undefined variable: errors in C:\wamp\www\AddLeads\addeadstemplate.php on line 161 Call Stack # Time Memory Function Location 1 0.0000 166344 {main}( ) ..\addeadstemplate.php:0

 

( ! ) Notice: Undefined index: companyname in C:\wamp\www\AddLeads\addeadstemplate.php on line 172 Call Stack # Time Memory Function Location 1 0.0000 166344 {main}( ) ..\addeadstemplate.php:0

 

( ! ) Notice: Undefined index: firstname in C:\wamp\www\AddLeads\addeadstemplate.php on line 172 Call Stack # Time Memory Function Location 1 0.0000 166344 {main}( ) ..\addeadstemplate.php:0

 

( ! ) Notice: Undefined index: address1 in C:\wamp\www\AddLeads\addeadstemplate.php on line 172 Call Stack # Time Memory Function Location 1 0.0000 166344 {main}( ) ..\addeadstemplate.php:0

 

( ! ) Notice: Undefined index: address2 in C:\wamp\www\AddLeads\addeadstemplate.php on line 172 Call Stack # Time Memory Function Location 1 0.0000 166344 {main}( ) ..\addeadstemplate.php:0

 

( ! ) Notice: Undefined index: area in C:\wamp\www\AddLeads\addeadstemplate.php on line 172 Call Stack # Time Memory Function Location 1 0.0000 166344 {main}( ) ..\addeadstemplate.php:0

 

( ! ) Notice: Undefined index: city in C:\wamp\www\AddLeads\addeadstemplate.php on line 172 Call Stack # Time Memory Function Location 1 0.0000 166344 {main}( ) ..\addeadstemplate.php:0

 

( ! ) Notice: Undefined index: postcode in C:\wamp\www\AddLeads\addeadstemplate.php on line 172 Call Stack # Time Memory Function Location 1 0.0000 166344 {main}( ) ..\addeadstemplate.php:0

 

( ! ) Notice: Undefined index: email in C:\wamp\www\AddLeads\addeadstemplate.php on line 172 Call Stack # Time Memory Function Location 1 0.0000 166344 {main}( ) ..\addeadstemplate.php:0

 

( ! ) Notice: Undefined index: website in C:\wamp\www\AddLeads\addeadstemplate.php on line 172 Call Stack # Time Memory Function Location 1 0.0000 166344 {main}( ) ..\addeadstemplate.php:0

 

( ! ) Notice: Undefined index: clubphone in C:\wamp\www\AddLeads\addeadstemplate.php on line 172 Call Stack # Time Memory Function Location 1 0.0000 166344 {main}( ) ..\addeadstemplate.php:0

 

( ! ) Notice: Undefined index: homephone in C:\wamp\www\AddLeads\addeadstemplate.php on line 172 Call Stack # Time Memory Function Location 1 0.0000 166344 {main}( ) ..\addeadstemplate.php:0

 

( ! ) Notice: Undefined index: mobilephone in C:\wamp\www\AddLeads\addeadstemplate.php on line 172 Call Stack # Time Memory Function Location 1 0.0000 166344 {main}( ) ..\addeadstemplate.php:0

 

( ! ) Notice: Undefined index: typeofbusiness in C:\wamp\www\AddLeads\addeadstemplate.php on line 172 Call Stack # Time Memory Function Location 1 0.0000 166344 {main}( ) ..\addeadstemplate.php:0

Error: Column count doesn't match value count at row 1

 

if anyone can help please get back to me soon as

Link to comment
Share on other sites

is this in the right position because its still coming up with the same errors

if (count($errors)==0) 

{

  $con = mysqli_connect("localhost","root","","nib");
  // Check connection
  if (mysqli_connect_errno())
  {
	echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
if($_SERVER['REQUEST_METHOD'] == "POST" && count($errors) == 0) {
// Do it
}
  $sql="INSERT INTO tbl_club_contacts (CompanyName, FirstName, Address1, Address2, Area, City, PostCode, Email, Website, ClubPhone, HomePhone, MobilePhone ) VALUES ('$_POST[companyname]','$_POST[firstname]','$_POST[address1]','$_POST[address2]','$_POST[area]','$_POST[city]','$_POST[postcode]','$_POST[email]','$_POST[website]','$_POST[clubphone]','$_POST[homephone]','$_POST[mobilephone]','$_POST[typeofbusiness]')";

  if (!mysqli_query($con,$sql))
  {
   die('Error: ' . mysqli_error($con));
   echo "record added";
  }
 
 
  mysqli_close($con);

} // end if $errors == 0

?>
   </form>
 
 
 
</body>
</html>
Link to comment
Share on other sites

right thats one down, now its all the ones on row 174 any ideas

<?php

 
if($_SERVER['REQUEST_METHOD'] == "POST" && count($errors) == 0) {
// Do it
}
{

  $con = mysqli_connect("localhost","root","","nib");
  // Check connection
  if (mysqli_connect_errno())
  {
	echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  $sql="INSERT INTO tbl_club_contacts (CompanyName, FirstName, Address1, Address2, Area, City, PostCode, Email, Website, ClubPhone, HomePhone, MobilePhone ) VALUES ('$_POST[companyname]','$_POST[firstname]','$_POST[address1]','$_POST[address2]','$_POST[area]','$_POST[city]','$_POST[postcode]','$_POST[email]','$_POST[website]','$_POST[clubphone]','$_POST[homephone]','$_POST[mobilephone]','$_POST[typeofbusiness]')";

  if (!mysqli_query($con,$sql))
  {
   die('Error: ' . mysqli_error($con));
   echo "record added";
  }
 
 
  mysqli_close($con);

} // end if $errors == 0

?>
   </form>
 
 
 
</body>
</html>
Link to comment
Share on other sites

that's removed all of the errors but now its not submitting data into my database, any ideas?

<!DOCTYPE HTML> 
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body> 
 
<?php
$con = mysqli_connect("localhost","root","","nib");
// Check connection
if (mysqli_connect_errno())
{
	echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// define variables and set to empty values

$companyname = $firstname = $address1 = $address2 = $area = $city = $postcode = $email = $website = $clubphone = $homephone = $mobilephone = $typeofbusiness = "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
 $errors = array();
   if (empty($_POST["companyname"]))
     {$errors['companyname'] = "please enter your companys name";}
   else
     {$companyname = test_input($_POST["companyname"]);}
   
   if (empty($_POST["typeofbusiness"]))
     {$errors['typeofbusiness'] = "please enter your TypeofBusiness";}
   else
     {$typeofbusiness = test_input($_POST["typeofbusiness"]);}	 
    
	if (empty($_POST["firstname"]))
     {$errors['firstname'] = "Please Enter Your First Name";}
   else
     {$firstname = test_input($_POST["firstname"]);}
     
   if (empty($_POST["address1"]))
     {$errors['address1'] = "Please Enter Address 1";}
   else
     {$address1 = test_input($_POST["address1"]);}
   
    if (empty($_POST["address2"]))

     {$address2 = test_input($_POST["address2"]);}
 
     if (empty($_POST["area"]))
     {$errors['area'] = "Please enter Area";}
   else
     {$area = test_input($_POST["area"]);}
	 
     if (empty($_POST["city"]))
     {$errors['city'] = "Please Enter City";}
   else
     {$city = test_input($_POST["city"]);}
    
	if (empty($_POST["postcode"]))
     {$errors['postcode'] = "Please enter your PostCode";}
   else
     {$postcode = test_input($_POST["postcode"]);}
	 
	if (empty($_POST["email"]))
     {$errors['email'] = "Please enter your Email";}
   else
     {$email = test_input($_POST["email"]);}
	  
     if (empty($_POST["Website"]))
     {$errors['Website'] = "Please Enter your Website";}
   else
     {$Website = test_input($_POST["Website"]);}
	
	if (empty($_POST["ClubNumber"]))
     {$errors['clubnumber'] = "Please enter your club number";}
   else
     {$website = test_input($_POST["clubphone"]);}
	   
	if (empty($_POST["HomeNumber"]))
     {$errors['homenumber'] = "Please enter your home number";}
   else
     {$website = test_input($_POST["homephone"]);}
	 
	if (empty($_POST["MobileNumber"]))
     {$errors['mobilenumber'] = "Please enter your mobile number";}
   else
     {$website = test_input($_POST["mobilephone"]);}
	 }
		function test_input($data)
{
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
}
?>
 
<h2>Add Leads</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
  <table border = "0">
	<tr>
		<td>Company Name:</td><td> <input type="text" name="companyname"></td>
		<td><span class="error">* <?php if (isset($errors['companyname'])) echo $errors['companyname']; ?></span></td>
	</tr>
	<tr>
		<td>type of business:</td><td> <input type="text" name="typeofbusiness"></td>
		<td><span class="error">* <?php if (isset($errors['companyname'])) echo $errors['companyname']; ?></span></td>
	</tr>
    <tr>
		<td>First Name:</td><td> <input type="text" name="firstname"></td>
		<td><span class="error">* <?php if (isset($errors['firstname'])) echo $errors['firstname']; ?></span></td>

	</tr>
	<tr>
		<td>Address 1:</td><td> <input type="text" name="address1"></td>
		<td><span class="error">* <?php if (isset($errors['address1'])) echo $errors['address1']; ?></span></td>
   </tr>
   <tr>
		<td>Address 2:</td><td><input type="text" name="address2"></td>
	
   </tr>
   <tr>
		<td>Area:</td> <td><input type="text" name="area"></td>
		<td><span class="error">* <?php if (isset($errors['area'])) echo $errors['area']; ?></span></td>
   </tr>
   <tr>
		<td>PostCode:</td> <td><input type="text" name="postcode"></td>
		<td><span class="error">* <?php if (isset($errors['postcode'])) echo $errors['postcode']; ?></span></td>
   </tr>
   <tr>
		<td>City:</td> <td><input type="text" name="city"></td>
		<td><span class="error">* <?php if (isset($errors['city'])) echo $errors['city']; ?></span></td>
	</tr>
	<tr>
		<td>Email:</td> <td><input type="text" name="email"></td>
		<td><span class="error">* <?php if (isset($errors['email'])) echo $errors['email']; ?></span></td>
	</tr>
	<tr>
		<td>Website:</td> <td><input type="text" name="website"></td>
		<td><span class="error">* <?php if (isset($errors['website'])) echo $errors['website']; ?></span></td>
	</tr>
	<tr>
		<td>Club phone:</td> <td><input type="text" name="clubphone"></td>
		<td><span class="error">* <?php if (isset($errors['clubphone'])) echo $errors['clubphone']; ?></span></td>
	</tr>
		<tr>
		<td>home phone:</td> <td><input type="text" name="homephone"></td>
		<td><span class="error">* <?php if (isset($errors['homephone'])) echo $errors['homephone']; ?></span></td>
	</tr>
	</tr>
		<tr>
			<td>mobile phone:</td> <td><input type="text" name="mobilephone"></td>
			<td><span class="error">* <?php if (isset($errors['mobilephone'])) echo $errors['mobilephone']; ?></span></td>
		</tr>
	<tr>
   <td><input type="submit" name="submit" value="Submit"></td>
   </tr>
<?php

 
if($_SERVER['REQUEST_METHOD'] == "POST" && count($errors) == 0) {
// Do it

{

  $con = mysqli_connect("localhost","root","","nib");
  // Check connection
  if (mysqli_connect_errno())
  {
	echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  $sql="INSERT INTO tbl_club_contacts (CompanyName, FirstName, Address1, Address2, Area, City, PostCode, Email, Website, ClubPhone, HomePhone, MobilePhone ) VALUES ('$_POST[companyname]','$_POST[firstname]','$_POST[address1]','$_POST[address2]','$_POST[area]','$_POST[city]','$_POST[postcode]','$_POST[email]','$_POST[website]','$_POST[clubphone]','$_POST[homephone]','$_POST[mobilephone]','$_POST[typeofbusiness]')";

  if (!mysqli_query($con,$sql))
  {
   die('Error: ' . mysqli_error($con));
   echo "record added";
  }
 
 
  mysqli_close($con);

} // end if $errors == 0
}
?>
   </form>
 
 
 
</body>
</html>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.