Jump to content

[SOLVED] Help with Form Submission?


stublackett

Recommended Posts

Hi,

 

I've got a Bookings Form that has an Array for Validation purposes and it works if said fields are left NULL

 

But if the form is filled in correctly, The Thank You Message and SQL Statement just doesnt work ???

 

Apologies for it being a large chunk of code, But if anyone can spot why the SQL Statement and the "Thank You" message doesnt load, It would be much appreciated :)

 

<?php 
// create flag(s) for validation errors -
$errors = array(); // 

// check if the form has been submitted
if(isset($_POST['submit']))
{
//Collect Group Vars
$kids = $_POST['kids'];
$adults = $_POST['adults'];
$specialneeds = $_POST['specialneeds'];
$carers = $_POST['carers'];
$seniorcitizens = $_POST['seniorcitizens'];
$under3 = $_POST['under3'];

//Calculate Thte Total
$total = $kids * 5.25 + $adults * 8.00 + $specialneeds * 5.25 + $carers * 5.25 + $seniorcitizens * 5.25;
//New Total	

//Collect Bookings Vars
$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$company = $_POST['company'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$town = $_POST['town'];
$county = $_POST['county'];
$postcode = $_POST['postcode'];
$phone = $_POST['phone'];
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];

//Check Forms' Post Vars
if (!empty($_POST['firstname'])) {
	$firstname = $_POST['firstname'];
}else{
	$title = NULL;
	$errors['firstname'] = '<p><font color="red">You need to enter a firstname</font></p>';
}
if (!empty($_POST['surname'])) {
	$surname = $_POST['surname'];
}else{
	$surname = NULL;
	$errors['surname'] = '<p><font color="red">You need to enter a surname</font></p>';
}

if (!empty($_POST['email'])) {
	$email = $_POST['email'];
}else{
	$email = NULL;
	$errors['description'] = '<p><font color="red">You need to enter an E-Mail Address</font></p>';
}

// If everything is filled out print the message.
if(empty($errors))
{
	// If all is ok, Insert into DB
	$sql = "INSERT INTO bdBookings(firstname,surname,email,company,address1,address2,town,county,postcode,phone,visit_date,additional_info,kids,adults,special_needs,carers,senior_citizens,under3,total_cost) values ('$firstname','$surname','$email','$company','$address1','$address2','$town','$county','$postcode','$phone','$visit_date','$additional_info','$kids','$adults','$special_needs','$carers','$senior_citizens','$under3','$total_cost')"; 
	// Incase needed($result = mysql_query($sql ,$db));
	($result = mysql_query($sql ,$db) or die(mysql_error()));

echo "Thank you! Your Booking has been made, We will be in touch soon";
echo "<br />";
echo "<meta http-equiv=Refresh content=5;url=index.php>";
echo $sql;
	exit;
}
}

// if the form was not submitted or there were validation errors, display the form -
if(!isset($_POST['submit']) || !empty($errors))
{
?>

<?php
// check for any errors and display -
if(!empty($errors))
{
foreach($errors as $key => $error)
{
	echo "<b>Please correct this error</b>: $error";
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/117368-solved-help-with-form-submission/
Share on other sites

Silly question time:

 

1) Have you ensured that it's actually entering into the if statement where the sql statement is?

2) Have you printed out the $errors array right before the "if(empty($errors))" line, to make sure it's empty?

3) Printed out of the $sql statement before it runs, then run it manually to test to make sure it actually runs?

 

Can't help much on the sql bit as I don't have much experience with it, but maybe this will help?

 

 

hi,

 

I think you havent established a connection with the database?.. and thats y the query is not working?

 

($result = mysql_query($sql ,$db) or die(mysql_error()));

 

in the above line, $db is the connection id to the database... and i dont see it being given a value anywhere.

 

connect to your database, and then run the script. It should work. $db need not be given with mysql_query most of the time though

hi,

 

I think you havent established a connection with the database?.. and thats y the query is not working?

 

($result = mysql_query($sql ,$db) or die(mysql_error()));

 

in the above line, $db is the connection id to the database... and i dont see it being given a value anywhere.

 

connect to your database, and then run the script. It should work. $db need not be given with mysql_query most of the time though

Well spotted niranjnn01

 

I've got the error_report(E_ALL) already set and the error I'm getting from PHP is :

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in

 

and thats coming from the line of code : ($result = mysql_query($sql ,$database) or die(mysql_error()));

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.