代码之家  ›  专栏  ›  技术社区  ›  kerry sun

PHP删除函数?

  •  1
  • kerry sun  · 技术社区  · 7 年前

    这里是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"); 
    
    }
    
    ?>
    

    使用/删除。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 回复  |  直到 6 年前
        1
  •  1
  •   Aman Maurya    7 年前

    更改这些行

    <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>
    
        2
  •  0
  •   user9324656 user9324656    7 年前
    <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"); 
    
    }
    
    ?>
    
        3
  •  0
  •   jerome    7 年前

    你能试试吗?

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