PHP删除功能?

时间:2018-02-06 05:02:36

标签: php mysql error-handling crud sql-delete

这里的PHP初学者,可能会遇到这个错误。这里有一个crud函数,试图让删除按钮工作。用户目前将在/crud/view.php上。它目前要求"你想删除"屏幕刷新,没有任何事情发生。

<?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"); 

}
?>

3 个答案:

答案 0 :(得分:1)

更改这些行

//accesor methods
public String getName() {
    return this.name;
}
public int getId() {
    return this.id;
}
public double getBalance() {
    return this.balance;
}
public double getInterest() {
    return this.interest;
}

//mutator methods
public void setName(String name) {
    String accName = null;
    this.name = accName;
}
public void setBalance(double balance) {
    double accBalance = 0;
    this.balance = accBalance;
}
public void setInterest(double interest) {
    double monthlyInterest = 0;
    this.interest = monthlyInterest;
}

总是首选在HTML脚本顶部写下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;
    }
} 

答案 1 :(得分:0)

你能试试吗?

$select = "DELETE from contact where id='".$_GET['u_uid']."'";

答案 2 :(得分:0)

<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"); 

}

?>