2011-06-11 129 views
0

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> 
+0

如果我可以提出一個建議,也許閱讀[pear(php)代碼標準](http://pear.php.net/manual/en/standards.control.php)將是有益的;特別是如果你剛剛開始關於你的格式。 – Ross 2011-06-11 20:11:35

+0

你根本不知道HTML表單是如何工作的。爲了得到一些工作,忘記刪除一分鐘,每行只有一個表單進行編輯。得到這個工作,然後爲刪除添加第二個表單。 – Cups 2011-06-11 21:23:43

+0

@Cups,我試圖用刪除(看起來比編輯更容易),它不斷與「添加」混合... 你是對的,有很多事情我需要找出有關如何形式工作... – vaso 2011-06-12 06:40:13

回答

1

事情爲什麼不表現你期望的方式的原因是,所有的行會被放進一個大單。因此,有多個隱藏字段<input type ='hidden' name='id' value = ".$info['id']." />,當您單擊任何「提交」按鈕時都會提交所有這些字段。但實際上,將傳遞給腳本的值將是最後一個值,即最後一行ID。你可以繞過它

一種方法是使用一個鏈路與ID爲URL中的$ _GET參數,例如:

<a href="scriptname.php?mode=edit&id=".$info['id'].">Edit</a> 

然後您可以更改頂部附近使用$mode = $_GET['mode'];行和$id=$_GET['id']

一旦你已經得到了ID的方式,你可以顯示一個窗體的編輯字段只是該特定的ID。

if ($mode == 'edit') 
{ 
    $data = mysql_query("SELECT * FROM address WHERE id=$id"); 
    // then fetch the row data and populate the HTML form 
} 

當然,上面的例子是容易受到SQL注入http://php.net/manual/en/security.database.sql-injection.php所以稍微更可靠的方法將是明智的 - 看到該鏈接的意見!

+0

非常感謝,我會嘗試一下! 至於SQL注入漏洞,這對我來說太弱了,但是會在後面記住:) – vaso 2011-06-12 06:34:25

+0

邁克爾,非常感謝你,我做了你所說的和它的工作! – vaso 2011-06-13 20:56:54

+0

嘿,太好了!如果這是固定它的話,隨意標記我的答案;) – 2011-06-17 09:08:31