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.

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.