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
Replace your
<button type="submit">
by an
<input type="submit">
and delete
onclick="refresh()"
.Write two separate queries and execute them.
First, PHP is a backend language.
To execute you script, you’ve to point the action form attrib to the same page.
E.g:
So when your browser (ok, i mean server side ) a post request, it will execute your mysql query.