Add Delete Button to PHP results table

戏子无情 提交于 2019-12-03 23:05:23

You have to pass variable in delete link. You must have to pass <?php echo $contact['name']; ?> name value in hidden field or pass this value in URL

Replace

<td class="contact-delete">
      <form action='delete.php' method="post">
      <input type="hidden" name="name" value="">
      <input type="submit" name="submit" value="Delete">
      </form>
</td>

With

<td class="contact-delete">
    <form action='delete.php?name="<?php echo $contact['name']; ?>"' method="post">
        <input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
        <input type="submit" name="submit" value="Delete">
    </form>
</td>

USe javascript

<input name="Submit2" type="button" class="button" onclick="javascript:location.href='delete.php?id=<?php echo $your_id;?>';" value="&laquo; Back" />

and in delet.php

$id=$_GET['id'];

and put $id in your sql statement.

<input type="hidden" name="name" value="">

You are missing a value which wil be picked up by this line in your delete file.

$query = "DELETE FROM contacts WHERE name={$_POST['name']} LIMIT 1";

Right now it isn't receiving anything, which is why it will not work.

So add a value to it and it will work. Example:

<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">

You are missing to pass name in this line:

<input type="hidden" name="name" value="">

You need to have something (<?php echo $contact['name']; ?>) in the value attribute.

BTW, do not use deprecated mysql_* functions, use PDO or mysqli_* instead.

cardeol

At first, you cannot write the code in that way, the code has no protection against SQL injection.

1) Try to use primary ID's instead of using a name (what happens if 2 people has the same name?).

So, you can create a hidden field to know what person you are handling with.

<input type="hidden" name="contact_id" value="<?php $contact['contact_id']; ?>">

2) Sanitize variables to avoid attacks:

<?php $contact_id = isset($_POST['contact_id'])?intval($_POST['contact_id']):0;

// proceed with the query
if($contact_id>0) { $query = "DELETE FROM contacts WHERE contact_id = '$contact_id'";

}

// redirect to the main table with header("location: main.php");

?>
Prakash Bora

This is a php delete process script you link this code in your delete button

<?php
$link=mysql_connect("localhost","root","");

mysql_select_db("photo",$link);

if(isset($_GET["id"])

    $photoid=$_GET['id'];
$sql="delete from photos where photoid=".$_GET['id'];
$result=mysql_query($sql,$link);
header("location:home.php?ok");
?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!