add.php

<form action="" method="post" name="form1"><br>
Input address book data:<br>
<input name="name" type="text" value="shinder"><br>
<input name="phone" type="text" value="0918981520"><br>
<input name="address" type="text" value="Taipei"><br>
<input name="submit" type="submit" value="Add"><br>
</form>
<?php

//建立資料連線物件
$pdo = new PDO("sqlite:mydb.sqlite");

$pdo->exec("CREATE TABLE address_book (
sid INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,
name VARCHAR,
phone VARCHAR,
address VARCHAR,
creation_date DATETIME DEFAULT CURRENT_TIMESTAMP)");

if( isset($_POST['submit']) && $_POST['submit']=='Add' ) {
    // 準備新增資料的語法
    $name = $_POST["name"];
    $phone = $_POST["phone"];
    $address = $_POST["address"];
    $pdo->exec("INSERT INTO 'address_book' ('name', 'phone', 'address' )
    VALUES( '$name', '$phone', '$address')");
}

if( $_GET['op']=='del' ) {
// 準備刪除資料的語法
$pdo->exec("DELETE FROM 'address_book' WHERE
SID=$_GET[sid]");
}

if( $_GET['op']=='sort_sid' ) {
	$pdoStatement = $pdo->query("SELECT * FROM address_book order by sid desc");
}
else if( $_GET['op']=='sort_name' ) {
	$pdoStatement = $pdo->query("SELECT * FROM address_book order by name desc");
}
else {
	$pdoStatement = $pdo->query("SELECT * FROM address_book");
}

makeTable($pdoStatement);

function makeTable($temp)
{
	echo "<table border='1'>";
	//設定Title
	echo "<tr><td><a href=add.php?op=sort_sid>sid</a></td>";
	echo "<td><a href=add.php?op=sort_name>name</a></td>";
	echo "<td>phone</td>";
	echo "<td>address</td>";
	echo "<td></td>";
	echo "<td></td></tr>";
	//讀取內容
	while($row = $temp->fetch() ) {
	echo "<tr><td>$row[sid]</td>";
	echo "<td>$row[name]</td>";
	echo "<td>$row[phone]</td>";
	echo "<td>$row[address]</td>";
	echo "<td><a href=add.php?op=del&sid=$row[sid]>del</a></td>";
	echo "<td><a href=update.php?op=update&sid=$row[sid]&name=$row[name]&phone=$row[phone]&address=$row[address]>update</a></td></tr>";
	}
	echo "</table>";
}

?>

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *