0

I'm having a trouble with AJAX results. I've got results from the query from MySQL, but AJAX responded differently. I have equated the query both MySQL and AJAX. The query will only show 1 row or not at all. Could you show me where the wrong comes?

This is the query

SELECT a.id_pendaftaran, l.nama_layanan, p2.asal_perusahaan, a.status from
 antrian a JOIN pengunjung p2 ON a.id_pendaftaran = p2.id_pengunjung JOIN 
layanan l ON a.id_layanan = l.id_layanan WHERE a.id_pendaftaran = 'gmFhJd2t0N'

And then this is the AJAX

   $('.cekbutton').click(function(e){
        var id = $('#id_pendaftaran').val();
            e.preventDefault(); 
            $.ajax({
                url:"ajax.php",
                type:"GET",
                data : "id="+id,
                success : function(result) {
                    console.log(result);
                    var datanew = JSON.parse(result);
                    $('#layanan').val(datanew.layanan);
                    $('#nama_perusahaan').val(datanew.nama);
                    $('#status').val(datanew.status);
                    $('#layanan').prop('disabled', true);
                    $('#nama_perusahaan').prop('disabled', true);
                    $('#status').prop('disabled', true);
                }
            });
        });
<?php

    include "../connection.php";

    $q = mysqli_real_escape_string($koneksi, $_GET['id']);

    $query = mysqli_query($koneksi, "SELECT a.id_pendaftaran, l.nama_layanan,
 p2.asal_perusahaan, a.status from antrian a JOIN pengunjung p2 
ON a.id_pendaftaran = p2.id_pengunjung JOIN layanan l ON a.id_layanan = l.id_layanan 
WHERE a.id_pendaftaran = '".$q."'");
    if (mysqli_num_rows($query) == 1) {
        $row = mysqli_fetch_array($query);

        $nama = $row['asal_perusahaan'];
        $layanan = $row['nama_layanan'];
        $status = $row['status'];

        $myObj = array('hasil' => true, 'nama' => $nama, 'layanan' => $layanan, 'status' => $status);
    }
    else {
        $myObj = array('hasil' => false, 'nama' => "-", 'layanan' => "Belum terdaftar", 'status' => "-");
    }

    $myJSON = json_encode($myObj);
    echo $myJSON;
?>

Result from MySQL

id_pendaftaran | nama_layanan  | asal_perusahaan | status |
gmFhJd2t0N     | Certification | ABC             | Waiting |

and this from the browser console

{"hasil":false,"nama":"-","layanan":"Belum terdaftar","status":"-"}

If I changed this equation (mysqli_num_rows($query) == 1) other than this, the result would be:

{"hasil":true,"nama":null,"layanan":null,"status":null}
  • 2
    **Warning:** You are wide open to [SQL Injections](http://php.net/manual/en/security.database.sql-injection.php) and should really use parameterized [Prepared Statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead of manually building your queries like that. Specially since you're not escaping the user inputs at all! – Dharman Jan 13 '19 at 17:53
  • Check your mysqli statements for errors. Even better enable exceptions. [How to enable MySQLi exception mode?](https://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli) – Dharman Jan 13 '19 at 17:56
  • thanks for your concern, but I'm aware with it – danielraysa Jan 13 '19 at 17:59
  • ok let me try the exception – danielraysa Jan 13 '19 at 18:00

1 Answers1

0

Maybe your query returns more than one row? if (mysqli_num_rows($query) == 1)

empy26
  • 81
  • 5