skip to Main Content

I’m trying to send a SQL command from a HTML button to copy contents of one table into another table, then delete the those contents from the original table. My SQL works if I run through PHPMyAdmin, but not from the button click. How do I get the button to process the query?

<?php include("datagrid/dataconfig.php"); ?>
<form action="Daily-Activity-Log.php" method="POST">
    <button type="submit" style="float:right;" class="btn btn-outline-success" name="dgrefresh"  onclick="refresh()"><span class="glyphicon glyphicon-refresh"></span></button>
</form>

<?php
    $con = mysqli_connect("localhost", "root", "");

    //REFRESH
        if(isset($_POST['dgrefresh'])){
            $refreshquery = "INSERT INTO `activityarchive` SELECT * FROM `activity` WHERE ActivityCompleted='1';
                                DELETE FROM `activity` WHERE ActivityCompleted='1'";

                $query_run = mysqli_query($con, $refreshquery);
                /*DEBUG MODE*/
                if($query_run)
              {
                echo '<script> alert("Data Saved"); </script>';
                header('Location: Daily-Activity-Log.php');
              }
              else {
                echo '<script> alert("Data not Saved"); </script>';
              }
            }
     ?>

<!-- REFRESH Script -->
<script>
  function refresh(){
    location.reload();
  }
</script>

Thank you Dharman for the link. I used the mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); command and found out that it wasn’t completely talking to the DB. Once I added that it worked as expected.

3

Answers


  1. Replace your
    <button type="submit">
    by an
    <input type="submit">

    and delete
    onclick="refresh()".

    Login or Signup to reply.
  2. Write two separate queries and execute them.

    <?php
        $con = mysqli_connect("localhost", "root", "");
        //REFRESH
        if(isset($_POST['dgrefresh'])){
        $refreshquery = "INSERT INTO `activityarchive` SELECT * FROM `activity` WHERE ActivityCompleted='1';";
        $deleteData = "DELETE FROM `activity` WHERE ActivityCompleted='1'";
    
        $query_run = mysqli_query($con, $refreshquery);
        mysqli_query($con, $deleteData); //Execute delete query here
        /*DEBUG MODE*/
        if($query_run){
            echo '<script> alert("Data Saved"); </script>';
            header('Location: Daily-Activity-Log.php');
        }
        else{
            echo '<script> alert("Data not Saved"); </script>';
        }
    }
    ?>
    
    Login or Signup to reply.
  3. First, PHP is a backend language.
    To execute you script, you’ve to point the action form attrib to the same page.

    E.g:

    //index.php
    <form action="index.php">
    ...
    

    So when your browser (ok, i mean server side ) a post request, it will execute your mysql query.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search