Page 1 of 1

sending form data to a database

Posted: Sun Feb 13, 2005 1:15 pm
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.

Posted: Sun Feb 13, 2005 1:27 pm
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')");

Posted: Sun Feb 13, 2005 4:37 pm
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

Posted: Sun Feb 13, 2005 4:46 pm
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

Posted: Sun Feb 13, 2005 5:16 pm
by John Cartwright
1) Either database does not exist
2) You have mispelled your database name

Posted: Sun Feb 13, 2005 5:25 pm
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

Posted: Sun Feb 13, 2005 5:26 pm
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

Posted: Sun Feb 13, 2005 6:07 pm
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