0

Here, I am trying to update my data in SQL. Therefore, I have used 000webhost to update and fetch my data from mysql using php. I try to update and fetch data, but it always returning 'uploadAttendanceStatus() data: {"UCEC106":"1"}'. I want an output like this 'uploadAttendanceStatus() data: {"UCEC106":"15"}'.

This is the code which is use in flutter inside a Button Widget.

try {
        final response = await http.post(_url, body: {
          'roll_number': '1701305',
          'course_code': 'UCEC106',
          'dept': 'civil,
          'attendance': '15',
          'date': 'Jan 15',
        });

        if (response.statusCode == 200) {
          var data = response.body;
          print('uploadAttendanceStatus() data: $data');
        } else {
          print('uploadAttendanceStatus.php: ${response.statusCode}');
        }
      } catch (e) {
        print('Catch uploadAttendanceStatus.php: $e');
      }

This is the table, where I want to update.

______________________
roll_number | UCEC106 |
______________________
  1701305   |    0    |
______________________

This is the php code.

<?php

include "config.php";

$rollNumber = $_POST['roll_number'];
$courseCode = $_POST['course_code'];
$dept = $_POST['dept'];
$attendance = $_POST['attendance'];//I am not aware of php, but i think the problem is here....
$date = $_POST['date'];


if($dept!="" && $rollNumber!="" && $courseCode!="" && $attendance=!"" && $date!=""){
    if($dept=="civil"){
         $updateRequest = "UPDATE today_civil_attendance_sheet SET $courseCode=$attendance, date='$date' 
          WHERE roll_number=$rollNumber";
          $con->query($updateRequest);
       
              $getRequest = "SELECT $courseCode FROM today_civil_attendance_sheet WHERE 
              roll_number=$rollNumber";
              $result = $con->query($getRequest);
                  echo json_encode($result->fetch_assoc());       
 }else{
      //another dept..................................
 }   
}
?>

This is the output: Expected Output: uploadAttendanceStatus() data: {"UCEC106":"15"} Actual Output: uploadAttendanceStatus() data: {"UCEC106":"1"}

Also is there anyway adding that field, like adding 0+5=5. What is the query for that?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Arud
  • 131
  • 8
  • use print_r($_POST); before "if($dept!="" && $rollNumber..." to see what you're getting – Ricardinho Jan 20 '21 at 05:35
  • Using this, I was only able see what i post and this is the output .....( I/flutter (15034): [roll_number] => 1701035 I/flutter (15034): [course_code] => UCEC106 I/flutter (15034): [dept] => civil I/flutter (15034): [date] => January 20 I/flutter (15034): [attendance] => 2 I/flutter (15034): ) I/flutter (15034): {"UCEC106":"1"} – Arud Jan 20 '21 at 06:21
  • @Dharman You are right but I don't know how to that. – Arud Jan 21 '21 at 12:55
  • You can learn here. https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-statement – Dharman Jan 21 '21 at 12:55
  • If you are only starting to learn PHP then you should learn PDO instead of mysqli. PDO is much easier and more suitable for beginners. Start here https://phpdelusions.net/pdo – Dharman Jan 21 '21 at 12:55

1 Answers1

0

I have solved my problem. Actually, My problem was with the if statement

if($dept!="" && $rollNumber!="" && $courseCode!="" && $attendance=!"" && $date!="")

I have used $attendance=!"" instead of $attendance!="".

Arud
  • 131
  • 8