skip to Main Content

I’m working with Android Studio,
and using php and mySQL to use my data.

my table’s name is ‘laundry’
and it has columns named ‘lmachine’, ‘lroom’, and ‘ltime’.

I used below code to insert new data to my db and it worked well.

<?php

include 'DatabaseConfig.php' ;
 
 $con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);

if ( isset( $_POST['lmachine'] ) && isset( $_POST['lroom'] ) && isset( $_POST['ltime'])) {
    $lmachine = $_POST['lmachine'];
    $lroom = $_POST['lroom'];
    $ltime = $_POST['ltime'];


    $Sql_Query = "insert into laundry(lmachine,lroom, ltime) values ('$lmachine','$lroom', '$ltime')";
         
    if(mysqli_query($con,$Sql_Query)){
        echo 'Data Submit Successfully';
    }
    else{
        echo 'Try Again';
    }

    mysqli_close($con);
}


?>

in this php file, I want to add a query to delete rows which ‘ltime’ is older than now.

that query is ‘delete from laundry where ltime < now()’. I checked whether this query works (in phpMyAdmin console) and it worked.

I want to put that query in upper php file.

so tried this code

<?php

include 'DatabaseConfig.php' ;
 
 $con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);

 if (isset($_GET['ltime'])){
     $sql = "delete from laundry where ltime < now()";

     if(mysqli_query($con, $sql)){
        echo 'Data Deleted Successfully';
     }
     else{
        echo 'Try Again';
     }
 }

if ( isset( $_POST['lmachine'] ) && isset( $_POST['lroom'] ) && isset( $_POST['ltime'])) {
    $lmachine = $_POST['lmachine'];
    $lroom = $_POST['lroom'];
    $ltime = $_POST['ltime'];


    $Sql_Query = "insert into laundry(lmachine,lroom, ltime) values ('$lmachine','$lroom', '$ltime')";
         
    if(mysqli_query($con,$Sql_Query)){
        echo 'Data Submit Successfully';
    }
    else{
        echo 'Try Again';
    }

    mysqli_close($con);
}


?>

and it doesn’t work. I wonder if my approach was wrong or it’s matter of syntax.

  • ‘ doesn’t work ‘ means… when I run a project using this php file in Android Studio, there’s no error log, inserting data works as before but deleting data doesn’t work. And the php file itself also works without any error message.

2

Answers


  1. <?php
    include 'DatabaseConfig.php' ;
     
     $con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);
    
    if ( isset( $_POST['lmachine'] ) && isset( $_POST['lroom'] ) && isset( $_POST['ltime'])) {
        $lmachine = $_POST['lmachine'];
        $lroom = $_POST['lroom'];
        $ltime = $_POST['ltime'];
    
        // Delete earlier data
         $sql = "delete from laundry where ltime < now()";
         mysqli_query($con, $sql);
    
    
        // Add new data
        $Sql_Query = "insert into laundry(lmachine,lroom, ltime) values ('$lmachine','$lroom', '$ltime')";
             
        if(mysqli_query($con,$Sql_Query)){
            echo 'Data Submit Successfully';
        }
        else{
            echo 'Try Again';
        }
    
        mysqli_close($con);
    }
    
    
    ?>
    
    Login or Signup to reply.
  2. <?php
    
    include 'DatabaseConfig.php' ;
     
     $con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);
    
     if (isset($_GET['ltime'])){
         $sql = "delete from laundry where ltime < ".time();
    
         if(mysqli_query($con, $sql)){
            echo 'Data Deleted Successfully';
         }
         else{
            echo 'Try Again';
         }
     }
    
    if ( isset( $_POST['lmachine'] ) && isset( $_POST['lroom'] ) && isset( $_POST['ltime'])) {
        $lmachine = $_POST['lmachine'];
        $lroom = $_POST['lroom'];
        $ltime = $_POST['ltime'];
    
    
        $Sql_Query = "insert into laundry(lmachine,lroom, ltime) values ('$lmachine','$lroom', '$ltime')";
             
        if(mysqli_query($con,$Sql_Query)){
            echo 'Data Submit Successfully';
        }
        else{
            echo 'Try Again';
        }
    
        mysqli_close($con);
    }
    
    
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search