drls2738 2017-03-10 10:15
浏览 83
已采纳

如何从MYSQL数据库中检索数据并在Android上点击Button进行验证

I have an Android app which is used to insert user records for each User.Each user can input data into the database only 2 times a day. If the user tries to input data the third time(click the Submit button),a new activity(in Android) will be shown with a message and the date field(TextView) should be incremented by 1 Day.I am able to add the date field by 1 day on every click. So what I have tried is to get the count of data in the MYSQL database of each user for the particular day and if the count is greater than 3, then the PHP Script will return an echo statement according to which the Async Task in Anroid will function and show the new Activity.

My PHP Script is:

   <?php
require "conn.php";
require "SalesLogin.php";

$enquiry = $_POST["enquiry"];
$retail = $_POST["retail"];
$collection = $_POST["collection"];
$booking = $_POST["booking"];
$evaluation = $_POST["evaluation"];
$test_drive = $_POST["test_drive"];
$home_visit = $_POST["home_visit"];
$user_name = $_POST["user_name"];
$update_date = $_POST["date"];
$absent = $_POST["absent"];


$timezone = new DateTimeZone("Asia/Kolkata" );
$date = new DateTime();
$date->setTimezone($timezone );
$time =  $date->format( 'H:i:s A' );


$sql = "UPDATE employee_details SET
enquiry_sum = (SELECT SUM(enquiry) +'$enquiry' FROM (SELECT * FROM employee_details WHERE date = CURDATE() AND name = '$user_name') AS x)
WHERE date = CURDATE() AND name = '$user_name'";
$res = $conn->query($sql);

$check = "UPDATE employee_details SET enquiry_target_status = ( SELECT IF (MAX(enquiry_sum) = 52, 'ACHIEVED', 'NOT ACHIEVED') FROM (SELECT * from employee_details WHERE date = CURDATE() AND name = '$user_name') AS Y ) WHERE date = CURDATE() AND name = '$user_name'";
$insert_status = $conn->query($check);


$miss_count = "UPDATE employee_details SET enquiry_target_missed_by = (SELECT (50 - MAX(enquiry_sum)) FROM (SELECT * from employee_details WHERE date = CURDATE() AND name = '$user_name') AS Z ) WHERE date = CURDATE() AND name = '$user_name'";
$insert_status = $conn->query($miss_count);


$mysql_qry1 = "INSERT INTO employee_details(enquiry,retail, 
collection,booking, evaluation, test_drive, home_visit, name, date,time,absent) values ('$enquiry','$retail','$collection','$booking','$evaluation','$test_drive',
'$home_visit','$user_name','$update_date','$time','$absent');";

if($conn->query($mysql_qry1) === TRUE) 
    echo "Your details has been successfully inserted.";

else 
    echo "Error: " .$mysql_qry1. "<br>" . $conn->error;


if($update_date != $date){
$mysql_qry2 = "UPDATE employee_data SET last_updated_date = (DATE_ADD('$update_date', INTERVAL 1 DAY)) WHERE name = '$user_name';";
$conn->query($mysql_qry2);
echo "Date changed," .$mysql_qry2;
} 


$mysql_qry3 = "SELECT COUNT(*) from employee_details WHERE date = '$update_date' and name LIKE '$user_name';";
$conn->query($mysql_qry3);
      if($mysl_qry3 <= 2) 
              {
                   echo "You can login.";
              } 
         else 
              {
                  echo "You cannot login anymore for today.";
             }


$conn->close();
?>

My AsyncTask code is:

