简体   繁体   English

如何一键删除多个数据库中的记录

[英]How to delete records from multiple database with one button

I searched around and got close, but no cigar.我四处搜寻并靠近,但没有雪茄。 I can make this work with only one database, though I am attempting to delete all records (without deleting the row structure) from 3 databases with one button.我只能使用一个数据库来完成这项工作,尽管我试图用一个按钮从 3 个数据库中删除所有记录(不删除行结构)。 When I try to do this with 3 databases, I get "Page is not working" and no error codes.当我尝试使用 3 个数据库执行此操作时,我得到“页面不工作”并且没有错误代码。 Any thoughts on what I'm missing?对我所缺少的有什么想法吗? Below is my codes and thank you all for any assistance.以下是我的代码,感谢大家的帮助。 The button is on delete1.php and passes to delete2.php该按钮位于 delete1.php 并传递给 delete2.php

UPDATE 2: Okay so now with this update on both pages, I get zero errors however it will only list the entries in the first database, and also will only empty the entries on the first database.更新2:好的,现在在两个页面上都进行了此更新,我得到零错误,但是它只会列出第一个数据库中的条目,也只会清空第一个数据库中的条目。 Any thoughts?有什么想法吗?

https://www.peredy1.com/select/delete1.php https://www.peredy1.com/select/delete1.php

Delete1.php删除1.php


    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <body text="#FFFFFF" bgcolor="#000000" link="#FFFF00" vlink="#FFFF00" alink="#FFFF33">
    <font face="Verdana, Arial, Helvetica, sans-serif"> 
    <meta http-equiv="refresh" content="15" ></font>
    <p> <?php
    $servername = "localhost";
    $username = " ";
    $password = " ";
    $dbname = "vbauyvmy_randomname";
    
    
    $servernameA = "localhost";
    $usernameA = " ";
    $passwordA = " ";
    $dbnameA = "vbauyvmy_reqlinksbu";
    
    
    $servernameB = "localhost";
    $usernameB = " ";
    $passwordB = " ";
    $dbnameB = "vbauyvmy_ppreq_busend";
    
    
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
    }
    
    $sql = "SELECT id, name, band, song, extra, band2, song2, extra2, band3, song3, extra3 FROM nametable";
    $result = $conn->query($sql);
    
    
    $resultt = mysqli_query($conn, "select COUNT(id) AS count FROM `nametable`");
    
    if(!$result) {
        die('Error: ' . mysqli_error($link));
    } else {
        $num_rows = mysqli_fetch_assoc($resultt);
        // echo it
        echo "<font color='red'>Total Songs</font>: <font color='yellow'><b>" . $num_rows['count']."</font></b></br></br>";
    }
    
    
    
    $connA = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($connA->connect_error) {
      die("Connection failed: " . $connA->connect_error);
    }
    
    $sql = "SELECT id, name, band, song, extra, band2, song2, extra2, band3, song3, extra3 FROM nametable";
    $result = $connA->query($sql);
    
    
    $resultt = mysqli_query($connA, "select COUNT(id) AS count FROM `nametable`");
    
    if(!$result) {
        die('Error: ' . mysqli_error($link));
    } else {
        $num_rows = mysqli_fetch_assoc($resultt);
        // echo it
        echo "<font color='red'>Total Songs</font>: <font color='yellow'><b>" . $num_rows['count']."</font></b></br></br>";
    }
    
    
    
    
    
    
    $connB = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($connB->connect_error) {
      die("Connection failed: " . $connB->connect_error);
    }
    
    $sql = "SELECT id, name, band, song, extra, band2, song2, extra2, band3, song3, extra3 FROM nametable";
    $result = $connB->query($sql);
    
    
    $resultt = mysqli_query($connB, "select COUNT(id) AS count FROM `nametable`");
    
    if(!$result) {
        die('Error: ' . mysqli_error($link));
    } else {
        $num_rows = mysqli_fetch_assoc($resultt);
        // echo it
        echo "<font color='red'>Total Songs</font>: <font color='yellow'><b>" . $num_rows['count']."</font></b></br></br>";
    }
    
    
    
    ?> 
      <input type="submit" id="deletebutton" name="deleteall" value="Reset Show">
    
    <br>
      <?php
    if ($result->num_rows > 0) {
      // output data of each row
      while($row = $result->fetch_assoc()) {
    
    
    
    
    
    if(!empty($row["name"])  ){
        echo " ";
      }
    
    
    if(!empty($row["band2"]) && !empty($row["song2"])  ){
        echo " ";
      }
    
    
    
    if(!empty($row["band3"]) && !empty($row["song3"])  ){
        echo " ";
      }
    
    
    
    
    
    
      }
    } else {
      echo "0 Records";
    }
    
    $connB->close();
    ?> </p>
    <p>&nbsp;</p>
    <script>
    $(document).ready(function(){
      $("#deletebutton").click(function(){
      if(confirm("Are you sure you want to delete all record?")){ 
        /* user clicked "OK" */ 
        location.href = "delete2.php";
    } 
    else { 
     
    }
      });
    });
    
    </script>

