Jump to content

Simple PHP mySQL Contact Form


curvdaryn

Recommended Posts

Hey guys,

 

I have this PHP contact form with 3 fields that imports it a mySQL into a database as well as emailing the data. I have everything setup but I think my code is a little off, I know its not a mySQL issue since it seems to connect alright.

 

If someone could please help me I would greatly appreciate it. If I was more experienced with PHP the problem would probably scream out at me....

 

The code is below and a zip is attached.

 

Thanks in Advance,

Daryn

 

 

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/149758-simple-php-mysql-contact-form/
Share on other sites

oops.. code

 

<?php
if(isset($_POST['submit'])) {

$to = "[email protected]";
$subject = "Promotional Download";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$date_book_was_bought = $_POST['date_book_was_bought'];
$headers = "FROM: $email_field";
$ip = gethostbyname($_SERVER['REMOTE_ADDR']);


mysql_select_db($database, $connection);

$insert_query = sprintf("INSERT INTO contacts (name, email, date_book_was_bought, date, ip) VALUES (%s, %s, %s, %s, NOW(), %s)",
						sanitize($name, "text"),
						sanitize($email, "text"),
						sanitize($date_book_was_bought, "text"),
						sanitize($ip, "text"));

$result = mysql_query($insert_query, $connection) or die(mysql_error());

if($result)
{


	//headers and subject
	$headers  = "MIME-Version: 1.0rn";
	$headers .= "Content-type: text/html; charset=iso-8859-1rn";



$body = "Name: $name_field\nE-Mail: $email_field \nDate Book was Purchased: $date_book_was_bought";

echo "Data has been submitted to $to!";
mail($to, $subject, $body, $headers);

} else {

echo "blarg!";

}
function sanitize($value, $type)
{
  $value = (!get_magic_quotes_gpc()) ? addslashes($value) : $value;

  switch ($type) {
    case "text":
      $value = ($value != "") ? "'" . $value . "'" : "NULL";
      break;
    case "long":
    case "int":
      $value = ($value != "") ? intval($value) : "NULL";
      break;
    case "double":
      $value = ($value != "") ? "'" . doubleval($value) . "'" : "NULL";
      break;
    case "date":
      $value = ($value != "") ? "'" . $value . "'" : "NULL";
      break;
  }

  return $value;
}
?>

got a little farther with the code and I am at this point:

 

<?php

if(isset($_POST['submit'])) {

 

$to = "[email protected]";

$subject = "Promotional Download";

$name_field = $_POST['name_field'];

$email_field = $_POST['email_field'];

$date_book_was_bought = $_POST['date_book_was_bought'];

$headers = "FROM: $email_field";

$ip = gethostbyname($_SERVER['REMOTE_ADDR']);

 

 

mysql_select_db($database, $connection);

 

$insert_query = sprintf("INSERT INTO contacts (name_field, email_field, date_book_was_bought, date, ip) VALUES (%s, %s, %s, %s, NOW(), %s)",

sanitize($name_field, "text"),

sanitize($email_field, "text"),

sanitize($date_book_was_bought, "text"),

sanitize($ip, "text"));

 

$result = mysql_query($insert_query, $connection) or die(mysql_error());

 

if($result)

{

 

 

//headers and subject

$headers = "MIME-Version: 1.0rn";

$headers .= "Content-type: text/html; charset=iso-8859-1rn";

 

 

 

$body = "Name: $name_field\nE-Mail: $email_field \nDate Book was Purchased: $date_book_was_bought";

 

echo "Data has been submitted to $to!";

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

 

} else {

 

echo "blarg!";

 

}

function sanitize($value, $type)

{

$value = (!get_magic_quotes_gpc()) ? addslashes($value) : $value;

 

switch ($type) {

case "text":

$value = ($value != "") ? "'" . $value . "'" : "NULL";

break;

case "long":

case "int":

$value = ($value != "") ? intval($value) : "NULL";

break;

case "double":

$value = ($value != "") ? "'" . doubleval($value) . "'" : "NULL";

break;

case "date":

$value = ($value != "") ? "'" . $value . "'" : "NULL";

break;

}

 

return $value;

}

}

?>

 

with this error in return:

 

 

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/profits/public_html/mailer/mailer3.php on line 13

 

Fatal error: Call to undefined function sanitize() in /home/profits/public_html/mailer/mailer3.php on line 16

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.