-4

I need to update & delete data in the same page.

My problem is how to get the update or delete record id in the same page while the user clicks the "update" or "delete" link.

Anyone please help how to update and delete records in same page.

Please find my code below (View.php)

View.php

<?php
$q =($_GET['q']);
if($q=="all")
{
    $result=mysql_query("select * from password");
}
else
{
$result=mysql_query("select * from password WHERE tag = '".$q."'");
}
if($result === false )
{
    die(mysql_error());
}
?>
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>username</th>
<th>Password</th>
<th>Account Type</th>
<th>Link</th>
<th>update</th>
<th>Delete</th>
</tr>
</thead>
<?php

while($row=mysql_fetch_array($result))
{
    $id=$row['id'];
     $name=$row['name'];
    $url=$row['url'];
    $uname=$row['username'];
    $pass=$row['password'];
    $tag=$row['tag'];
    $des=$row['description'];

    ?>
    <tbody>
    <tr>
    <td><?php echo $id; ?></td>
    <td><?php echo $name; ?></td>
    <td><?php echo $uname; ?></td>
    <td><span data-tooltip aria-haspopup="true" class="has-tip" title="<?php echo $pass; ?>">View</span></td>
    <td><?php echo $des; ?></td>
    <td><a href="<?php echo $url; ?>" title="<?php echo $name; ?>" target="_blank">Link</a></td>
    <td><a href="update.php?<?php echo $id; ?>">Update</a></td>
    <td><a href="delete.php?<?php echo $id; ?>">Delete</a></td>
    </tr>
    </tbody>
    <?php

}

?>
</table>
S.L. Barth
  • 7,954
  • 71
  • 47
  • 62
CodeMan
  • 1,691
  • 4
  • 23
  • 42
  • 4
    Note this this code is open to sql injection – Patrick Apr 17 '15 at 10:05
  • 1
    Welcome to Stack Overflow! I've edited your question a bit. You can improve it further by indenting your code properly. As has been pointed out, this code is open to SQL Injection. There seem to be other security vulnerabilities as well. Once you can do the update & delete thing, you would do well to read up on password hashing. You may want to read http://stackoverflow.com/questions/549/the-definitive-guide-to-form-based-website-authentication when you have some time. Good luck! – S.L. Barth Apr 17 '15 at 19:05
  • I can see what you're trying to do here: you have links to update and delete scripts, and you're trying to pass the value `$id` via the query string. However, these needs keys, as @ajaykumartak points out. Furthermore, you don't want to delete/update in a hyperlink, since that's a GET operation - you want to modify the database in a POST. That's a big topic, so you may find doing some (good) tutorials of use here. – halfer Apr 17 '15 at 20:01

3 Answers3

1
try it as i have
(sample.php)

//db connections..
if ($_GET['delete_id']!="")
{
//delete statement goes here....
}
$result = mysql_query("SELECT commentid FROM comments") 
or die(mysql_error());  

while($row = mysql_fetch_array( $result )) {
?>
    <a href="sample.php?delete_id=<?=$row['id']?>">delete</a>
<?php } ?>
Imad Ullah
  • 397
  • 2
  • 10
0

By clicking a link you should send back some information to the server. This can happen via POST, GET (not recommended) or AJAX if you want to create an interactive look an feel.

Robert
  • 4,862
  • 1
  • 15
  • 35
0

In your code you are using two different file to to update and delete your records.

Example : update.php and delete.php and you are sending the id in parameter.

If you want to keep same page for both then create a page named with action.php and send one more parameter along with id.

Like : update.php?id=<?php echo $id; ?>&type=update/delete

you can keep any name for action.php page that was only for example.

Note : Also keep in mind about @patrick comment.

ajaykumartak
  • 746
  • 9
  • 27