@Override
    protected void onPostExecute(String result) {
        try {
            if (result.contains("Welcome") && result.contains("Date")) {
                String[] str = result.split("_");
                String name = str[1];

                String[] date = result.split(":");
                String upd_date = date[1];
                Intent intent = new Intent(context,InsertDataActivity.class);
                intent.putExtra("username",name);
                intent.putExtra("date",upd_date);
                context.startActivity(intent);
            }
            else if (result.contains("Login failed.") && !(result.contains("Undefined")))

                Toast.makeText(context,"Invalid Crendentials.",Toast.LENGTH_LONG).show();

            else if (result.contains("You cannot login anymore for today.")) {
                Log.i("Check",result);
                Intent intent = new Intent(context,ThankYouActivity.class);
                context.startActivity(intent);

            }

        } catch (NullPointerException e) {

            Toast.makeText(context,"No Internet Connectivity found.Please connect to the internet first and then retry.",Toast.LENGTH_LONG).show();
        }
        }

But my code is not working properly. Can anyone please help me with this?

  • 写回答

1条回答 默认 最新

  • doujing1858 2017-03-10 13:08
    关注

    I solved the problem using the below PHP Script. I did not use alias for the SQL output which is done using "AS". The below script is working fine and gives the desired result.

    <?php
    require "conn.php";
    require "SalesLogin.php";
    
    $enquiry = $_POST["enquiry"];
    $retail = $_POST["retail"];
    $collection = $_POST["collection"];
    $booking = $_POST["booking"];
    $evaluation = $_POST["evaluation"];
    $test_drive = $_POST["test_drive"];
    $home_visit = $_POST["home_visit"];
    $user_name = $_POST["user_name"];
    $update_date = $_POST["date"];
    $absent = $_POST["absent"];
    
    
    $timezone = new DateTimeZone("Asia/Kolkata" );
    $date = new DateTime();
    $date->setTimezone($timezone );
    $time =  $date->format( 'H:i:s A' );
    
    
    $sql = "UPDATE employee_details SET
    enquiry_sum = (SELECT SUM(enquiry) +'$enquiry' FROM (SELECT * FROM employee_details WHERE date = CURDATE() AND name = '$user_name') AS x)
    WHERE date = CURDATE() AND name = '$user_name'";
    $res = $conn->query($sql);
    
    $check = "UPDATE employee_details SET enquiry_target_status = ( SELECT IF (MAX(enquiry_sum) = 52, 'ACHIEVED', 'NOT ACHIEVED') FROM (SELECT * from employee_details WHERE date = CURDATE() AND name = '$user_name') AS Y ) WHERE date = CURDATE() AND name = '$user_name'";
    $insert_status = $conn->query($check);
    
    
    $miss_count = "UPDATE employee_details SET enquiry_target_missed_by = (SELECT (50 - MAX(enquiry_sum)) FROM (SELECT * from employee_details WHERE date = CURDATE() AND name = '$user_name') AS Z ) WHERE date = CURDATE() AND name = '$user_name'";
    $insert_status = $conn->query($miss_count);
    
    
    $mysql_qry1 = "INSERT INTO employee_details(enquiry,retail, 
    collection,booking, evaluation, test_drive, home_visit, name, date,time,absent) values ('$enquiry','$retail','$collection','$booking','$evaluation','$test_drive',
    '$home_visit','$user_name','$update_date','$time','$absent');";
    
    if($conn->query($mysql_qry1) === TRUE) 
        echo "Your details has been successfully inserted.";
    
    else 
        echo "Error: " .$mysql_qry1. "<br>" . $conn->error;
    
    
    if($update_date != $date){
    $mysql_qry2 = "UPDATE employee_data SET last_updated_date = (DATE_ADD('$update_date', INTERVAL 1 DAY)) WHERE name = '$user_name';";
    $conn->query($mysql_qry2);
    echo "Date changed," .$mysql_qry2;
    } 
    
    
    $mysql_qry3 = "SELECT COUNT(*) AS count from employee_details WHERE date = '$update_date' and name LIKE '$user_name';";
    $result1 = mysqli_query($conn,$mysql_qry3);
    
                           $row = mysqli_fetch_assoc($result1);
                           $count = $row['count'];
    
                           echo "Count: " .$count; 
          if($count <= 2) 
                  {
                       echo "You can login.";
                  } 
             else 
                  {
                      echo "You cannot login anymore for today.";
                 }
    
    
    $conn->close();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器