0% found this document useful (0 votes)
20 views4 pages

Edit 4

Uploaded by

eason.yih
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

Edit 4

Uploaded by

eason.yih
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Air Life - �‫��ڪ‬q�����O

�����V�O�� - ���L�N�O�A���F�I

Hello World
2014�~4��17�� �P��|
[PHP�о�] �ϥ�PDO�s�@²��PHP�d���O

2014/04/17 ²��PHP�d���O�s�@�о�

�̫
��s�ɶ��G2016/3/17

�e���G��²��d���O�ϥ�PDO�s������s�@�A�u�O�Ω�²�檺
�оǥγ~�A���PHP�D�`��x���M�~�H�h�i�H�L��o�g�оǤ�XDDD

��Ʈw�ϥΡGMySQL

�оǶ}�l�I

�Ĥ@�B�G�‫�إ‬Ʈw�B��ƪ�]���M�A��Ʃ��̡H�^

����i�JphpMyAdmin��

1. �����e�s�W�f�‫߷إ‬s��Ʈw

��Ʈw�W�١Gdemo

2. �����e�s�W�f�‫߷إ‬s��ƪ�

��ƪ�W�١Gguestbook
��ƪ����G4
���Ӹ`�G
id - INT - A_I - //�Ƶ� �s��
name - TEXT //�Ƶ� �W��
content - TEXT //���e
updtime - TIMESTAMP - CURRENT_TIMESTAMP - on update CURRENT_TIMESTAMP //�Ƶ� �d���ɶ�

�ΰ��SQL�y�k�‫�ت‬G
CREATE TABLE `guestbook` (
`id` int(11) NOT NULL,
`name` text NOT NULL,
`content` text NOT NULL,
`updtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE
CURRENT_TIMESTAMP
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=latin1;
view rawguestbook.sql hosted with ? by GitHub
��o��MySQL��B�‫ظ‬m���a��w�g�����I
��phpMyAdmin����Ǫ̨ӻ��A�u����K�‫ܦ‬h�I

�ĤG�B�G�‫߯إ‬d������PHP�{���X

�o�‫��ܤ‬h���A������WPHP���{���X�G
<?php
/* guestbook by Kevin Chang */
// �ϥα`�Ʃw�q��Ʈw����s����
define('DB_PATH', 'localhost'); // �]�w��Ʈw��m
define('DB_USER', 'admin'); // �]�w��Ʈw�b��
define('DB_PASSWORD', '*****'); // �]�w��Ʈw�K�X
define('DB_NAME', 'demo'); // �]�w��Ʈw�W��
define('DB_TABLE', 'guestbook'); // �]�w��ƪ�W��
date_default_timezone_set('Asia/Taipei'); // �]�w�ɰ�
$link = new PDO(
'mysql:host='.DB_PATH.';charset=UTF8;dbname='.DB_NAME,
DB_USER,
DB_PASSWORD
); // �‫إ‬ϥ�PDO�覡
�s�u������A�é�J�]�w������ƾ�
$query = $link->prepare('SELECT * FROM `'.DB_TABLE.'`'); // �ϥ�prepare�禡, �
‫߬إ‬d�߸�ƪ�SQL�y�k
$query->execute(); // ���sql�y�k -
execute();
$result = $query->fetchAll(PDO::FETCH_OBJ); //
�d�߫�߫N��������G�̧
Ǧs���}�C�æ^��
$data = array(
':name' => isset($_POST['name']) ? $_POST['name'] : null,
':content' => isset($_POST['content']) ? $_POST['content'] : null
); // �ŧiPOST�����e
switch($_GET['act']) {
case '': // $_GET['act'] = null,
�N���@��
break;
case 'ins': // $_GET['act'] = ins,
���s�W
ins($data, $link);
header('Location:guestbook.php'); // �s�W����A��^�D��
break;
default:
header('Location:guestbook.php'); // $_GET['act'] = ��L,
�N��^�D��
}
function ins($data, $link) {
$query = $link->prepare('
INSERT INTO `guestbook` (`name`, `content`)
VALUES (:name, :content)
;'
);
$query->execute($data); // ��� $data
}
?>
view rawguestbook.part1.php hosted with ? by GitHub

�j������a������Ƶ�F�A�p�G����~���a���‫ٷ‬нЫ�@�UXD

ۤ v���I
�‫ݭ‬n�`�N���a��j���N�O�e����MySQL�n�J�b���K�X������O�o�令�ۤ

��o��PHP�{���X���a��N�j�P�����աI

�ĤT�B�G�‫߯إ‬d������HTML�{���X(��M�̭
��٬O���Ψ�PHP)

�o��OHTML5���{���X�G
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>guestbook</title>
</head>
<body>
<div class="container">
<div>
<h1>�d���O</h1>
<hr>
<?php foreach($result as $i => $v): ?>
<p>
id: <?php echo $v->id; ?> |
name: <?php echo $v->name; ?> |
content: <?php echo $v->content; ?> |
update time: <?php echo $v->updtime; ?> |
<a href="#">�s��</a> |
<a href="#">�R��</a>
</p>
<hr>
<?php endforeach; ?>
</div>

<form method="post" action="?act=ins">


<div>
<label for="name">name:</label>
<input type="text" name="name"/>
</div>
<div>
<label for="content">content:</label>
<textarea name="content"></textarea>
</div>
<input type="submit" name="submit"/>
</form>
</div>
</body>
</html>
view rawguestbook.part2.php hosted with ? by GitHub
���ˤ�
�ƻs�K�W��@������A��ӴN�వ ²��s�W�աI

���N������o��

�����D�w��d�����‫ݡ‬I

�i�K�̡G Unknown �� �U��2:32


�H�q�l�l��ǰe�o�g�峹

BlogThis�I

��ɦ� Twitter

��ɦ� Facebook

��ɨ� Pinterest

3 �h�d�� :

�ΦW2018�~1��25�� �U��7:05
���j�‫^^ �ܫ‬

�^��
�^��

Kevin Chang2018�~1��26�� �W��3:42


���±z����� ^ ^
�^��

�ΦW2018�~3��26�� �W��1:55
���j�D�`�ԲӪ�����!

�^��

���ѰΦW�d���A�w��d���߰�߰ �^ ^

�o�g����s��
�‫߳إ‬s��

��s������ ª������
�q�\�G �i�K�d�� ( Atom )
��x�s��
? 2017 ( 2 )
? 2015 ( 3 )
�� 2014 ( 1 )
�� �|�� ( 1 )
[PHP�о�] �ϥ�PDO�s�@²��PHP�d���O
? 2013 ( 6 )
�`�s��q
29568
�D�D�Ϥ�ӷ��Gluoman. �޳N���ѡGBlogger.

You might also like