PHP删除函数?

PHP delete function?

PHP初学这里,大概绑定到这个错误。这里有一个 crud 函数,试图让删除按钮起作用。用户当前位于 /crud/view.php。当前正在询问 "do you want to delete",然后刷新屏幕但没有任何反应。

<?php 

session_start();

if(isset($_SESSION['u_uid'])){

$uid = $_SESSION['u_id'];

require_once('connect.php');
$ReadSql = "SELECT * FROM `contact` WHERE users_id=$uid ORDER BY Name";
$res = mysqli_query($connection, $ReadSql);

?>
<!DOCTYPE html>

<html>
<head>
<title>Motoko</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
</script>
</head>
<body> 
<div class="container">
<div class="row">
    <table class="table">
        <tr>    

        <th><strong>Extras</strong></th>
        </tr>
        <?php 
        while($r = mysqli_fetch_assoc($res)){
        ?>
        <tr> 

            <td><a href="update.php?id=<?php echo $r['id'] ?>">Edit</a>
</td>
            <td><input type="button" onClick="deleteme(<?php echo 
$r['u_uid']; ?>)" name="Delete" value="Delete"></td>
             </tr>
 <script language="Javascript">
 function deleteme(delid)
 {
 if(confirm("Are you sure you want to Delete?")){
 window.location.href='delete.php';
 }
 } 
 </script>
        <?php } ?>
    </table>
</div>
</body>

</html>

<?php

}else{

header("Location: http://motoko.sorainsurance.com.au"); 

}

?>

/delete.php 为:

<?php

 if(isset($_SESSION['u_uid'])){

require_once('connect.php');

$select = "DELETE from contact where id='".$_GET['del_id']."'";
$query = mysqli_query($connection, $select) or die($select);
}else{

header("Location: http://motoko.sorainsurance.com.au/crud/view.php"); 

}
?>

更改这些行

<input type="button" onClick="deleteme('<?php echo 
$r['u_uid']; ?>')" name="Delete" value="Delete">


function deleteme(delid){
    if(confirm("Are you sure you want to Delete?")){
         window.location.href='delete.php?del_id='+delid;
    }
} 

总是喜欢在 HTML 脚本

顶部编写 php 代码
<?php 

session_start();

if(!isset($_SESSION['u_uid'])){
  header("Location: http://motoko.sorainsurance.com.au"); 
}

$uid = $_SESSION['u_id'];

require_once('connect.php');

$ReadSql = "SELECT * FROM `contact` WHERE users_id=$uid ORDER BY Name";
$res = mysqli_query($connection, $ReadSql);

?>
<!DOCTYPE html>
<html>
<head>
<title>Motoko</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
</script>
</head>
<body> 
<div class="container">
<div class="row">
    <table class="table">
      <tr>    
        <th>
          <strong>Extras</strong>
        </th>
      </tr>
      <?php while($r = mysqli_fetch_assoc($res)){ ?>
        <tr> 
          <td>
            <a href="update.php?id=<?php echo $r['id'] ?>">Edit</a>
          </td>
          <td>
            <input type="button" onClick="deleteme('<?php echo $r['u_uid']; ?>')" name="Delete" value="Delete">
          </td>
        </tr>
        <?php } ?>
    </table>
</div>
</body>
</html>
<script language="Javascript">
  function deleteme(delid){
    if(confirm("Are you sure you want to Delete?")){
      window.location.href='delete.php?del_id=delid';
    }
  } 
</script>

你能试试吗?

$select = "DELETE from contact where id='".$_GET['u_uid']."'";
<form>
<input type="button" value="Search Contact 1.0" onclick="window.location.href='http://motoko.sorainsurance.com.au/crud/searchone.php'" />
</form>

<form>
<input type="button" value="Search Contact 2.0" onclick="window.location.href='http://motoko.sorainsurance.com.au/crud/search.php'" />
</form>

<html>
<head>
    <title>Motoko</title>
        <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body> 
<div class="container">
<div class="row">
    <table class="table">
        <tr>

            <th><strong>Name</strong></th>
            <th><strong>Company</strong></th>
            <th><strong>Title</strong></th>
            <th><strong>Phone</strong></th>
            <th><strong>Email</strong></th>
            <th><strong>Address</strong></th>
            <th><strong>Extras</strong></th>
        </tr>
        <?php 
        while($r = mysqli_fetch_assoc($res)){
        ?>
        <tr> 
            <td><?php echo $r['Name']; ?></td> 
            <td><?php echo $r['Company']; ?></td> 
            <td><?php echo $r['Title']; ?></td> 
            <td><?php echo $r['Phone']; ?></td> 
            <td><?php echo $r['Email']; ?></td> 
            <td><?php echo $r['Address']; ?></td> 
            <td><a href="update.php?id=<?php echo $r['id'] ?>">Edit</a></td>
            <td> <input type="button" onClick="deleteme(<?php echo $r['u_uid']; ?>)" name="Delete" value="Delete">
             </tr>

     <script language="Javascript">
 function deleteme(delid) {
 if(confirm("Are you sure you want to Delete?")){
   window.location.href='delete.php?del_id='+delid;
 }
 } 
     </script>
        <?php } ?>
    </table>
</div>
    </body>

    </html>

    <?php

}else{

header("Location: http://motoko.sorainsurance.com.au"); 

}

?>