Hello, I'm new to this forum, and php altogether so I'd really appreciate some help with this one!
I want the form to check and clean all the data input from the form (not included), then put it inot the database. I have been trying to do this for a while using a book but everytime I test the page I get the last line " Couldn't execute insert query.". I'm using phpMyAdmin - 2.8.2.4 and MySQL - 3.23.58, the website was already with this when I started updating it.
If anybody could point me in the right direction that would be great, I have been trying to get this work for some time :queasy:
<?php
$cxn = mysql_connect("localhost:3306","user","pass");
if (!$cxn)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("weddingquote", $cxn);
//check for blanks
foreach($_POST as $field => $value)
{
if (($field != "returntime") or
($field != "passengers") or
($field != "addinfo") or
($field != "telno"))
{
if( $value == " ")
{
$blanks[] = $field;
}
}
}
if(isset($blanks))
{
$message_new = "<b>You didn't fill in one or more of the required fields.
You must enter: </b><br>";
foreach($blanks as $value)
{
$message_new .= "$value, ";
}
extract($_POST);
include("coachhire.inc");
exit();
}
//validate data
foreach($_POST as $field => $value)
{
if(!empty($value))
{
if(eregi("name",$field) or eregi("address",$field) or eregi("pickup",$field) or eregi("destination",$field) or eregi("addinfo",$field))
{
if(!ereg("^[A-Za-z0-9.,' -]{1,50}$",$value) )
{
$errors[] = "$value is not a valid format.";
}
}
if(eregi("telno", $field) or eregi("date", $field) or eregi("returntime", $field))
{
if(!ereg("^[0-9)( -]{7,20}(([xX]|(ext)|(ex))?[ -]?[0-9]{1,7})?$",$value) )
{
$errors[] = "$value is not a valid phone number. ";
}
}
if(eregi("email", $$field))
{
if(!ereg("^.+@.+\\..+$",$value))
{
$errors[] = "$value is not a valid email address.";
}
}
}
}
if(@is_array($errors))
{
$message_new = "";
foreach($errors as $value)
{
$message_new .= $value." Please try again<br />";
}
extract($_POST);
include("coachhire.inc");
exit();
}
//clean data
$cxn = mysql_connect("host","user","password");
mysql_select_db("table", $cxn);
foreach($_POST as $field => $value)
{
if($field != "Button" )
{
$fields[]=$field;
$value = strip_tags(trim($value));
$values[] =
mysql_real_escape_string($value,$cxn);
$$field = $value;
}
}
?>
<html><head><title>Data added</title></head>
<body>
<?php
//add data to database
$fields_str = implode(",",$fields);
$values_str = implode('","',$values);
$sql = "INSERT INTO coachhire ";
$sql .="(".$fields_str.")";
$sql .="VALUES";
$sql .="(".'"'.$values_str.")";
$result = mysql_query($sql,$cxn)
or die("Couldn't execute insert query.");
break;
echo "Thank you for your enquiry, we will get back to you as soon as possible\n";
echo "<a href='index.htm'>Return to Homepage</a>";
?>
</body></html>
Thanks 🙂