Pemrograman Web
13. PHP: Insert, View Data.
M. Udin Harun Al Rasyid, S.Kom, Ph.D
http://lecturer.eepis-its.edu/~udinharun
udinharun@eepis-its.edu
Table of Contents
PHP MySQL insert data
PHP MySQL view data
PHP MySQL edit data
PHP MySQL delete data
PHP MySQL: Inserting Data
To insert data into database, use the sintaks:
INSERT INTO nama_table (field1,field2,...)
VALUES ('data1','data2',...)
If the field parameter do not declare, the
amount of the inserting data must have the
same as the amount of the table field.
Example
Result
View From Phpmyadmin
PHP MySQL: Creating Form Insert Data
In order to make this input data is 'user friendly', you can make a
HTML form for input data
Result
The HTML form will send two variable, $name and
$address variable, into input.php file as describe in the
ACTION parameter of FORM HTML.
After you have already made input.php, fill
the input data and then click the sent button
such as:
Result
View the result from phpmyadmin
PHP MySQL: Display Data
In looking for one or more data in the database,
you can use syntax such as:
SELECT field1,field2,... FROM name_table
WHERE condition1,condition2,...
ORDER BY name_field
If field displayed is all field from the table, so all
of the name field itself does not have to be
declared but it is enough to change with the sign
* then all field will be accessed.
ORDER BY parameter shows the data that is
organized based on which field you choose.
The default sequence is from the smallest
one (number sequence), from A-Z (letter
sequence), and from the first data to the last
data (time sequence).
You can reverse these sequence by adding
DESC attribute.
Example
Result
Then, you can use DESC like
$order = "SELECT * FROM data_employees
ORDER BY name DESC"
so the result will be:
PHP MySQL: mysql_fetch_row() Function
Mysql_fetch_row() function takes the data from $result variable in
per line.
The first take is the top-ranking data line. The data that it takes is in
the array shape where the element from array is the field of data
table.
For example, in the program of data_employees, the syntax of $row
= mysql_fetch_row($result) will produce:
and so on until the while order get the false value then the restarting
will be stopped.
PHP MySQL: mysql_fetch_array() Function
Beside using mysql_fetch_row() function in order to get the query
result into database, you can also use mysql_fetch_array() function.
It has the same function as mysql_fetch_row() function where the
data reads line per line.
The difference of both function is that the result of
mysql_fetch_array() function is in the array assosiatif shape.
For example, if you use
mysql_fetch_array() in the program of
data_employees such as $row =
mysql_fetch_array($result) will produce:
PHP MySQL: Editing data
In editing the data, you can use the syntax as
follow:
For example, we will try to edit one of the
data from data_employees table.
file: db.inc.php for connection database
file: edit.php for connection database
Result: edit.php
The picture above is edit.php file where this file will show
overall data in the table, then there is edit menu in the
last column.
If you click the edit menu, it will bring the program to
execute edit_form.php file.
Edit_form.php file will show a form to edit the data which
have been selected in the previous form.
The mechanism is that the user choose one of the data
that will be edited in the first form (edit.php file) by
clicking the edit menu in the right column.
Edit_form.php:
Result edit_form.php
By clicking the edit button, the program goes to the fourth program,
edit_data.php file, which brings three variable such as $id variable
which contains of employees number data, $name variable which
contains of employees name data, and $address variable which
contains of employees address.
In order to know whether the data is already change or not, the
program is re-instructed to edit.php file with the order of header
("location:edit.php").
Here is the edit_data.php program file:
PHP MySQL: Deleting data
In deleting the data, use the sintaks as follow:
DELETE FROM name_table WHERE condition1,condition2,...
As for example, we will add menu to delete the data in table edit
data such as:
Delete menu will bring the program into
delete.php file where it is used to delete the
data which have been selected in the above
form.
Delete.php file as follow:
Finish