Insert Record to a Database with PHP
To add data to a database table, you need an existing database plus the table to add the data to.
Let us assume that you have mysql database with the following table:
feeds
| Field Name | Data Type | Field Size |
| user_id | Autonumber | -- |
| Name | Text | 45 |
| Comments | Text | 200
|
To add a data to the table feeds, first we will have to create a form to enter the data and then open the database and make the connection.
Here is the form to enter the data before adding to the database:
| HTML PHP Form Example |
<html>
<head>
<title> Adding to database example </title>
</head>
<body>
<form name="form" method="post" action="save.php">
Name: <input type="text" name="name" maxlength="45"> <br>
Comments: <textarea cols="20" rows="8" name="comments" maxlength="200"> </textarea><br>
<input type="submit" name="Save" value="Submit">
</form>
</body>
</html>
Execution Result
|
This form stright forward, html form that submits it's information to another file save.php for process.
Now, we will create connection file and call it connect.inc.
<?php
$dbh=mysql_connect ("localhost", "root", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("feeds");
?> It's good programming practise to seperate your connection file to include any php file that will require connection to your sql database.
The following example checks if the form fields are empty, checks if the comment with the same name already exists in the database and inserts a record if the both cases are false:
| save.php |
<?php
include("connect.inc");
$msg="";
$save="";
if (isset($_POST['submit']))
{
$name=$_POST['name'];
$comments=$_POST['comments'];
if(trim($name)=="")
{
$msg=$msg."Name is missing.<br>";
}
if(trim($comments)=="")
{
$msg=$msg."No comment provided.<br>";
}
if(!get_magic_quotes_gpc())
{
$comments = addslashes($comments);
}
if(trim($msg) =="")
{
$check_record=mysql_query("SELECT name, comments FROM feed WHERE name='".$name."' AND comments='".$comments."'",$dbh);
$num_rows = mysql_num_rows($check_record);
if($num_rows !=0)
{
$msg=$msg."<font color=red size=2>* <strong>Name and the comment entered already exists. Record was not added due to duplication.</strong><br>";
}
else
{
$query="INSERT INTO feed (name, comments) VALUES ('$name', '$comments')";
$result = mysql_query($query);
$save=$save."<font color=blue size=4><strong>* Record was sucessfully entered* <br><a href='view.php'>View Feeds</a></font></strong><br>";
mysql_close($dbh);
}
}
}
if(trim($msg)!=""){
print "$msg";}
if(trim($save)!=""){
print "$save";}
?>
|
The third field (user_no) of our table is auto generated and
sequentially will accommulate it self on each addition of new record.
Test the form above to see the result.
After the data is added to the database, the next thing you may want do is view to see what is added.
This code bellow displays records from the feed table..
| View.php |
<?php
include("connect.inc");
$query="SELECT * FROM feed";
$result=mysql_query($query);
$records=mysql_numrows($result);
echo "<b>Database Records Output</b><br><br>";
$i=0;
while ($i < $records)
{
$name=mysql_result($result,$i,"name");
$comments=stripslashes(mysql_result($result,$i,"comments"));
$id=mysql_result($result,$i,"user_id");
echo "<hr size='3' color='red' width='65%' align='left'>";
echo "<b>User No:</b> $id<br><b>Name:</b> $name<br><b>Comment:</b>$comments<br>";
$i++;
}
?>
|
Form Process
Update Database
|