Adding data to a Database
In this first of our three part guide to manipulating
data in a MySQL database we will add data to a MySQL
database . First create our sample database.

Now create our sample table like this.

Now we have our table , what we will do is create a
form which allows a user to add some text to the database
in this we will make it a link. Here is the code for
our form , this is pretty straightforward .
<form action="addingtoadb1.php" method
="POST">
<input type = "text" name = "add"
maxlength="50"><br>
<input type = "submit" value = "add
link"><br>
</form>
Now we move onto our php script , replace the ("hostname"
, "username" , "password") with
your own details .
<?php
//make our connection details
$connection = mysql_connect("hostname" , "username"
, "password")
or die ("Cannot make the connection");
//connect to sampledb with our connection details
$db = mysql_selectdb("sampledb" , $connection)
or die ("Cannot connect to the database");
//if the text field wasnt blank enter data
if($add)
{
//insert the text box data into the sample table
$sql_query = "INSERT into sample VALUES ('','$add')";
$result = mysql_query($sql_query);
//display the entered link
echo "You entered the following info " . $add;
}
//if it was blank display a message
else
{
echo ("you didnt enter any data");
}
?>
This will add the text / link to the database , in
our next part we will display the links in the database
|