Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
|
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 10th, 2010, 01:42 PM
Registered User
 
Join Date: Feb 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy Need help inserting to a database then emailing

I need help inserting to a database then emailing that information. I am running into a PHP Notice: Undefined variable: REQUEST_METHOD on line 20 error also. It will not post to the database. It is on CentOS 5.4, with PHP 5.2.11, MySQL 5.0.86 and Apache 2.2.14.

Here is the script so far:

<?php

$usr = "dbuser"; #not real
$pwd = "dbpassword"; #not real
$db = "dbcontacts";
$host = "localhost";

// connect to database
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }

?>
<?php
// this is processed when the form is submitted
// back on to this page (POST METHOD)
if ($REQUEST_METHOD=="POST") {

// double-up apostrophes
$email = $_POST['email'];
$ip = gethostbyname($_SERVER['REMOTE_ADDR']);


// setup SQL statement
$SQL = " INSERT INTO contacts ";
$SQL = $SQL . " (email, date, ip) VALUES ";
$SQL = $SQL . " ('$email', NOW(), '$ip') ";

//execute SQL statement
$result = mysql_db_query($db,"$SQL",$cid);

// check for error
if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); }

if($result)
{
//send the email

$to = "[email protected]"; #not real
$subject = "New contact from the website";

//headers and subject
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: <".$email.">\r\n";

$body = "New contact<br />";
$body .= "Email: ".$email."<br />";
$body .= "IP: ".$ip."<br />";

mail($to, $subject, $body, $headers);

//ok message

echo "Your message has been sent";
}

}

?>
<div id="wrap"> <img src="http://p2p.wrox.com/images/top.jpg" id="top">
<div id="piece3">
<form method=post action="index.php" >
<input type=hidden name=redirect value="thankyou.php" />
<input type=hidden name=errorredirect value="error.php" />
<script type="text/javascript">
function verifyRequired() {
if (document.icpsignup["fields_email"].value == "") {
document.icpsignup["fields_email"].focus();
alert("The Email field is required.");
return false;
}
return true;
}
</script>
<input type=text name="email" id="email" title="Enter your email address">
<input name="Submit" type="submit" id="subscribe" value="" />

</form>
</div>
<div id="alerts">
<ul class="hoverbox">
<li><a href="#"><img src="http://p2p.wrox.com/images/Stock1.jpg"><img src="http://p2p.wrox.com/images/vnda.png" class="preview"></a></li>
</ul>
<ul class="hoverbox2">
<li><a href="#"><img src="http://p2p.wrox.com/images/Stock2.jpg"><img src="http://p2p.wrox.com/images/pir.png" class="preview"></a></li>
</ul>
<ul class="hoverbox3">
<li><a href="#"><img src="http://p2p.wrox.com/images/Stock3.jpg"><img src="http://p2p.wrox.com/images/gvbp.png" class="preview"></a></li>
</ul>
<ul class="hoverbox4">
<li><a href="#"><img src="http://p2p.wrox.com/images/Stock4.jpg"><img src="http://p2p.wrox.com/images/fre.png" class="preview"></a></li>
</ul>
</div>
<div id="footer">
<h1>Grow your portfolio like you never have, subscribe free above</h1>
</div>
</div>
<?php
mysql_close($cid);
?>


Can anybody help?

Last edited by gueland; February 10th, 2010 at 01:45 PM.. Reason: missed information
 
Old March 19th, 2010, 04:15 PM
Friend of Wrox
 
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

Instead of using $REQUEST_METHOD, use $_SERVER['REQUEST_METHOD'].





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help in inserting to Database?? Hannibal Pro JSP 0 March 14th, 2006 07:11 AM
inserting into database shieldsteven VS.NET 2002/2003 0 November 11th, 2004 07:44 PM
inserting into database shieldsteven VS.NET 2002/2003 8 October 20th, 2004 05:33 PM
Emailing File from SQL Server Database [email protected] VB How-To 0 July 24th, 2004 09:06 AM
Inserting into database ZiqXx C# 6 July 5th, 2004 04:13 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.