没有复选框我想删除删除按钮上的特定值

时间:2018-01-24 11:08:20

标签: php mysql

这是我的代码

    <?php
$conn=mysqli_connect('localhost','root','','admin');
$sql="SELECT * FROM admin";
$result=mysqli_query($conn,$sql);
while($row=$result->fetch_assoc())
{
    $id=$row['id'];
    $name=$row['name'];
    $email=$row['email'];   
?>
<table>
        <tr><td><?php echo $id ?></td><td><?php echo $name ?></td><td><?php echo $email ?></td><td><form action="111.php" method="post"><input type="hidden" name="row_id" id="row_id"> value="<?php echo $id ?>"><input type="submit" name="edit" id="row_id" value="edit"></form></td><td><form action="111.php" method="post"><input type="submit" name="delete" value="delete"></form></td></tr>

</table>
<?php
}
?>

点击按钮后的另一个php文件

<?php
if(isset($_POST['edit']))
{
    $id=$_POST['row_id'];
    echo $id;
}

if(isset($_POST['delete']))
{
    $id=$_POST['row_id'];
    echo $name;
}
?>

我的输出是

1   aaa aaaaa value="1">edit(button) delete(button)
2   bbb bbbbbb  value="2">edit(button) delete(button)
3   ccc cccccc  value="3">edit(button) delete(button)

当我点击编辑按钮或删除按钮时 没有打印id的值 我预期的输出是

when click button only specific id print

2 个答案:

答案 0 :(得分:1)

目前您没有<form>,因此单击按钮时无需“提交”。在表单中包装按钮,并在该表单中包含id值的元素:

<td>
    <form method="POST" action="yourPHPScript.php">
        <input type="hidden" name="id" value="<?php echo $row['id'] ?>">
        <input type="submit" name="delete" value="delete">
    </form>
<td>

发布此表单后,$_POST['id']将包含您要查看的值bind to your query

注意:您还在代码中创建了多个。你确定你想要那样做吗?或者说,在表格中只有多个?在结构上更像是这样:

<table>
    <?php while($row = $result->fetch_assoc()) ?>
        <tr>
            ....
        </tr>
    <?php } ?>
</table>

此外,您的DELETE查询不应指定列。您删除整行,而不是该行中的特定值:

DELETE FROM user WHERE id=?

答案 1 :(得分:-1)

(1.) @funk-forty-niner is Correct. 
     Your Connection Query is Wrong.It is mysql_connect();

   <?php
    $conn = mysql_connect("localhost","root","");
    $db = mysql_select_db('db_name',$conn);
    ?>

    But How you can get output from database.

(2.). and For particular delete button, try this code.

<?php

     //Connection code
    $conn = mysql_connect("localhost","root","");
    $db = mysql_select_db('db_name',$conn);

if(isset($_GET['del'])){
   $idGet = $_GET['del'];

  $del = "delete from user where id=$idGet";
   $exe = mysql_query($del);
header('location:view.php');    

}

$sql_view = "select * from user";

$exe_view = mysql_query($sql_view);
        $table="";
while($arr_view=mysql_fetch_array($exe_view)){
    $id = $arr_view['id'];          
    $name = $arr_view['name'];  


    $table.="<tr>
                <td>$id</td>
                <td>$name</td>
                <td><a href='view.php?del=$id'>DELETE</a></td>
                </tr>";
}

?>  
Hope This will Help U.