Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Problem sending form data to email and insert in database

form data insert database email



1:21 am on Jul 28, 2010 (gmt 0)

5+ Year Member

I have been try now for the past 6 hours, hundreds of different conotation to get some form data to be sent to 2 emails and also be submitted to a mysql database.
The form originally sent an email message to a client and also to us (admin) then returned a thank you page to the form user.
I have created the mysql database with all required fields for the form data to be inserted in but can't get it to work.

Here is the form page as it stands at the moment starting from the main php part as there is a lot of html stuff before the form:

if (isset($action)) {

$arrival_date = $_REQUEST["arrival_day"] . "/" . $_REQUEST["arrival_month"] . "/" . $_REQUEST["arrival_year"];

$to = $USERS["email"];
$mailheader = "From: $email\r\n";
$mailheader .= "Reply-To: $email\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$msg =
Message to client here.


Enquirer details:<br><br>

Name: $name<br>
Country Code: $country, Phone Number: $phone<br>
Mobile/Cell phone number: $cell_phone<br>
E-mail address: $email<br>
Date of arrival: $arrival_day<br>
Duration: $duration day(s)<br>

Comments: ".strip_tags($info)."<br><br>

This enquiry was generated from: [".$_SERVER['HTTP_HOST']."...]

Thank you<br><br>
<br><br>Enquiry IP/ISP: ".$content .= "\n\nIP:" . getEnv( "REMOTE_ADDR" )." - ".@gethostbyaddr($_SERVER["REMOTE_ADDR"]).".

mail($to, "Enquiry", $msg, $mailheader) or die ("Failure");
mail("admin@oursite.co.uk", "Enquiry", $msg, $mailheader) or die ("Failure");

"<br /><br /><font size=2>
<strong><center>Thank you!</center><br />
Your Enquiry has been sent to:</center><br />
".$USERS["name"]." &nbsp; Tel: ".$USERS["phone"]."<br /><br />

For : ".stripslashes($ACC["name"])." <br />
Ref No: (".stripslashes($ACC["acc_ref"]).") <br /><br />
Please make note of owners details above for future reference.<br /><br /><br /></strong>

Enquiry details entered were: <br /><br />

Name: $name<br />
Country Code: $country, Phone Number: $phone<br />
Mobile/Cell phone number: $cell_phone<br />
E-mail address: $email<br />
Date of arrival: $arrival_day<br />
Duration: $duration day(s)<br /><br />

<br /><br />


if (!isset($action)) {

<form action="enq.php" method="post" style="margin-bottom:0px" name="cform" onsubmit="return check(this);">
<input type="hidden" name="action" value="0">
<input type="hidden" name="id" value="<?php echo $_REQUEST["id"]; ?>">
<table width="420px" valign="top" cellspacing="2" cellpadding="2">
<td class="bodytext"><strong><font size="3">E-mail enquiry form to:</font> </strong></td>
<td align="left" class="bodytext"><strong>Name:</strong> &nbsp;<?php echo $USERS["name"]; ?><br />
<strong>For property:</strong> &nbsp;<?php echo $ACC["name"]; ?></td>
<td align="left" class="bodytext"><strong>Your name:</strong><br /><input name="name" type="text" id="name" size="58"></td>
<td align="left" class="bodytext"><strong>Country Code:&nbsp;&nbsp; Phone Number: (inc area code)</strong><br />
<input name="country" type="text" id="country" size="10">&nbsp;&nbsp;<input name="phone" type="text" id="phone" size="40"></td>
<td align="left" class="bodytext"><strong>Mobile/Cell phone number:</strong><br /><input name="cell_phone" type="text" id="cell_phone" size="58"></td>
<td align="left" class="bodytext"><strong>Your E-mail Address:</strong><br /><input name="email" type="text" id="email" size="58"></td>
<td align="left" class="bodytext"><strong>Date of arrival & Duration:</strong><br />

<input name="arrival_day" type="text" id="arrival_day" size="10" maxlength="10" readOnly="true" />
<A HREF="#" onClick="cal1x.select(document.forms['cform'].arrival_day,'anchor1x_in','dd-MM-yyyy'); return false;" NAME="anchor1x_in" ID="anchor1x_in">Pick a date</A>&nbsp;&nbsp;

&nbsp;&nbsp; for <input name="duration" type="text" id="duration" size="10"> nights</td>
<td align="left" valign="top" class="bodytext"><strong>Comments:</strong><br />
<textarea name="info" cols="60" rows="6" id="info" class="bodytext"></textarea></td>
<table width="100%"><tr>
<td align="center" valign="top">&nbsp;<input type="submit" name="Submit" value="Send"></td>
<table width="100%">
<tr><td>some text
<br />
<br />
</form><?php } ?>
<div id="divHint"></div>

} else {
<td width="500" align="left" valign="top"><p><strong>No such properties</strong></p></td>


if (isset($_REQUEST['Submit'])) {

$sql = "INSERT INTO ".$TABLES["enquiries"]." SET

website='".$_REQUEST["info"]."', (want to generate which website form came from but can be left out)
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);


3:55 pm on Jul 28, 2010 (gmt 0)

WebmasterWorld Administrator lifeinasia is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

You don't use SET with an INSERT. You only use SET with UPDATE. INSERTS are of the form:
INSERT INTO TableName (Field1, Field2, ..., Fieldn)
VALUES (Value1, Value2, ..., Valuen)

So yours would be something like:
INSERT INTO ".$TABLES["enquiries"]." (p_id, p_name, ...)
VALUES ('".mysql_escape_string($ACC["ref"])."','".mysql_escape_string($ACC["name"])."', ...)


1:29 am on Jul 29, 2010 (gmt 0)

5+ Year Member

Thanks for that LifeinAsia I will try it.

The thing is I have...
$sql = "INSERT INTO ".$TABLES["whatever"]." SET
all over the place and it works fine. Our site were initially created by proper php programmers, I was just trying to emulate what they had done and introduce a new feature!


3:45 pm on Jul 29, 2010 (gmt 0)

WebmasterWorld Administrator lifeinasia is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

Hmmm, I guess that is MySQL proprietary syntax, so it is valid.

What is the error message you get? Also, echo the actual values that you're trying to insert and make sure they are what you expect. Those should help narrow down the problem. My first guess is that you are trying to do one or more of the following:
A) Insert a blank value for a field that doesn't allow NULLs
B) Insert a string that is longer than the field length
C) Insert a string into a numeric field.


