A simple tutorial that could help
Hi
If you want a simple script to enter data from a form into a database, here are some scripts you can use or actually improve on to do what u want. I'm sure there are better scripts around, though.
For my purposes, I will create
- sample database table: "yourdbtable" (I know u might already be having one)
- the database script I call "db.php"
- a form for submiting the data: form.php
- a script to process it: postdata.php
The 3 php files must be in the same folder, unless you know what you are doing.
I put some basic form validation, with the database data submission succeeding only if first name, second name & "choice" are selected.
After successfull submission, you are returned to the form with a confirmation message.
I tried to comment to show what some portions of code do.
I tested & it functions. If you rename any of them, make sure you change all places in the scripts any of the names appears or it will not work.
I hope it helps, or at least someone else may find it useful.
The database table:
Code:
CREATE TABLE `yourdbtable` (
`id` tinyint(50) NOT NULL auto_increment,
`fname` varchar(50) NOT NULL,
`sname` varchar(50) NOT NULL,
`choice` varchar(50) NOT NULL,
`comment` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
The db.php:
Code:
<?php
$dbhost = "localhost"; //your database host server, eg. localhost
$dbname = "yourdatabasename";
$dbusername = "yourdatabseusername";
$dbpassword = "yourdbpassword";
$dbconnect = mysql_pconnect("$dbhost", "$dbusername", "$dbpassword") or die("Could not connect to server!");
$db = mysql_select_db("$dbname", $dbconnect) or die(mysql_error());
?>
I will call the form "form.php".
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title></title>
</head>
<body>
<h2>A sample form to post data to a database</h2>
<?
if($error1 || $error2 || $error3)
{
echo "<font color='red'><b>Please fill in the following: ";
if($error1)
{echo $error1;}
if($error2)
{echo $error2;}
if($error3)
{echo $error3;}
echo "</b></font>";
}
?>
<?
if($posted)
{echo $posted;}
?>
<form name="form1" method="post" action="postdata.php">
<table width="500">
<caption>A simple form to post Data</caption>
<tr>
<td>First name</td><td><input type="text" name="fname"></td>
</tr>
<tr>
<td>Second name</td><td><input type="text" name="sname"></td>
</tr>
<tr>
<td>Your Choices</td><td><select name="choice">
<option value=""></option>
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
<option value="3">Choice 3</option>
<option value="4">Choice 4</option>
<option value="5">Choice 5</option>
</select></td>
</tr>
<tr>
<td>Comment</td><td>
<textarea name="comment"></textarea></td>
</tr>
<tr>
<td><input type="submit"></td><td><input type="reset"></td>
</tr>
</table>
</form>
</body>
</html>
The script to get the data into the data base which I called "postdata.php":
Code:
<?php
include "db.php";
//Change post field values into variables
$fname = $_POST['fname'];
$sname = $_POST['sname'];
$choice = $_POST['choice'];
$comment = $_POST['comment'];
//strip slashes
$fname = stripslashes($fname);
$sname = stripslashes($sname);
$choice = stripslashes($choice);
$comment = stripslashes($comment);
//If you want fist name, second name & choice to be compulsory, include this to check if something has not been filled in.
//Simply add comment in the same if u want to be compulsory.
if((!$fname) || (!$sname) || (!$choice))
{
if(!$fname)
{$error1 = "first name - ";}
if(!$sname)
{$error2 = "second name - ";}
if(!$choice)
{$error3 = "choice";}
include "form.php";
exit(); //We exit so no incomplete data is entered
}
//If all form info is there, enter data into database
$postdata = mysql_query("INSERT INTO yourdbtable (id, fname, sname, choice, comment) values('', '$fname', '$sname', '$choice', '$comment')") or die(mysql_error());
if(!$postdata)
{
echo "<font color='red>'<b>The data could not be entered into database. Please contact the webmaster.</b></font>";
}
else
{
$id = mysql_insert_id();
}
$posted = "<font color='red'><b>Your data has been successfully entered into database.</b></font>";
include "form.php";
?>