前後台互動(無ajax等架構)
add.html
<!DOCTYPE html><html><head lang="en"><meta charset="UTF-8"><title>相加</title></head><body><!--GET是從伺服器上擷取資料(地址欄顯示參數,不安全)。 --><!--POST是向伺服器傳送資料(地址欄不顯示參數,更安全)。--><form action="add.php" method="post"> a:<input type="text" name="a"><br> b:<input type="text" name="b"><br> <input type="submit" value="提交"></form></body></html>
add.php
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>相加</title></head><body><?phpif($_POST['a']&&$_POST['b']){ echo $_POST['a']+$_POST['b'];}else{ echo "提交錯誤";//含有中文故需html頭}?></body></html>
hello.html
<!DOCTYPE html><html><head lang="en"><meta charset="UTF-8"><title>歡迎</title></head><body><form action="hello.php" method="get"> <input name="name" type=""> <input type="submit" value="提交"></form></body></html>
hello.php
<!DOCTYPE html> <html><head><meta charset="UTF-8"><title>document</title></head><body><?phpif(isset($_GET['name'])&&$_GET['name']){ echo 'hello'.$_GET['name'];}else{ echo '請輸入名字';}?></body></html>
檔案上傳
upload.html
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title></head><body><!--enctype="multipart/form-data"可設定接受二進位檔案上傳 --><form action="upload.php" method="post" enctype="multipart/form-data"><input type="file" name="file"><input type="submit" value="提交"></form></body></html>
upload.php
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>上傳檔案</title></head><body><?php$file = $_FILES['file'];$filename = $file['name'];move_uploaded_file($file['tmp_name'], $filename);echo "<img src='$filename'>";?></body></html>
與資料庫互動
添加使用者
adduser.html
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>添加使用者</title></head><body> <form action="adduser.php" method="post"> <div>使用者名稱字:<input type="text" name="name"></div> <div>使用者年齡:<input type="text" name="age"></div> <input type="submit" value="提交"> </form></body></html>
adduser.php
<?php$name = $_POST['name'];$age = $_POST['age'];if(!isset($name)){ die('user name not define');}if(!isset($age)){ die('user age not define');}if(empty($name)){ die('user name is empty');}if(empty($age)){ die('user age is empty');}require_once 'functions.php';$conn = connectp1();$age = intval($age);//非字串必須強制轉化,防止錯誤資料放入資料庫mysql_query("INSERT INTO users(name,age) VALUES ('$name',$age)");//字串必須引起來,防止錯誤資料放入資料庫if(mysql_errno()){//如果出現錯誤 echo mysql_error();}else{ header("Location:p1.php");//介面跳轉到p1.php}
config.php
<?phpdefine('MYSQL_HOST', 'localhost');define('MYSQL_USER', 'root');define('MYSQL_PWD','');?>
functions.php
<?phprequire_once 'config.php';//與config.php檔案相關聯function connectp1(){ $conn = mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_PWD);//串連資料庫 if(!$conn){ die('can not connect db');//輸出錯誤資訊並運行中斷 } mysql_select_db('myapp');//選擇資料表 return $conn;}?>
修改使用者
edituser.php
<!DOCTYPE html><html><head> <meta charset=" UTF-8"> <title>編輯使用者</title></head><body><?php// if(isset($_GET['id'])&&!empty($_GET['id'])){//id在集合中或者id為空白// }require_once 'functions.php';if(!empty($_GET['id'])){ connectp1();//functions.php中串連到資料庫的函數 $id = intval($_GET['id']); $result = mysql_query("SELECT * FROM users WHERE id = $id");//應用sql語句 if(mysql_errno()){ die('can not connect db'); } $arr = mysql_fetch_assoc($result);// print_r($arr);}else{ die('id not define');}?><form action="edituser_server.php" method="post"> <div>使用者ID <input type="text" name="id" value="<?php echo $arr['id'];?>"> </div> <div>使用者名稱字 <input type="text" name="name" value="<?php echo $arr['name'];?>"> </div> <div>使用者年齡 <input type="text" name="age" value="<?php echo $arr['age'];?>"> </div> <input type="submit" value="提交修改"></form></body></html>
edituser_server.php
<?phprequire_once 'functions.php';if(empty($_POST['id'])){ die('id is empty');}if(empty($_POST['name'])){ die('name is empty');}if(empty($_POST['age'])){ die('age is empty');}$id = intval($_POST['id']);$name = $_POST['name'];$age = intval($_POST['age']);connectp1();mysql_query("UPDATE users SET name='$name',age=$age WHERE id=$id");//字串需加單引號if(mysql_errno()){ echo mysql_error();}else{ header("Location:p1.php");//跳轉頁面}
p1.php
<?php require_once 'functions.php';?><!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>使用者的添加</title></head><body><a href="adduser.html">添加使用者</a><table style='text-align:left' border='1'> <tr><th>id</th><th>名字</th><th>年齡</th><th>修改</th></tr> <?php $conn = connectp1();//引用檔案functions.php內的connectp1函數 $result = mysql_query("SELECT * FROM users ORDER BY id DESC",$conn);//從資料庫表users中倒序匯入所有資訊 $datacount = mysql_num_rows($result);//記錄表中的資料的條數// echo $datacount.'<br>';//輸出資料條數// for($i=0;$i<$datacount;$i++){//迴圈輸出每條資料// $result_arr = mysql_fetch_assoc($result);// print_r($result_arr);// echo '<br>';// } for($i=0;$i<$datacount;$i++){//迴圈輸出每條資料 $result_arr = mysql_fetch_assoc($result); $id = $result_arr['id'];//允許字元下標 $name = $result_arr['name']; $age = $result_arr['age']; echo "<tr><td>$id</td><td>$name</td><td>$age</td><td><a href='edituser.php?id=$id'>修改</a></td></tr>"; } ?></table></body></html>