6:17 pm on Jul 29, 2010 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Yeah that may be valid, it's just not the way it's **normally* done.

insert into table ([field list]) values ([values list]);

Anyway, did you try this? If $action is set, it sends the email, if not, displays the form. so

if (isset($action)) {
// other code
mail($to, "Enquiry", $msg, $mailheader) or die ("Failure");
mail("admin@oursite.co.uk", "Enquiry", $msg, $mailheader) or die ("Failure");

Do your insert right here

// more code
} //end isset($action)

else {
// Should really use else, but in the context of this
// script, if (!isset($action)) { is *almost* the same thing

// display form code

See that? It should work right there. Not sure what the purpose of the else is after the !isset($action), seems trivial.

if ($action) {
// process form, send email, insert to db, display response
else {
// display form

Poor error handling, but should work, most of the time . . . .


6:45 pm on Aug 4, 2010 (gmt 0)

5+ Year Member

Try moving calls to mysql_escape_string() outside of the query itself, along these lines:

$p_id = mysql_real_escape_string($ACC["ref"]);
$p_name = mysql_real_escape_string($ACC["name"]);

and then in your query:

INSERT INTO whatever SET p_id = '$p_id', p_name = '$p_name'[...]


6:50 pm on Aug 4, 2010 (gmt 0)

5+ Year Member


MySQL allows partial inserts using SET, as long as it does not collide with certain indexing schemes. It is common practice to omit auto_increment fields in INSERT statements - MySQL figures these things out on the fly. On the flip side, you can send 'NULL' or '' (empty string) as stand-ins for auto_increment field values with the same result.

Featured Threads

Hot Threads This Week

Hot Threads This Month