简体   繁体   中英

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. When I try to do this with 3 databases, I get "Page is not working" and no error codes. 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

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. Any thoughts?

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

Delete1.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


    <?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


    $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 .

$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.

if you want to delete everything try this:

$sql = "DELETE * FROM nametable";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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