Help with SQL

Help with SQL

Posted by: intent
Posted on: 2005-05-06 03:13:00

I am not sure this is a beginner question but, not really sure what other section it would fit.

Normally I search and search till I find the answer myself but this time it seems the answers are just confusing.

I am trying to setup a form mail (which isn't a problem that I think I can do with lil effort) I have setup one before on another website. My question is...we are wanting the answers that are submitted from the form mail to go into a database and be stored somewhere. I was told this could be done with a SQL. Could someone please point me to a place where I could find information on how to set this up if it is truley possible. Always before I just entered the information into a Access database that I created myself but it would be easier if it was all done automatically.

Thank you in advance for your time.



One Smile can make a difference

Re: Help with SQL

Posted by: scjessey
Posted on: 2005-05-06 04:52:00

In reply to:

we are wanting the answers that are submitted from the form mail to go into a database and be stored somewhere.


Given a form like this one:

<?php
session_start();
$title = ($_SESSION['title']) ? $_SESSION['title'] : "";
$first_name = ($_SESSION['first_name']) ? $_SESSION['first_name'] : "";
$last_name = ($_SESSION['last_name']) ? $_SESSION['last_name'] : "";
$email = ($_SESSION['email']) ? $_SESSION['email'] : "";
$day_phone = ($_SESSION['day_phone']) ? $_SESSION['day_phone'] : "";
$night_phone = ($_SESSION['night_phone']) ? $_SESSION['night_phone'] : "";
$cell_phone = ($_SESSION['cell_phone']) ? $_SESSION['cell_phone'] : "";
$street1 = ($_SESSION['street1']) ? $_SESSION['street1'] : "";
$street2 = ($_SESSION['street2']) ? $_SESSION['street2'] : "";
$city = ($_SESSION['city']) ? $_SESSION['city'] : "";
$state = ($_SESSION['state']) ? $_SESSION['state'] : "";
$zip = ($_SESSION['zip']) ? $_SESSION['zip'] : "";
$comments = ($_SESSION['comments']) ? $_SESSION['comments'] : "";
$error = ($_SESSION['error']) ? $_SESSION['error'] : "";

header("Content-Type: text/html;charset=UTF-8");

?>
<!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" xml:lang="en" lang="en">

<head>
<title>Form</title>
</head>

<body>
<h1>Contact Form</h1>
<form method="post" action="submit.php">
<?php
if($error != "") {
print " <p class="error">$error</p>n";
}
?>
<p>Please note: Required information is marked with an asterisk (*).</p>
<dl>
<dt>Title*:</dt>
<dd><input type="text" size="40" name="title" value="<?php print $title ?>" /></dd>
<dt>First Name*:</dt>
<dd><input type="text" size="40" name="first_name" value="<?php print $first_name ?>" /></dd>
<dt>Last Name*:</dt>
<dd><input type="text" size="40" name="last_name" value="<?php print $last_name ?>" /></dd>
<dt>Email Address*:</dt>
<dd><input type="text" size="40" name="email" value="<?php print $email ?>" /></dd>
<dt>Daytime Phone Number*:</dt>
<dd><input type="text" size="40" name="day_phone" value="<?php print $day_phone ?>" /></dd>
<dt>Evening Phone Number*:</dt>
<dd><input type="text" size="40" name="night_phone" value="<?php print $night_phone ?>" /></dd>
<dt>Cell Phone Number:</dt>
<dd><input type="text" size="40" name="cell_phone" value="<?php print $cell_phone ?>" /></dd>
<dt>Street Address 1*:</dt>
<dd><input type="text" size="40" name="street1" value="<?php print $street1 ?>" /></dd>
<dt>Street Address 2:</dt>
<dd><input type="text" size="40" name="street2" value="<?php print $street2 ?>" /></dd>
<dt>City*:</dt>
<dd><input type="text" size="40" name="city" value="<?php print $city ?>" /></dd>
<dt>State*:</dt>
<dd><input type="text" size="40" name="state" value="<?php print $state ?>" /></dd>
<dt>Zip*:</dt>
<dd><input type="text" size="40" name="zip" value="<?php print $zip ?>" /></dd>
<dt>Comments:</dt>
<dd><textarea name="comments" rows="10" cols="30"><?php print $comments ?></textarea></dd>
</dl>
<p>
<input type="hidden" name="error" value="" />
<input type="submit" name="submit" value="Send Your Information" />
</p>
</form>
</body>

</html>

You could feed the data into a database with this:

<?php

session_start();
session_register("title");
session_register("first_name");
session_register("last_name");
session_register("email");
session_register("day_phone");
session_register("night_phone");
session_register("cell_phone");
session_register("street1");
session_register("street2");
session_register("city");
session_register("state");
session_register("zip");
session_register("comments");
session_register("error");

// check if form was submitted
if(!$_POST) {
header("Location: /");
exit;
}

header("Content-Type: text/html;charset=UTF-8");

// convert POST variables into local variables
$title = $_POST['title'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$day_phone = $_POST['day_phone'];
$night_phone = $_POST['night_phone'];
$cell_phone = $_POST['cell_phone'];
$street1 = $_POST['street1'];
$street2 = $_POST['street2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$comments = $_POST['comments'];
$error = $_POST['error'];

// create session variables
$_SESSION['title'] = $title;
$_SESSION['first_name'] = $first_name;
$_SESSION['last_name'] = $last_name;
$_SESSION['email'] = $email;
$_SESSION['day_phone'] = $day_phone;
$_SESSION['night_phone'] = $night_phone;
$_SESSION['cell_phone'] = $cell_phone;
$_SESSION['street1'] = $street1;
$_SESSION['street2'] = $street2;
$_SESSION['city'] = $city;
$_SESSION['state'] = $state;
$_SESSION['zip'] = $zip;
$_SESSION['comments'] = $comments;
$_SESSION['error'] = $error;

$regexp = "^([_a-z0-9-] )(.[_a-z0-9-] )*@([a-z0-9-] )(.[a-z0-9-] )*(.[a-z]{2,4})$";

// check required fields
if($title == "") {
$_SESSION['error'] = "You must fill in all required information. You have not entered a title.";
header("Location: /contact/");
exit;
}
if($first_name == "") {
$_SESSION['error'] = "You must fill in all required information. You have not entered a first name.";
header("Location: /contact/");
exit;
}
if($last_name == "") {
$_SESSION['error'] = "You must fill in all required information. You have not entered a last name.";
header("Location: /contact/");
exit;
}
if($email == "") {
$_SESSION['error'] = "You must fill in all required information. You have not entered an email address.";
header("Location: /contact/");
exit;
}
// check if email address is valid
if(!eregi($regexp,$email)) {
$_SESSION['error'] = "The email address you have entered appears to be invalid. Please enter a valid email address.";
header("Location: /contact/");
exit;
}
if($day_phone == "") {
$_SESSION['error'] = "You must fill in all required information. You have not entered a daytime phone number.";
header("Location: /contact/");
exit;
}
if($night_phone == "") {
$_SESSION['error'] = "You must fill in all required information. You have not entered an evening phone number.";
header("Location: /contact/");
exit;
}
if($street1 == "") {
$_SESSION['error'] = "You must fill in all required information. You have not entered a street address.";
header("Location: /contact/");
exit;
}
if($city == "") {
$_SESSION['error'] = "You must fill in all required information. You have not entered a city.";
header("Location: /contact/");
exit;
}
if($state == "") {
$_SESSION['error'] = "You must fill in all required information. You have not entered a state.";
header("Location: /contact/");
exit;
}
if($zip == "") {
$_SESSION['error'] = "You must fill in all required information. You have not entered a zip code.";
header("Location: /contact/");
exit;
}

// If the script gets down to here, the user has entered everything correctly!

// get IP address
$ip = $_SERVER['REMOTE_ADDR'];

// make timestamp
$now = time();
$timestamp = date("YmdHis",$now);

// initialize database variables
$hostname = ""; // your hostname
$username = ""; // your database username
$password = ""; // your database password
$database = ""; // the name of your database

// connect to database
$link = mysql_connect($hostname,$username,$password);
mysql_select_db($database);

// insert information into database
$sql = "insert into contacts (timestamp, ip, title, first_name, last_name, email, day_phone, night_phone, cell_phone,
street1, street2, city, state, zip, comments) values ('$timestamp', '$ip', '$title', '$first_name', '$last_name',
'$email', '$day_phone', '$night_phone', '$cell_phone', '$street1', '$street2', '$city', '$state', '$zip', '$comments')";
$query = mysql_query($sql,$link) or die("Could not insert data: ".mysql_error());

// compose email
$message = "Thank you for submitting comments. Here is the information you sent to us:nn";
$message = $message."Title: $titlen";
$message = $message."First Name: ".stripslashes($first_name)."n";
$message = $message."Last Name: ".stripslashes($last_name)."n";
$message = $message."Email: $emailn";
$message = $message."Daytime Phone Number: $day_phonen";
$message = $message."Evening Phone Number: $night_phonen";
$message = $message."Cellular Phone: $cell_phonen";
$message = $message."Street Address 1: ".stripslashes($street1)."n";
$message = $message."Street Address 2: ".stripslashes($street2)."n";
$message = $message."City: ".stripslashes($city)."n";
$message = $message."State: $staten";
$message = $message."Zip Code: $zipn";
$message = $message."Comments:n".stripslashes($comments);

// send mail
$to = "$email";
$from = ""; // your email address
$subject = "Contact Repsonse";
mail ( "$to", "$subject", "$message", "$from" );

// redirect to home page once finished
header("Location: /");
?>

That script will add the information into a database, and then send a confirmation email back to the sender. It'll also send the user back to the form if required fields are missing (which is why I've made use of session variables).

You could access the information from a simple table:

<?php

// initialize database variables
$hostname = ""; // your hostname
$username = ""; // your database username
$password = ""; // your database password
$database = ""; // the name of your database

// connect to database
$link = mysql_connect($hostname,$username,$password);
mysql_select_db($database);

// get contacts data
$sql = "select * from contacts order by timestamp desc";
$query = mysql_query($sql,$link) or die("Could not retrieve contacts: ".mysql_error());

function make_date($timestamp) {
$yr = substr($timestamp,0,4);
$mo = substr($timestamp,4,2);
$dy = substr($timestamp,6,2);
$hr = substr($timestamp,8,2);
$mn = substr($timestamp,10,2);
$sc = substr($timestamp,12,2);
$timestamp = date("l, F d, Y",mktime($hr,$mn,$sc,$mo,$dy,$yr));
return $timestamp;
}

header("Content-Type: text/html;charset=UTF-8");

?>
<!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" xml:lang="en" lang="en">

<head>
<title>List of contacts</title>
</head>

<body>
<h1>List of Contacts</h1>
<table cellspacing="0px">
<thead>
<tr>
<th>Title</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Daytime Phone</th>
<th>Evening Phone</th>
<th>Cell Phone</th>
<th>Street Address 1</th>
<th>Street Address 2</th>
<th>City</th>
<th>State</th>
<th>Zip</th>
<th>Comments</th>
<th>Date submitted</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysql_fetch_assoc($query)) {
$title = $row['title'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$email = $row['email'];
$day_phone = $row['day_phone'];
$night_phone = $row['night_phone'];
$cell_phone = $row['cell_phone'];
$street1 = $row['street1'];
$street2 = $row['street2'];
$city = $row['city'];
$state = $row['state'];
$zip = $row['zip'];
$comments = $row['comments'];
$timestamp = $row['timestamp'];
print " <tr>n";
print " <td>$title</td>n";
print " <td>$first_name</td>n";
print " <td>$last_name</td>n";
print " <td><a href="mailto:$email">$email</a></td>n";
print " <td>$day_phone</td>n";
print " <td>$night_phone</td>n";
print " <td>$cell_phone</td>n";
print " <td>$street1</td>n";
print " <td>$street2</td>n";
print " <td>$city</td>n";
print " <td>$state</td>n";
print " <td>$zip</td>n";
print " <td>".nl2br($comments)."</td>n";
print " <td>".make_date($timestamp)."</td>n";
print " </tr>n";
}
?>
</tbody>
</table>
</body>

</html>

I hope you find that useful.

Re: Help with SQL

Posted by: intent
Posted on: 2005-05-06 19:28:00

Thank you so much that will definately help LOTS.

Is there a site somewhere that I can read up on the how to's on developing SQL databases (for website builders/designers?)

I am someone that loves to learn new things and this is something that has me stumped.



One Smile can make a differenceEdited by intent on 05/06/05 10:48 PM (server time).

Re: Help with SQL

Posted by: knitsmitts
Posted on: 2005-05-07 05:24:00

For basic information about relational databases, take a look at Marc-Jason Dominus' Short Guide to DBI http:// http://www.perl.com/pub/a/1999/10/DBI.html. It is oriented toward Perl rather then PHP, but gives you a lot of basics about relational databases.

For learning more about MySQL, the searchable documentation available on their site is quite helpful: http://dev.mysql.com/doc/mysql/en/index.html


Re: Help with SQL

Posted by: intent
Posted on: 2005-05-07 09:26:00

Thank you both very much.

I have printed out a lot of the materials and going to go find a nice spot and read hopefully I will figure it out (crosses fingers.)

You have both been very helpful and kind.

One Smile can make a difference

Re: Help with SQL

Posted by: guice
Posted on: 2005-05-08 11:21:00

suggestion; add 'isset' in front of all them session checks on the initial lines. That way it'll pass PHP's ~E_NOTICE error checking.

In reply to:

$title = isset($_SESSION['title']) ? $_SESSION['title'] : "";



W/out that, you'll get 'Notice: using undefined index' for each line.

Re: Help with SQL

Posted by: scjessey
Posted on: 2005-05-08 14:24:00

In reply to:

W/out that, you'll get 'Notice: using undefined index' for each line.


Actually, you don't get that error. Each of those lines is making use of the ternary operator, which is checking for the existence of the existence of the session variable. If the variable does not exist, an empty string is substituted.

Re: Help with SQL

Posted by: decswxaqz
Posted on: 2005-05-08 16:02:00

You could always put @ at the start of the declaration. But I prefer to use the other method of
isset($_SESSION) or $_POST... that way I can give it a default value if it doesn't exist. Useful for things like carrying page numbers when a user has just entered a multi-page website and doesn't have a variable in the address bar.

Re: Help with SQL

Posted by: scjessey
Posted on: 2005-05-08 16:21:00

In reply to:

I can give it a default value if it doesn't exist.


That is what the script is doing. For example:

$title = ($_SESSION['title']) ? $_SESSION['title'] : "";

In English, that says "if the session variable 'title' exists, assign that value to $title. Otherwise, give $title a default value of "" (empty string)." There is no need to supress any error message, because no error message is given.

Tags: form mailsqlaccess databaseconfusingfithelp