Head First PHP&MySQL笔记,1

2014年6月10日

20:22

此处仅将HF PHP&MySQL上的主要代码做注释和笔记(虽然一步步来很精彩 不过对于笔记来说~)。

数据库连接与插入数据:

1 <?php

2 $first_name = $_POST['firstname'];

3 $last_name = $_POST['lastname'];

4 $when_it_happened = $_POST['whenithappened'];

5 $how_long = $_POST['howlong'];

6 $how_many = $_POST['howmany'];

7 $alien_description = $_POST['aliendescription'];

8 $what_they_did = $_POST['whattheydid'];

9 $fang_spotted = $_POST['fangspotted'];

10 $email = $_POST['email'];

11 $other = $_POST['other'];

12

13 $dbc = mysqli_connect('data.aliensabductedme.com', 'owen', 'aliensrool', 'aliendatabase')//此处用来连接数据库 格式:域名或者ip 用户名 密码 选用的数据库

14 or die('Error connecting to MySQL server.');//连接失败的反馈

15

16 $query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, " .

17 "how_many, alien_description, what_they_did, fang_spotted, other, email) " .

18 "VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " .

19 "'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";//MySQL的插入语句 对应的column插入对应的值

20

21 $result = mysqli_query($dbc, $query)//执行query语句

22 or die('Error querying database.');

23

24 mysqli_close($dbc);//要有访问完数据库就关闭的良好习惯 数据库的并发访问有限

25

26 echo 'Thanks for submitting the form.<br />';

27 echo 'You were abducted ' . $when_it_happened;

28 echo ' and were gone for ' . $how_long . '<br />';

29 echo 'Number of aliens: ' . $how_many . '<br />';

30 echo 'Describe them: ' . $alien_description . '<br />';

31 echo 'The aliens did this: ' . $what_they_did . '<br />';

32 echo 'Was Fang there? ' . $fang_spotted . '<br />';

33 echo 'Other comments: ' . $other . '<br />';

34 echo 'Your email address is ' . $email;

35 ?>