index.php新增Form2(藍色部分)和更新部分程式(紅色部分)

<form id="form1" name="form1">
<table border="1">
<tr>
<td>id</td>
<td>name</td>
<td>tel</td>
<td>address</td>
<td>修改</td>
<td>刪除</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_result['id']; ?></td>
<td><?php echo $row_result['name']; ?></td>
<td><?php echo $row_result['tel']; ?></td>
<td><?php echo $row_result['address']; ?></td>
<td><a href="index.php?id=<? echo $row_result[id]; ?>& name=<? echo $row_result[name]; ?>& tel=<? echo $row_result[tel]; ?>& address=<? echo $row_result[address]; ?>& edit=1">修改</a></td>
<td><a href="del.php?id=<? echo $row_result[id]; ?>">刪除</a></td>
</tr>
<?php } while ($row_result = mysql_fetch_assoc($result));?>
<tr>

</table>
</form>

<p></p>
<? if($_GET[edit] == 1){ //如果有接收到edit的值就是要更新功能?>

<form id="form2" name="form2" method="post" action="update.php">
<input name="id" type="hidden" value="<? echo $_GET[id] ; ?>" />
name<input name="name" type="text" id="name" value="<? echo $_GET[name] ; ?>" />
tel <input name="tel" type="text" id="tel" value="<? echo $_GET[tel] ; ?>" />
address<input name="address" type="text" id="address" value="<? echo $_GET[address] ; ?>" />
<input type="submit" name="button" id="button" value="更新" />
</form>

<? }else{ //沒有接收到edit值就保留新增功能?>

<form id="form2" name="form2" method="post" action="insert.php">
name:<input type="text" name="name" id="name">
tel:<input type="text" name="tel" id="tel">
address:<input type="text" name="address" id="address">
<input type="submit" name="button" id="button" value="新增" />
</form>

<? } ?>


update.php

<?php
$hostname_restaurant = "localhost";//
$database_restaurant = "restaurant";//
$username_restaurant = "kai";//
$password_restaurant = "admin";//
$link = mysql_pconnect($hostname_restaurant, $username_restaurant, $password_restaurant) or trigger_error(mysql_error(),E_USER_ERROR);
//使用 mysql_pconnect()而不使用mysql_connect( ) 可以省下每次重新開啟資料庫連接的時間

mysql_query("set names utf8");

$sql ="UPDATE taipei_eat SET name='$_POST[name]',tel='$_POST[tel]',address='$_POST[address]' WHERE id='$_POST[id]'";

$result = mysql_query($sql, $link) or die(mysql_error()); //執行sql語法

mysql_close($link); //關閉資料庫連結

header( "location:index.php"); //回index.php


?>

 

參考資料:http://muta1021.wordpress.com/category/php/

相关文章