2020年5月7日

    1. <?php
    2. $link =mysqli_connect('127.0.0.1','root','root','class06')or exit('数据库连接失败');
    3. mysqli_set_charset($link,'utf-8');
    4. //查询语句
    5. $sql="SELECT * FROM books ORDER BY book_id DESC";
    6. $res =mysqli_query($link,$sql);
    7. var_dump($res);
    8. $row=mysqli_fetch_row($res);
    9. $row=mysqli_fetch_assoc($res);
    10. $row=mysqli_fetch_object($res);
    11. var_dump($row);
    12. $data = [];
    13. while($row=mysqli_fetch_assoc($res)){
    14. $data[] =$row;
    15. }
    16. var_dump($data);
    17. mysqli_close($link);
    18. ?>
    19. <!DOCTYPE html>
    20. <html>
    21. <head>
    22. <meta charset="UTF-8">
    23. <title>sql留言板</title>
    24. <style>
    25. .cb{
    26. border: 1px solid blue;
    27. }
    28. </style>
    29. </head>
    30. <body>
    31. <h1>留言板</h1>
    32. <p><a herf="./add.php">添加留言</a></p>
    33. <div class="cb"><p>内容</p>
    34. <div class="ca">
    35. <b>姓名</b><sapn>邮箱</span>
    36. </div>
    37. <div class="c-act">
    38. <a herf="">编辑</a>
    39. <a herf="">删除</a>
    40. </div>
    41. </div>
    42. </body>
    43. </html>
    44. <!DOCTYPE html>
    45. <html>
    46. <head>
    47. <meta charset="UTF-8">
    48. <title>添加留言</title>
    49. </head>
    50. <body>
    51. <h1>添加留言</h1>
    52. <form action="./demo.php" method="post">
    53. <p>昵称:<input type="text" name="name"></p>
    54. <p>邮箱:<input type="text" name="email"></p>
    55. <p>留言:<input type="text" name="comment"></p>
    56. <input type="submit" value="添加">
    57. </form>
    58. </body>
    59. </html>
    60. <?php
    61. $post =$_POST;
    62. var_dump($post);
    63. $link =mysqli_connect('127.0.0.1','root','root','liuyan')or exit('连接数据库失败');
    64. mysqli_set_charset($link,'UTF-8');
    65. $sql=sprintf("INSERT INTO COMMENT VALUES(DEFAULT, '%S','%S','%S')", $post['comment'],$post['name'],$post['email']);
    66. $res=mysqli_query($link,$sql);
    67. if($res && mysqli_afffected_rows($link)>0){
    68. echo"成功"
    69. }else{
    70. echo"失败";
    71. }
    72. mysqli_close($link);
    73. CREATE TABLE comment(
    74. id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
    75. content varchar(255) NOT NULL COMMENT '留言内容',
    76. name varchar(10) NOT NULL COMMENT '姓名',
    77. email varchar(60) NOT NULL COMMENT '邮箱'
    78. );
    79. //首页显示功能
    80. <?php
    81. $link = mysqli_connect('127.0.0.1','root','root','liuyan')or exit('连接数据库失败');
    82. mysqli_set_charset($link,'utf8');
    83. $sql= "SELECT * FROM comment";
    84. $res=mysqli_query($link,$sql);
    85. if($res false){
    86. echo mysqli_errno($link),"<br>";
    87. echo mysqli_error($link),"<br>";
    88. exit('sql语句执行失败!');
    89. }
    90. $data =[];
    91. while($row = mysqli_fetch_assoc($res)){
    92. $data[]=$row;
    93. }
    94. mysqli_close($link);
    95. ?>
    96. <!DOCTYPE html>
    97. <html>
    98. <head>
    99. <meta charset="UTF-8">
    100. <title>内容</title>
    101. <style></style>
    102. </head>
    103. <body>
    104. <h1>留言板</h1>
    105. <p><a herf="./add.php">添加留言</a></p>
    106. <?php foreach($data as $key $liuyan){ ?>
    107. <div class="cb">
    108. <p><?php echo $liuyan['content'] ?></p>
    109. <div class="ca"><b><?php echo $liuyan['name'] ?></b><span><?php echo $liuyan['email'] ?></span></div>
    110. </div>
    111. <div class="c-act">
    112. <a herf="">编辑</a>
    113. <a herf="">删除</a>
    114. </div>
    115. </div>
    116. <?php }?>
    117. </body>
    118. </html>
    119. <?php
    120. $link =mysqli_connect('127.0.0.1','root','root','class06')or exit('数据库连接失败');
    121. mysqli_set_charset($link,'utf-8');
    122. //查询语句
    123. $sql="SELECT * FROM books ORDER BY book_id DESC";
    124. $res =mysqli_query($link,$sql);
    125. var_dump($res);
    126. $row=mysqli_fetch_row($res);
    127. $row=mysqli_fetch_assoc($res);
    128. $row=mysqli_fetch_object($res);
    129. var_dump($row);
    130. $data = [];
    131. while($row=mysqli_fetch_assoc($res)){
    132. $data[] =$row;
    133. }
    134. var_dump($data);
    135. mysqli_close($link);
    136. ?>
    137. <!DOCTYPE html>
    138. <html>
    139. <head>
    140. <meta charset="UTF-8">
    141. <title>sql留言板</title>
    142. <style>
    143. .cb{
    144. border: 1px solid blue;
    145. }
    146. </style>
    147. </head>
    148. <body>
    149. <h1>留言板</h1>
    150. <p><a herf="./add.php">添加留言</a></p>
    151. <div class="cb"><p>内容</p>
    152. <div class="ca">
    153. <b>姓名</b><sapn>邮箱</span>
    154. </div>
    155. <div class="c-act">
    156. <a herf="">编辑</a>
    157. <a herf="">删除</a>
    158. </div>
    159. </div>
    160. </body>
    161. </html>
    162. <!DOCTYPE html>
    163. <html>
    164. <head>
    165. <meta charset="UTF-8">
    166. <title>添加留言</title>
    167. </head>
    168. <body>
    169. <h1>添加留言</h1>
    170. <form action="./demo.php" method="post">
    171. <p>昵称:<input type="text" name="name"></p>
    172. <p>邮箱:<input type="text" name="email"></p>
    173. <p>留言:<input type="text" name="comment"></p>
    174. <input type="submit" value="添加">
    175. </form>
    176. </body>
    177. </html>
    178. <?php
    179. $post =$_POST;
    180. var_dump($post);
    181. $link =mysqli_connect('127.0.0.1','root','root','liuyan')or exit('连接数据库失败');
    182. mysqli_set_charset($link,'UTF-8');
    183. $sql=sprintf("INSERT INTO COMMENT VALUES(DEFAULT, '%S','%S','%S')", $post['comment'],$post['name'],$post['email']);
    184. $res=mysqli_query($link,$sql);
    185. if($res && mysqli_afffected_rows($link)>0){
    186. echo"成功"
    187. }else{
    188. echo"失败";
    189. }
    190. mysqli_close($link);
    191. CREATE TABLE comment(
    192. id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
    193. content varchar(255) NOT NULL COMMENT '留言内容',
    194. name varchar(10) NOT NULL COMMENT '姓名',
    195. email varchar(60) NOT NULL COMMENT '邮箱'
    196. );
    197. //首页显示功能
    198. <?php
    199. $link = mysqli_connect('127.0.0.1','root','root','liuyan')or exit('连接数据库失败');
    200. mysqli_set_charset($link,'utf8');
    201. $sql= "SELECT * FROM comment";
    202. $res=mysqli_query($link,$sql);
    203. if($res false){
    204. echo mysqli_errno($link),"<br>";
    205. echo mysqli_error($link),"<br>";
    206. exit('sql语句执行失败!');
    207. }
    208. $data =[];
    209. while($row = mysqli_fetch_assoc($res)){
    210. $data[]=$row;
    211. }
    212. mysqli_close($link);
    213. ?>
    214. <!DOCTYPE html>
    215. <html>
    216. <head>
    217. <meta charset="UTF-8">
    218. <title>内容</title>
    219. <style></style>
    220. </head>
    221. <body>
    222. <h1>留言板</h1>
    223. <p><a herf="./add.php">添加留言</a></p>
    224. <?php foreach($data as $key $liuyan){ ?>
    225. <div class="cb">
    226. <p><?php echo $liuyan['content'] ?></p>
    227. <div class="ca"><b><?php echo $liuyan['name'] ?></b><span><?php echo $liuyan['email'] ?></span></div>
    228. </div>
    229. <div class="c-act">
    230. <a herf="">编辑</a>
    231. <a herf="">删除</a>
    232. </div>
    233. </div>
    234. <?php }?>
    235. </body>
    236. </html>
    237. <?php
    238. $link =mysqli_connect('127.0.0.1','root','root','class06')or exit('数据库连接失败');
    239. mysqli_set_charset($link,'utf-8');
    240. //查询语句
    241. $sql="SELECT * FROM books ORDER BY book_id DESC";
    242. $res =mysqli_query($link,$sql);
    243. var_dump($res);
    244. $row=mysqli_fetch_row($res);
    245. $row=mysqli_fetch_assoc($res);
    246. $row=mysqli_fetch_object($res);
    247. var_dump($row);
    248. $data = [];
    249. while($row=mysqli_fetch_assoc($res)){
    250. $data[] =$row;
    251. }
    252. var_dump($data);
    253. mysqli_close($link);
    254. ?>
    255. <!DOCTYPE html>
    256. <html>
    257. <head>
    258. <meta charset="UTF-8">
    259. <title>sql留言板</title>
    260. <style>
    261. .cb{
    262. border: 1px solid blue;
    263. }
    264. </style>
    265. </head>
    266. <body>
    267. <h1>留言板</h1>
    268. <p><a herf="./add.php">添加留言</a></p>
    269. <div class="cb"><p>内容</p>
    270. <div class="ca">
    271. <b>姓名</b><sapn>邮箱</span>
    272. </div>
    273. <div class="c-act">
    274. <a herf="">编辑</a>
    275. <a herf="">删除</a>
    276. </div>
    277. </div>
    278. </body>
    279. </html>
    280. <!DOCTYPE html>
    281. <html>
    282. <head>
    283. <meta charset="UTF-8">
    284. <title>添加留言</title>
    285. </head>
    286. <body>
    287. <h1>添加留言</h1>
    288. <form action="./demo.php" method="post">
    289. <p>昵称:<input type="text" name="name"></p>
    290. <p>邮箱:<input type="text" name="email"></p>
    291. <p>留言:<input type="text" name="comment"></p>
    292. <input type="submit" value="添加">
    293. </form>
    294. </body>
    295. </html>
    296. <?php
    297. $post =$_POST;
    298. var_dump($post);
    299. $link =mysqli_connect('127.0.0.1','root','root','liuyan')or exit('连接数据库失败');
    300. mysqli_set_charset($link,'UTF-8');
    301. $sql=sprintf("INSERT INTO COMMENT VALUES(DEFAULT, '%S','%S','%S')", $post['comment'],$post['name'],$post['email']);
    302. $res=mysqli_query($link,$sql);
    303. if($res && mysqli_afffected_rows($link)>0){
    304. echo"成功"
    305. }else{
    306. echo"失败";
    307. }
    308. mysqli_close($link);
    309. CREATE TABLE comment(
    310. id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
    311. content varchar(255) NOT NULL COMMENT '留言内容',
    312. name varchar(10) NOT NULL COMMENT '姓名',
    313. email varchar(60) NOT NULL COMMENT '邮箱'
    314. );
    315. //首页显示功能
    316. <?php
    317. $link = mysqli_connect('127.0.0.1','root','root','liuyan')or exit('连接数据库失败');
    318. mysqli_set_charset($link,'utf8');
    319. $sql= "SELECT * FROM comment";
    320. $res=mysqli_query($link,$sql);
    321. if($res false){
    322. echo mysqli_errno($link),"<br>";
    323. echo mysqli_error($link),"<br>";
    324. exit('sql语句执行失败!');
    325. }
    326. $data =[];
    327. while($row = mysqli_fetch_assoc($res)){
    328. $data[]=$row;
    329. }
    330. mysqli_close($link);
    331. ?>
    332. <!DOCTYPE html>
    333. <html>
    334. <head>
    335. <meta charset="UTF-8">
    336. <title>内容</title>
    337. <style></style>
    338. </head>
    339. <body>
    340. <h1>留言板</h1>
    341. <p><a herf="./add.php">添加留言</a></p>
    342. <?php foreach($data as $key $liuyan){ ?>
    343. <div class="cb">
    344. <p><?php echo $liuyan['content'] ?></p>
    345. <div class="ca"><b><?php echo $liuyan['name'] ?></b><span><?php echo $liuyan['email'] ?></span></div>
    346. </div>
    347. <div class="c-act">
    348. <a herf="">编辑</a>
    349. <a herf="">删除</a>
    350. </div>
    351. </div>
    352. <?php }?>
    353. </body>
    354. </html>

    php增删改查表
    1.连接数据库选择数据库 并且判断数据库是否连接成功
    2.设置字符集(编码)为了中文显示不乱码
    3.准备sql语句
    4.向mysql服务器发送sql语句执行
    5.释放结果同时关闭数据库连接

    查询的处理方式和新增 删除 修改的方式不同
    查询的处理:
    获取一个结果,这个函数的功能类似readdir();
    相信为结果集合内有一个指针,每调用函数一次则将当前指针指向的内容拿出来
    并且将指针指向下一个值的位置;同时如果获取不到值则返回false(NULL)
    mysqli_fetch_array(结果集[,返回格式]);
    返回格式:MYSQLI_BOTH(默认)/MYSQLI_NUM / MYSQLI_ASSOC

    MYSQLI_BOTH 索引+关联
    MYSQLI_NUM 索引
    MYSQLI_ASSOC 关联

    mysqli_fetch_row(结果集) 获取数据为索引数组
    mysqli_fetch_assoc(结果集) 获取数据为关联数组
    mysqli_fetch_object(结果集) 获取数据为对象格式

    $row=mysqli_fetch_row($res);
    $row=mysqli_fetch_assoc($res);
    $row=mysqli_fetch_object($res);
    var_dump($row);

    获取错误信息:
    mysql_errno($link); 获取错误代码(编号)
    mysql_error($link);获取错误文字信息

    mysql版留言板添加功能