sending form data to a database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
philweb
Forum Newbie
Posts: 5
Joined: Sun Feb 13, 2005 1:06 pm

sending form data to a database

Post by philweb »

I have setup an email form, which is working fine but because I have a little knowledge of PHP I cannot do what I want to which is as follows.

I want to be able to have the email form data saved to variables

Then sent to the database so that I can retreive data later if needs be.

Also is it possible to re-use this kind of script to send data from any other form.
the code I have used is as follows.

contacttest.php

Code: Select all

<body>
<form name="form1" method="post" action="sendmailtest.php">
<table width="99%" border="0" align="center" cellpadding="1" cellspacing="0">
<tr>
<td width="44%"><div align="right">Your Name:</div></td>
<td width="56%"><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td><div align="right">Your E-Mail:</div></td>
<td><input name="mail" type="text" id="mail"></td>
</tr>
</table>
<div align="center"><br>
Comment:<br>
<br>
<textarea name="comment" cols="50" rows="6" id="comment"></textarea>
<br>
<br>
<table width="99%" border="0" cellspacing="0" cellpadding="2">
<tr>
<td>
<div align="right">
<input type="submit" value="Send Form">
</div></td>
<td><input type="reset" name="Submit2" value="Reset Form"></td>
</tr>
</table>
</div>
</form>
<br>
<br>
<br>
<br>
<br>
</body>

and the php script for the email (sendmail)  sendmailtest.php

<body>
<?php 
$yourmail = "me@pellsweb.co.uk"; // put your e-mail here 
$t1 = $_POST&#1111;'name'];
$t2 = $_POST&#1111;'mail'];
$t3 = $_POST&#1111;'comment']; 
$data = "This is an automated mail. Someone submited the form and this is what he had to say:\nHis Name: $t1\nHis mail: $t2\nComment: $t3\n"; 
mail ($yourmail, "The Form", $data); // The Form mean subject of mail you will receive 
die ("Form submited\n"); 
?> 

<br>
<br>
<br>
<br>
<br>
</body>
any help would be appreciated

thanks philweb


feyd | time to read the posting guidelines.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

well if you want to save this to a database before the die() command do this

Code: Select all

$dbhost = '****';
$dbusername = '****';
$dbpassword = '****';
$dbname = '****';

$connect = mysql_connect($dbhost, $dbusername, $dbpassword) or die ('Cant connect to MySQL');
mysql_select_db($dbname,$connect) or die ("Could not select database");

mysql_query("INSERT INTO table (email, name, comment) VALUES ('$t2', '$t1', '$t3')");
philweb
Forum Newbie
Posts: 5
Joined: Sun Feb 13, 2005 1:06 pm

Post by philweb »

so that would come into the script here,

mail you will receive

<-------------------------------------

die ("Form submited\n");

i will try that thanks a lot
philweb
Forum Newbie
Posts: 5
Joined: Sun Feb 13, 2005 1:06 pm

Post by philweb »

well i tried it and it sates could not selct database

my database is called pellsweb and the table contacts it seems to do evrything but still throws the could not select database, have i missed something like a comma ,


at least it isnt the sql errors i get normally


thanks phil
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

1) Either database does not exist
2) You have mispelled your database name
philweb
Forum Newbie
Posts: 5
Joined: Sun Feb 13, 2005 1:06 pm

Post by philweb »

ok i have finally got it working i think, it says form submitted there are no errors, however the database, table isn't getting the information, i started with one contact and have tried the php pages several times resulting in still having one database entry. This cant be right
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

try

Code: Select all

mysql_query("INSERT INTO table (email, name, comment) VALUES ('$t2', '$t1', '$t3')") or die(mysql_error());
and tell me what that outputs
philweb
Forum Newbie
Posts: 5
Joined: Sun Feb 13, 2005 1:06 pm

Post by philweb »

thanks a lot you have been a big help,
I thought i was gonna have to kill the monitor for a minute there
Post Reply