Jump to content

not inserting to the db


lovelee

Recommended Posts

This is my first time coding php to mysql db, and am stuck to find where I have gone wrong.

 

The form information is not getting to the db tables nor is it sending me an email with the user info.

 

Would anyone be able to see where I have gone wrong? I was using a tutorial, though I have made changes to the original file.

 


<?php
//include the connection file

require_once('connection.php');

//save the data on the DB and send the email

if(isset($_POST['action']) && $_POST['action'] == 'submitform')
{
//recieve the variables

$name = $_POST['name'];
$number = $_POST['number'];
$email = $_POST['email'];
$address = $_POST['address'];
$job = $_POST['job'];
$ip = gethostbyname($_SERVER['REMOTE_ADDR']);

//save the data on the DB

mysql_select_db($database_connection, $connection);

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

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

if($result)
{
	//send the email

	$to = "[email protected]";
	$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: ".$name." <".$email.">\r\n";

	$body = "New contact<br />";
	$body .= "Name: ".$name."<br />";
	$body .= "Number: ".$number."<br />";
	$body .= "Email: ".$email."<br />";
	$body .= "Address: ".$address."<br />";
	$body .= "job: ".$job."<br />";
	$body .= "IP: ".$ip."<br />";

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

	//ok message

	echo "Your message has been sent";
}
}

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;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Content Form</title>

</head>
<body>

<form id="contact" name="contact" action="contact.php" method="post">

<p><label>Name: <input type="text" id="name" name="name" value="" /></label></p>

<p><label>Contact Number: <input type="text" id="number" name="number" value="" /></label></p>

<p><label>Email: <input type="text" id="email" name="email" value="" /></label></p>

<p><label>Address of Jobs: <input type="text" id="address" name="address" value="" /></label></p>

<p><label>Description of proposed job.<br> 
(Examples include: prepare sloping home site for concrete foundations, or, lift and remove existing concrete driveway):<br />
<textarea id="job" name="job"></textarea><!---->
</label></p>
<!--<p>
  <label>
  <input type="checkbox" name="priceGuide" id="priceGuide">
  <strong>Yes!</strong> Send me some pricing information regarding Matnic Excavations’ general hire services</label>
</p>-->
<input type="hidden" id="action" name="action" value="submitform" />

<p><input type="submit" id="submit" name="submit" value="Submit" /> <input type="reset" id="reset" name="reset" value="Reset" /></p>

</form>

<p> </p>

<script type="text/javascript">

</script>
</body>
</html>

 

thanks for any help!

Link to comment
https://forums.phpfreaks.com/topic/162326-not-inserting-to-the-db/
Share on other sites

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.