Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

insert variables fram html page to mysql db

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi - i'm new to mysql and php. but im very funned of both. i have my first db up and running, and i can make a select ... query to print the content of my db on a html page.

my next step is now to send a form from my html page to my db - how can i do that?
 
Here's an example of inserting data... Include your db connection stuff, of course. This just includes the form and the SQL INSERT statement for contact information (for American addresses, anyway).

In terms of going from an HTML form to your db, the form input NAME (eg name="firstname" below) becomes your variable name in PHP (INSERT into contact values (NULL,'$firstname',...) (first value is NULL in this case because it's an auto_increment primary key).

Probably isn't the greatest example of PHP code either, but it works. There are a lot of tutorials out there on MySQL/PHP that can give you more information.

<?php
if(!$enter) {
?>
<H2 align=center>Contact</H2>
<FORM action=<?php echo $PHP_SELF ?> method=post>
<P align=center>
<TABLE BORDER=0 ALIGN=center WIDTH=500>
<TR><TD>First Name</TD>
<TD><INPUT TYPE=text name=&quot;firstname&quot; size=36></TD>
</TR>
<TR><TD>Last Name</TD>
<TD><INPUT TYPE=text name=&quot;lastname&quot; size=36></TD>
</TR>
<TD>Email Address</TD>
<TD><INPUT TYPE=text name=&quot;email&quot; size=36></TD>
</TR>
<TR>
<TD>Address</TD>
<TD><INPUT TYPE=text name=&quot;address&quot; size=36></TD>
</TR>
<TR>
<TD>Address line 2</TD>
<TD><INPUT TYPE=text name=&quot;address2&quot; size=36></TD>
</TR>
<TR>
<TD>City, State</TD>
<TD><INPUT TYPE=text name=&quot;city&quot; size=32>,<INPUT TYPE=text name=&quot;state&quot; size=3></TD>
</TR>
<TR>
<TD>ZIP</TD>
<TD>
<INPUT TYPE=text name=&quot;zip&quot; size=5>-<INPUT TYPE=text name=&quot;zipextra&quot; size=4></TD>
</TR>
<TR>
<TD>Phone</TD>
<TD>
<INPUT TYPE=text name=&quot;phone&quot; size=17></TD>
</TR>
<TR ALIGN=center>
<TD COLSPAN=2><INPUT TYPE=submit name=&quot;enter&quot; value=&quot;Alright&quot;> <INPUT TYPE=reset name=&quot;reset&quot; value=&quot;Clear&quot;></TD>
</TR>
</TABLE>
</FORM>

<?php
}
if ($enter) {
$sql = &quot;INSERT into contacts VALUES (NULL,'$firstname','$lastname','$email','$address','$address2','$city','$state','$zip-$zipextra','$phone')&quot;;
$result = mysql_query($sql);
if(!$result) {
echo(&quot;<P>Oops: &quot;.mysql_error());
} else {
echo(&quot;<P>Contact information saved. Thanks!&quot;);
}
}
?>

Hope this helps you out some. Matt
matt@paperlove.org
If I can help, I will.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top