hallo!php中的地址簿
我是一名大學生,我想使用php製作地址簿,其中某人可以查看帶有所有列出的聯繫人詳細信息(例如姓名,電話,電子郵件)的表格,並且可能會有
-adding新聯繫人和
編輯專 -deleting現有的聯繫人。
我在我的數據庫中創建了一個包含相關字段的表,並且我編寫了一些代碼(在互聯網上可以找到,但它沒有工作+ 20小時試圖讓它自己工作......沒有正確的結果),顯示聯繫人的表,但:
編輯完全不
當刪除作品的工作,它會刪除從去年接觸到第一,而不是我選擇
行當我把註釋中的代碼刪除時,「添加」的作品
當刪除的代碼有效時,則「添加」實際上是「刪除」(除非該表是空的,在這種情況下,它加起來只有一個聯繫人)。
如果任何人都可以給我一些提示/建議改變什麼,請做!
這裏是我的代碼:
<html>
<head>
<title>Address Book</title>
</head>
<body>
<?php
mysql_connect("localhost", "mydb", "mypassword") or die(mysql_error());
mysql_select_db("mydb") or die(mysql_error());
if (isset($_POST['mode']))
{
$mode = $_POST['mode'];
$id = $_POST['id'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email= $_POST['email'];
if ($mode=="add")
{Print '<h2>Add Contact</h2> <p>
<form action="" method=post>
<table>
<tr><td>Name:</td><td><input type="text" name="name" /></td></tr>
<tr><td>Phone:</td><td><input type="text" name="phone" /></td></tr>
<tr><td>Email:</td><td><input type="text" name="email" /></td></tr>
<tr><td colspan="2" align="center">
<input type="hidden" name="mode" value="added" />
<input type="submit" value="Submit" />
</td></tr>
</table> </form> <p>'; }
if ($mode =="added")
{mysql_query ("INSERT INTO address (name, phone, email) VALUES ('$name', '$phone', '$email')");
echo "New contact added successfully!";}
if ($mode=="edit")
{Print '<h2>Edit Contact</h2> <p>
<form action="" method=post>
<table>
<tr><td>Name:</td><td><input type="text" value="';
Print $name; print '" name="name" /></td></tr>
<tr><td>Phone:</td><td><input type="text" value="';
Print $phone; print '" name="phone" /></td></tr>
<tr><td>Email:</td><td><input type="text" value="';
Print $email; print '" name="email" /></td></tr>
<tr><td colspan="2" align="center"><input type="submit" /></td></tr>
<input type=hidden name=mode value=edited>
<input type=hidden name=id value='; Print $id; print '>
</table>
</form> <p>';
}
if($mode=="edited")
{mysql_query ("UPDATE address SET name = '$name', phone = '$phone', email = '$email' WHERE id = $id");
Print "Data Updated!<p>";}
if ($mode=="remove")
{mysql_query ("DELETE FROM address where id=$id");
Print "Entry has been removed <p>";}
}
$data = mysql_query("SELECT * FROM address ORDER BY name ASC")
or die(mysql_error());
Print '<h2>Address Book</h2><p>
<form action="" method=post>
<table border cellpadding=3>
<tr><th width=100>Name</th><th width=100>Phone</th><th width=200>Email</th><th width=100 colspan=2>Admin</th></tr>
<td colspan=5 align=right>
<input type ="hidden" name = "mode" value="add"/> <input type = "submit" value="Add Contact"/>';
while($info = mysql_fetch_array($data))
{
Print "<tr><td>".$info['name'] . "</td> ";
Print "<td>".$info['phone'] . "</td> ";
Print "<td> <a href=mailto:".$info['email'] . ">" .$info['email'] . "</a></td>";
Print '<td>
<input type ="hidden" name="mode" vlaue="edit"/>
<input type ="submit" value="Edit"
?id='. $info['id'] .'&name=' . $info['name'] .'
&phone=' . $info ['phone'] .'&email=' . $info['email'] .'/></td>';
Print "<td>
<input type ='hidden' name='mode' value='remove'/>
<input type ='hidden' name='id' value = ".$info['id']." />
<input type ='submit' value = 'remove' /> </td></tr> ";
}
Print "</table>";
Print " </form>";
if(!$mode) echo "You may add, edit or delete a contact";
echo $mode;
?>
</body>
</html>
如果我可以提出一個建議,也許閱讀[pear(php)代碼標準](http://pear.php.net/manual/en/standards.control.php)將是有益的;特別是如果你剛剛開始關於你的格式。 – Ross 2011-06-11 20:11:35
你根本不知道HTML表單是如何工作的。爲了得到一些工作,忘記刪除一分鐘,每行只有一個表單進行編輯。得到這個工作,然後爲刪除添加第二個表單。 – Cups 2011-06-11 21:23:43
@Cups,我試圖用刪除(看起來比編輯更容易),它不斷與「添加」混合... 你是對的,有很多事情我需要找出有關如何形式工作... – vaso 2011-06-12 06:40:13