insert php

9
12/01/14 An example to insert some data in to the MySQL database using PHP people.cis.ksu.edu/~hankley/d764/tut06/GopisettyPHP.html 1/9 An example to insert some data in to the MySQL database using PHP 1. Create a Index.php page in a new folder(“764”) created under public_html folder present in your home directory To use a PHP script on your web page, you just need to end the file name with .php and make sure the permissions on the file are set correctly. Any files to be accessed by the web server must be publically readable, but none of your PHP files (nor the directory containing them) may be group or publically writable. Following are the commands you can use for setting the permissions in linux: chmod 755 public_html chmod 644 public_html/764/index.php The PHP page will have two simple text boxes for the user to enter some data in to it. Label them to be Firstname and Lastname. INDEX.PHP <html> <body> <h1>A small example page to insert some data in to the MySQL database using PHP</h1> <form action="insert.php" method="post"> Firstname: <input type="text" name="fname" /><br><br> Lastname: <input type="text" name="lname" /><br><br> <input type="submit" /> </form> </body> </html> We also need to make sure that the form method attribute is “post” so as to access the data being entered in a reliable way in the next page being directed “insert.php” so that the data being entered in the textboxes can then be saved to the database in the “insert.php” page. none * 0 ? <> {}

Upload: migl-vallee

Post on 23-Oct-2015

33 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Insert Php

12/01/14 An example to insert some data in to the MySQL database using PHP

people.cis.ksu.edu/~hankley/d764/tut06/GopisettyPHP.html 1/9

An example to insert some data in to the MySQL database using PHP

1. Create a Index.php page in a new folder(“764”) created under public_html folder present in your

home directory

To use a PHP script on your web page, you just need to end the file name with .php and make sure the

permissions on the file are set correctly. Any files to be accessed by the web server must be publically

readable, but none of your PHP files (nor the directory containing them) may be group or publically

writable. Following are the commands you can use for setting the permissions in linux:

chmod 755 public_html

chmod 644 public_html/764/index.php

The PHP page will have two simple text boxes for the user to enter some data in to it. Label them to be

Firstname and Lastname.

INDEX.PHP

<html>

<body>

<h1>A small example page to insert some data in to the MySQL database using

PHP</h1>

<form action="insert.php" method="post">Firstname: <input type="text" name="fname" /><br><br>Lastname: <input type="text" name="lname" /><br><br> <input type="submit" /></form></body>

</html>

We also need to make sure that the form method attribute is “post” so as to access the data being entered in a reliable way

in the next page being directed “insert.php” so that the data being entered in the textboxes can then be saved to the

database in the “insert.php” page.

none * 0 ? <>

Page 2: Insert Php

12/01/14 An example to insert some data in to the MySQL database using PHP

people.cis.ksu.edu/~hankley/d764/tut06/GopisettyPHP.html 2/9

2. To connect to MySQL :-­

Before you can access your MySQL database, you must contact the system administrators to request an

account.

Once the administrators have notified you that your account has been created, you may connect using

the following instructions.

Go to http://mysql.cis.ksu.edu/phpmyadmin and type your MySQL ID and password being given.

Page 3: Insert Php

12/01/14 An example to insert some data in to the MySQL database using PHP

people.cis.ksu.edu/~hankley/d764/tut06/GopisettyPHP.html 3/9

3. Now enter the new table name “nametable” , number of fields in that table as “2” and hit GO button.

Page 4: Insert Php

12/01/14 An example to insert some data in to the MySQL database using PHP

people.cis.ksu.edu/~hankley/d764/tut06/GopisettyPHP.html 4/9

4. Enter the field names to be “firstname” and “lastname” and keep the length attributes to be “20” forboth the fields. The default type of VARCHAR is kept as it is.

5. After the table fields are being created, the following screen will be shown to you.

Page 5: Insert Php

12/01/14 An example to insert some data in to the MySQL database using PHP

people.cis.ksu.edu/~hankley/d764/tut06/GopisettyPHP.html 5/9

6. Now we need to make a connection to the MySQL database and then send this entered data from our

textboxes. For that we create a new PHP page “insert.php” and use the following connection strings to

the connection variable $con

After making a connection, a SQL query is being written to enter this data in to the MySQL database

being created (“nametable”)

To tell the user that the data is being entered we set the echo to "1 record added"

INSERT.PHP

<html>

<body>

<?php

$con = mysql_connect("mysql.cis.ksu.edu","cis_id","password");;if (!$con)

die('Could not connect: ' . mysql_error());;

mysql_select_db("cis_id", $con);;

$sql="INSERT INTO nametable (fname, lname)

VALUES

('$_POST[fname]','$_POST[lname]')";;

Page 6: Insert Php

12/01/14 An example to insert some data in to the MySQL database using PHP

people.cis.ksu.edu/~hankley/d764/tut06/GopisettyPHP.html 6/9

if (!mysql_query($sql,$con)) die('Error: ' . mysql_error());; echo "1 record added";; mysql_close($con)?></body></html>

7. Now type the index page URL in your browser and enter some data in to the textboxes. Submit theQuery.

We will then be shown the confirmation page after the data is being entered.

Page 7: Insert Php

12/01/14 An example to insert some data in to the MySQL database using PHP

people.cis.ksu.edu/~hankley/d764/tut06/GopisettyPHP.html 7/9

8. For browsing the data, you need to click on Database: cis_id on top of the page(http://mysql.cis.ksu.edu/phpmyadmin) to get to the database tables page. Then click on the browsebutton as shown below, beside the “nametable” which you have already created to browse through theentered data.

Page 8: Insert Php

12/01/14 An example to insert some data in to the MySQL database using PHP

people.cis.ksu.edu/~hankley/d764/tut06/GopisettyPHP.html 8/9

9. The following screen then shows us the data being entered in to the MySQL database table beingcreated.

Page 9: Insert Php

12/01/14 An example to insert some data in to the MySQL database using PHP

people.cis.ksu.edu/~hankley/d764/tut06/GopisettyPHP.html 9/9

J