php我不能删除表中的一行

nfg76nw0  于 2021-06-20  发布在  Mysql
关注(0)|答案(4)|浏览(324)

无法删除使用“删除”输入选择的行。我想我必须在querydelete中设置其他参数,但我不知道具体是什么。
有人能帮我吗,因为我是初学者?有没有别的办法做这种事?
我的目的是推送delete输入,然后删除具有特定id的客户(与delete输入在同一raw中)。
谁能给我一个例子的链接?

<!DOCTYPE html>
 <html>
 <head>
 <title>Table with database</title>
<style>
 table {
  border-collapse: collapse;
 width: 100%;
  color: #588c7e;
  font-family: monospace;
  font-size: 25px;
  text-align: left;
   } 
th {
  background-color: #588c7e;
  color: white;
   }
 tr:nth-child(even) {background-color: #f2f2f2}
  </style>
  </head>
  <body>
  <table>
  <tr>
  <th>ID</th> 
  <th>Room</th> 
  <th>Name</th>
  <th>Check In</th> 
  <th>Check Out</th> 
   </tr>

 <?php

  include('db_connection.php');
  $conn = OpenCon();

    //SQL query 
     $query = "Select * from ergazomenos"; 

    if(isset($_POST['delete'])){
    $querydelete = "delete from ergazomenos where trim(ID) 
    ='$_POST[hidden]'";
   $queryexee = mysqli_query($conn, $querydelete);

    }

    $result = mysqli_query($conn, $query); 

    if (!$result){
       echo("Error description: " . mysqli_error($conn));
    }

    //query database 

    while($rows = mysqli_fetch_array($result)){

         $ID = $rows['ID'] ;
            $Room = $rows['Room'] ;
         $Name = $rows['Name'];
         $CheckIn = $rows['Check In'] ;
         $CheckOut = $rows['Check Out'] ;

        //echo "</td><td>" . $ID. "</td><td>" "<input type=hidden ID=hidden 
value=" . $rows['ID'] . $Room. "</td><td>". $Name. "</td><td>" . $CheckIn. " 
</td><td>" . $CheckOut. "</td><td>";

            echo "</td><td>" . $ID. "</td><td>" . $Room. "</td><td>". $Name. 
  "</td><td>" . $CheckIn. "</td><td>" . $CheckOut. "</td><td>"; 
            </td>";

        echo ("<form action=delete.php method=post>");
        echo ("<tr><td><div align=\"center\"> $ID </div>" . "<input 
        type=hidden name=hidden value=".$rows['ID'] . "</td> <td><div 
        align=\"center\"> 
      $Room </div></td> <td><div align=\"center\"> $Name </div></td> <td><div 
  align=\"center\"> $CheckIn </div></td> <td><div align=\"center\"> $CheckOut 
  </div></td> <td><div align=\"center\">  <td><div   ");    
        echo ("<td>" . "<input type=submit name=delete value=delete"  . " 
  </td>");      
        //    
          echo ($rows['ID']);
                //echo '<td><input type="button" name="delete" 
  value="delete"></td>';
                echo ("</tr>");
       echo ($_POST['hidden']);
      }  

  CloseCon($conn);

   ?> 

    </table>

  <button type="button" onclick="alert('Hello world!')">Insert</button>
  <button type="button" onclick="alert('Hello world!')">Update</button>

</body>
</html>
noj0wjuj

noj0wjuj1#

必须使用(单引号或转义双引号)作为 input 标签:

<input type='hidden' name='hidden' [etc.]
qij5mzcb

qij5mzcb2#

尝试将$querydelete更改为:

$querydelete = "delete from ergazomenos where trim(ID) 
='$_POST[hidden]'";

$querydelete = "delete from ergazomenos where trim(ID)=". $_POST['hidden'];

这应该会有帮助。我没有测试代码,tho。

lf5gs5x2

lf5gs5x23#

分享我的风格。
在网格中创建名为 Action 沿着每个db驱动的记录。

<td>
    <a href="delete.php?id=<?php echo $rows['ID'];?>">Delete</a>
</td>

delete.php 文件:

include('connection.php');
$id = $_GET['id'];
$query= "delete from table where id = '$id'";
mysqli_query($conn, $query); // or $dbConn->query($query);

重定向到网格页。

6ljaweal

6ljaweal4#

应该使用prepare语句来防止sql注入

<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$id = $_POST['hidden'];

// if you are using ID, make sure variable is number
if (is_numeric(id)) {

delete from ergazomenos where trim(ID) 
    ='$_POST[hidden]'

/* create a prepared statement */
if ($stmt = mysqli_prepare($link, "DELETE FROM ergazomenos WHERE trim(ID) = ?")) {

    /* bind parameters for markers */
    mysqli_stmt_bind_param($stmt, "s", $id);

    /* execute query */
    mysqli_stmt_execute($stmt);

    /* close statement */
    mysqli_stmt_close($stmt);
}

/* close connection */
mysqli_close($link);
}
?>

这样更安全。

相关问题