Pages

Tuesday, November 13, 2012

Add data to MySQL using PHP


This is just a simple example adding data to MySql using PHP. Hope this helps. :)


In the add.php file, the form named "add" will carry the data to the add_info.php


Inside the add.php
<table>
<form name="add" method="post" action="add_info.php">
<tr>
<td>First: <input type="text" name="first" /></td>
</tr>
<tr>
<td>Second: <input type="text" name="second" /></td>
</tr>
<tr>
<td><input type="submit" name="Submit" /></td>
</tr>
</form>
</table>

the add_info.php

<?php

#establish the connection to your database
mysql_connect("localhost", "root", "") or die(mysql_error());

#name of your database
mysql_select_db("sample_db") or die(mysql_error());

#assign the "$_POST" values, the "$_POST" values are the names of field in the add.php
$first_inp = $_POST["first"];
$second_inp = $_POST["second"];

#values will be inserted to the sample_table in the sample_db database
mysql_query("insert into sample_table (first_data, second_data) values ('$first_inp','$second_inp')");

#use echo to display the added values
echo "Values ".$first_inp." and ".$second_inp." are added to the database.";

?>

Now check the database to see if the data is added.

Here's the download link for complete example along with the database. Just manipulate the codes and explore.

No comments:

Post a Comment