Jump to content

[SOLVED] creating a contact form that writes to database


ryanfc

Recommended Posts

Ok I got this tutorial on writing to a database out of a book I have: PHP for the World Wide Web.  However,  it is not working.  Can anyone help me with what I am doing wrong?  Here is the code for the page.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>myevvspace.com | premium local listings</title>

<link href="myevvspace_style_home.css" rel="stylesheet" type="text/css" />
<script src="navigation.js" type="text/javascript" language="javascript"></script>

<?php
require('taken out for security');

?>

</head>

<body>
<?php

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

	if ($dbc = mysql_connect ($dbhost, $dbuser, $dbpass))
	{
	if (!mysql_select_db ('contactform'))
	{
	die ('could not select' . mysql_error() . ' ');
	}
} else {
die ('coud not connect' . mysql_error() . ' ');
}

$query = "INSERT INTO contactform (contact_id, name, email) VALUES (0, '{$_POST['name']}', '{$_POST['email']}')";

if (mysql_query ($query)) {
print 'added';
} else {
print "not added" . mysql_error() . "the query was $query";
}

mysql_close();
}
?>
<form  method="POST" action="contact.php">
  <p>
    <input name="name" type="text" />
</p>
  <p>
    <input name="email" type="text" />
  </p>

  <p> </p>
  <p>
    <input type="submit" name="Submit" value="Submit" />
  </p>
</form>

</body>
</html>

 

Thanks.

i have a database created and a table in that database called contactform.  that table has 3 fields contact_id (the key), name and email.  When a user goes to the contact.php page i want them to fill out the form and when it they hit submit I want it to send the name and e-mail to the contactform table.  I eventually want the page to have more fields and have it write just the name and email address to the database and send all fields in an e-mail.  But first I am just trying to get it to write to the database.  Does that make sense?

 

No I do not.  I added those lines and have changed a few things so here is the new code for the page.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>myevvspace.com | premium local listings</title>

<link href="myevvspace_style_home.css" rel="stylesheet" type="text/css" />
<script src="navigation.js" type="text/javascript" language="javascript"></script>

<?php
require('///////');

?>

</head>

<body>
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

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

	if ($dbc = mysql_connect ($dbhost, $dbuser, $dbpass))
	{
	if (!mysql_select_db ($dbname))
	{
	die ('could not select' . mysql_error() . ' ');
	}
} else {
die ('coud not connect' . mysql_error() . ' ');
}

$query = "INSERT INTO contactform (contact_id, name, email) VALUES (0, '{$_POST['name']}', '{$_POST['email']}')";

if (mysql_query ($query)) {
print 'added';
} else {
print "not added" . mysql_error() . "the query was $query";
}

mysql_close();
}
?>
<form  method="POST" action="">
  <p>
    <input name="name" type="text" />
</p>
  <p>
    <input name="email" type="text" />
  </p>

  <p> </p>
  <p>
    <input type="submit" name="Submit" value="Submit" />
  </p>
</form>

</body>
</html>

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.