標簽:com http class blog style div code ext string log color
1
2
3
4
5
6
7
8
9
10
11 |
<?php $result
= mysql_query( $sql ); while ( $row
= mysql_fetch_array( $result )) { ?> 要寫的內容代碼,比如說Html <?php } ?> |
可以在html中添加数据提交到數據庫中,前提要和數據庫中名字信息一致
表單提交:
1 |
<form action= "showDaYin.php"
method= "post" ><br> .......<br> <input type= "submit"
name= "submit"
value= "添加" ><br></form> |
这是一部分提交表单中的数据的代码,其中包含有链接到數據庫的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 |
<html> <head> <title>添加信息</title> <meta http-equiv= "Content-Type"
content= "text/html;charset=UTF-8" /> </head> <body> <?php if (@ $_POST [ ‘submit‘ ]){ $id
= $_POST [ ‘id‘ ]; $name
= $_POST [ ‘name‘ ]; $address
= $_POST [ ‘address‘ ]; $age
= $_POST [ ‘age‘ ]; $sex
= $_POST [ ‘sex‘ ]; $hobby
= $_POST [ ‘hobby‘ ]; $fav
= "" ; foreach ( $hobby
as $v ){ $fav .= $v . "," ; } if ( $sex ){ $sex
= "男" ; } else { $sex
= "女" ; } echo
"學號:" . $id . "<br/>" ; echo
"姓名:" . $name . "<br/>" ; echo
"地址:" . $address . "<br/>" ; echo
"年齡:" . $age . "<br/>" ; echo
"性別:" . $sex . "<br/>" ; echo
"愛好:" . $fav . "<br/>" ; $conn
= mysql_connect( "localhost" , "root" , "" ) or
die ( "數據庫连接失败!!" ); <span style= "color: rgb(204, 153, 255);" > //找到mysql服務器</span> mysql_select_db( "test" ); <span style= "color: rgb(204, 153, 255);" > //选择链接到的數據庫</span> mysql_query( "set names ‘utf8‘" ); <span style= "color: rgb(204, 153, 255);" > //設置編碼</span> $sql
= "INSERT INTO `test`.`students`(`id`, `names`, `address`, `age`, `sex`, `hobby`)VALUES (null,‘$name‘,‘$address‘,$age,‘$sex‘,‘$fav‘)" ; mysql_query( $sql ) or
die (mysql_error()); <span style= "color: rgb(204, 153, 255);" > //执行數據庫操作</span> mysql_close( $conn ); } ?> </body> </html> |
只能讀出返回運行一次的值:
1
2 |
$result
= mysql_query( $sql ); $row = mysql_fetch_array( $result ) |
要顯示庫中所有數據需要加上循環
1
2
3
4
5
6
7
8
9 |
<?php $result
= mysql_query( $sql ); while ( $row
= mysql_fetch_array( $result )){ ?> //內容代碼 <?php } ?> |
ieset 得到的是布尔类型的值(1 or 0)
php連接到數據庫操作,布布扣,bubuko.com
標簽:com http class blog style div code ext string log color
原文地址:http://www.cnblogs.com/5huihui/p/3698295.html