update.php

<form action="" method="post" name="form1"><br>
Input address book data:<br>
<input name="name" type="text" value="
<?php
    if($_POST['submit']=='Update') {
		echo $_POST['name'];
	}
	else {
		echo $_GET['name'];
	}
?>
"><br>
<input name="phone" type="text" value="
<?php
    if($_POST['submit']=='Update') {
		echo $_POST['phone'];
	}
	else {
		echo $_GET['phone'];
	}
?>
"><br>
<input name="address" type="text" value="
<?php
    if($_POST['submit']=='Update') {
		echo $_POST['address'];
	}
	else {
		echo $_GET['address'];
	}
?>
"><br>
<input name="submit" type="submit" value="Update"><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']=='Update' ) {
    // 準備新增資料的語法
    $name = $_POST["name"];
    $phone = $_POST["phone"];
    $address = $_POST["address"];
	$pdo->exec("UPDATE 'address_book' SET name='$name', phone='$phone', address='$address' where sid=$_GET[sid]");
}

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


$pdoStatement = $pdo->query("SELECT * FROM address_book");

/*
while($row = $pdoStatement->fetch() ) {
    print_r( $row );
}
*/
makeTable($pdoStatement);

function makeTable($pdoStatement)
{
	echo $_POST["sql"]."<br>";
	echo "<table border='1'>";
	//設定Title
	echo "<tr><td>sid</td>";
	echo "<td>name</td>";
	echo "<td>phone</td>";
	echo "<td>address</td>";
	echo "<td></td></tr>";
	//讀取內容
	while($row = $pdoStatement->fetch(PDO::FETCH_ASSOC) ) {
	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></tr>";
	}
	echo "</table>";
}





?>

發表迴響

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