delete2.php删除2.php


    <?php
    
    
    $servername = "localhost";
    $username = "vbauyvmy_peredyrandom";
    $password = "Destined1!2";
    $dbname = "vbauyvmy_randomname";
    
    $servernameA = "localhost";
    $usernameA = "vbauyvmy_peredylinksbu";
    $passwordA = "Destined1!2";
    $dbnameA = "vbauyvmy_reqlinksbu";
    
    $servernameB = "localhost";
    $usernameB = "vbauyvmy_busendperedy";
    $passwordB = "Destined1!2";
    $dbnameB = "vbauyvmy_ppreq_busend";
    
    
    $id = $_GET['id'];
    //Connect DB
    //Create query based on the ID passed from your table
    //query : delete where id = $id
    // on success delete : redirect the page to original page using header() method
    
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    
    
    $connA = new mysqli($servername, $username, $password, $dbname);
    if (!$connA) {
        die("Connection A failed: " . mysqli_connect_error());
    }
    
    
    $connB = new mysqli($servername, $username, $password, $dbname);
    if (!$connB) {
        die("Connection B failed: " . mysqli_connect_error());
    }
    
    
    
    
    // sql to delete a record
    $sql = "DELETE FROM nametable"; 
    
    if (mysqli_query($conn, $sql)) {
        mysqli_close($conn);
    
    } else {
        echo "Error deleting record";
    }
    
    
    if (mysqli_query($connA, $sql)) {
        mysqli_close($connA);
    
    } else {
        echo "Error deleting record";
    }
    
    
    if (mysqli_query($connB, $sql)) {
        mysqli_close($connB);
        header('Location: delete1.php'); 
        exit;
    } else {
        echo "Error deleting record";
    }
    
    
    
    
    
    
    ?>

I got it.我知道了。 And HUGE thanks to all of you for the help.非常感谢你们所有人的帮助。

The problem was in all of the comments above AND... I forgot to edit the A and B in the connections for both pages at问题出在上面的所有评论中,并且...我忘记在两个页面的连接中编辑 A 和 B


    $connA = new mysqli($servernameA, $usernameA, $passwordA, $dbnameA);

Thank you ALL SOOOOO MUCH I learned a lot on this one.谢谢大家,我学到了很多。 I'm very greatful!我非常感激!

One thing you can try is to add something to delete in your file delete2.php .您可以尝试的一件事是在文件delete2.php中添加要删除的内容。

$sql = "DELETE FROM nametable"; you are missing the data that should be deleted.您缺少应该删除的数据。

Between DELETE and FROM you have to add the data that should be deleted.DELETEFROM之间,您必须添加应该删除的数据。

if you want to delete everything try this:如果您想删除所有内容,请尝试以下操作:

$sql = "DELETE * FROM nametable";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM