如何从MYSQL数据库中检索数据并在Android上点击按钮

时间:2017-03-10 10:15:19

标签: php android mysql

我有一个Android应用程序,用于为每个User插入用户记录。每个用户每天只能将数据输入数据库2次。如果用户第三次尝试输入数据(单击“提交”按钮),将显示一个带有消息的新活动(在Android中),并且日期字段(TextView)应该增加1天。我能够添加每次点击,日期字段为1天。所以我试过的是在特定日期获取每个用户的MYSQL数据库中的数据计数,如果计数大于3,则PHP脚本将返回一个echo语句,根据该语句,Anroid中的异步任务将功能并显示新的活动。

我的PHP脚本是:

   <?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();
?>

我的AsyncTask代码是:

@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();
        }
        }

但是我的代码工作不正常。有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

我使用下面的PHP脚本解决了这个问题。我没有为使用“AS”完成的SQL输出使用别名。下面的脚本工作正常,并提供了所需的结果。

<?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();
?>