开发者

addressbook in php

hallo!

I am a university student, and I want to make an address book, using php, where someone can view a table with all the listed contacts' details (e.g. name, phone, email) and there will be the possibility of

-adding a new contact and

-editing -deleting an existing contact.

I have created a table with the relevant fields in my database, and I have written some code (found on the internet but it didn't work + 20hours of trying to make it work on my own.... without the proper result) which shows the table of contacts, but:

The edit doesn't work at all

When the delete works, it deletes the contacts from last to first and not the row I am selecting

when I put the code for deleting in comments, the "Add" works

when the code for deleting is functional, then the "Add" actually "Deletes" (unless the table is empty, in which case it adds up to ONE contact).

If anyone can give me some hints/suggestions on what to change, please please do!

Here is my code:

<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" val开发者_Python百科ue="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>


The reason why things aren't behaving the way you expect is that all your rows are being put into one big form. Therefore there are multiple hidden fields <input type ='hidden' name='id' value = ".$info['id']." />, all of which get submitted when you click any of your 'submit' buttons. But actually the value that will be passed along to your script will be the last value i.e. the last row id.

One way you could get around it is to use a link with the ID as a $_GET argument in the URL, for example:

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

Then you can change the line near the top to use $mode = $_GET['mode']; and $id=$_GET['id']

Once you have got the ID in the way, you can display a form with editable fields just for that particular id.

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

Of course, the above example is vulnerable to SQL injection http://php.net/manual/en/security.database.sql-injection.php so a slightly more robust method would be wise - see the link for advice!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