Adding to Database via HTML form
I want to allow users to add details to my MYSQL database using Html Forms. Can this be done and if so how.
I want to allow users to add details to my MYSQL database using Html Forms. Can this be done and if so how.
Maverick posted this at 06:32 — 15th February 2001.
They have: 334 posts
Joined: Dec 1999
You need some sort of intermediary language to communicate with the database, simple HTML doesn't cut it. If you're using MySQL, I assume you have PHP available since they play so nicely together. So
<HTML>
<HEAD>
<TITLE>test</TITLE>
</HEAD
<BODY>
<?PHP
// SET THE VARIABLES
$db = "databasename";
$table = "tablename";
$host = "databaseserver";
$user = "databaseusername";
$pw = "databasepassword";
if ($submit) {
mysql_connect($host,$user,$pw) or die ("Unable to connect");
mysql_select_db("$db") or die("Unable to open database");
$query = "insert into $table (name, address) VALUES ('$name', '$address') or die ("INSERT failed");
$result = mysql_query($query);
echo "data inserted";
} else {
echo "<FORM METHOD=\"post\" ACTION=$PHP_SELF>";
echo "Name: <INPUT type=\"text\" NAME=\"name\"><P>";
echo "Address: <INPUT type=\"password\" NAME=\"address\"><P>";
echo "<input type=\"submit\" value=\"Submit\" name=\"submit\">";
echo "</FORM>";
}
</BODY>
</HTML>
I didn't test that, so there might be a typo in there somewhere, but that's a nice simple way to do it. And for security purposes, it's best to set the values in a protected file and include it into the page that's running the script.
Gman posted this at 12:18 — 15th February 2001.
They have: 5 posts
Joined: Feb 2001
Thanks for that Mavrick.
I will post feedback later if it works.
Thanks
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.