Hai yesterday from our mentor, Thyagu sir’s class i learned how to insert the values into MySQL database through PHP script.
Here iam sharing my experiences of how i did this process
Before proceeding with this task, we need to install LAMP stack on our machines.
To do it in one command, open the terminal and type
sudo tasksel install lamp-server
The first step is to write a html code to design a form
For example say the name as input.html
<html> <head> </head> <body> <form name="run" action="store.php" method="post"> Name : <input type="text" name="name" /> <br /> Address : <input type="text" name="address" /> <br /> <input type="submit" value="Clickme" /> </form> </body> </html>
Store this file into documentroot i.e., /var/www
The second step is to create a database in mysql and a table inside the database
To create a database in mysql we need to enter into mysql shell prompt.
To do it, enter the following command from the terminal
$ mysql -u root -p
You will be asked to enter a password
Give your mysql password which you gave during mysql installation
You will get a mysql shell prompt which looks as follows
Now create a database using mysql commands
Here i attached a screenshot to describe the steps done in mysql db
The last step is to write a PHP script to receive the values entered in the HTML file
Store the below script in a file matching the name of attribute ‘action’ in html code ( here store.php )
Place this file inside document root i.e., /var/www
<?php $conn = mysql_connect('localhost','root','yourmysqlpassword') or die('Iam dying'); mysql_select_db('slash',$conn); $name = $_POST['name']; $address = $_POST['address']; $string = "insert into slashprog(name,address) values('".$name."','".$address."')"; mysql_query($string,$conn); mysql_close(); ?>
Now run the Apache Web Server
/etc/init.d/apache2 start on Ubuntu 10.04
Open a browser and type http://localhost/input.html.
You can see the form looks like below
Now go to mysql shell and confirm whether the values are inserted or not.
That’s all the values get inserted.
Our next step is to convert the same concept into Ruby on Rails