php+mysql 連接範例 add_mysql.php

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

mysql_connect('localhost','root','student') or die("connect error!");
mysql_select_db('phone') or die("db error");
mysql_query("SET NAMES 'utf8'");
mysql_query("CREATE TABLE  `address_book` (
 `sid` INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
 `name` VARCHAR( 20 ) NOT NULL ,
 `phone` VARCHAR( 20 ) NOT NULL ,
 `address` VARCHAR( 20 ) NOT NULL
) ENGINE = MYISAM ;");


if(count($_POST)>0) {
// 準備更新資料的語法
$name = $_POST["name"];
$phone = $_POST["phone"];
$address = $_POST["address"];
mysql_query("INSERT INTO address_book (name, phone, address )
		VALUES( '$name', '$phone', '$address')");
}

$result = mysql_query("SELECT * FROM address_book");
makeTable($result);

function makeTable($result)
{
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>";
echo "<td></td></tr>";
//讀取內容
while($row = mysql_fetch_assoc($result)) {
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_mysql.php?op=del&sid=$row[sid]>del</a></td>";
echo "<td><a href=update.php?sid=$row[sid]&name=$row[name]&phone=$row[phone]&address=$row[address]>update</a></td></tr>";
}
echo "</table>";
}
?>