0

I'm getting this error, can't get rid of it. Google won't help me.
I don't really understand, what is really wrong here: "Trying to get property 'num_rows' of non-object" The thing is I want to send value as company_name not company_id. And if I change company_id to company_name, then textarea gives me that error. (i want to change to company_name, coz to save value to db) Maybe someone can explain me. I'm new in this thing.

while ($row = $result->fetch_assoc()) {
                      echo '<option value="' . $row['company_id'] . '">' . $row['company_name'] . '</option>';
                    }


( ! ) Notice: Trying to get property 'num_rows' of non-object in C:\wamp64\www\Baigiamasis\ajaxData.php on line 12 Call Stack #TimeMemoryFunctionLocation 10.0004407600{main}( )...\ajaxData.php:0

This is ajaxData.php file:

    <?php 
// Include the database config file 
include_once 'dbConfig.php'; 

if(!empty($_POST["company_id"])){ 
    // Fetch state data based on the specific country 
    $query = "SELECT * FROM receiver WHERE company_id = ".$_POST['company_id']." AND status = 1 ORDER BY company_name ASC"; 
    $result = $db->query($query); 

    // Generate HTML of state options list 

    if($result->num_rows > 0){ 
        echo '<option value="">Select</option>'; 
        while($row = $result->fetch_assoc()){  
            echo $row['company_address'];
            // echo ''.$row['company_address'].''; 
        } 
    } 
}
?>

This is the ajax script:

<script>
  $(document).ready(function() {
    $('#companyName').on('change', function() {
      var companyID = "";
      companyID = $(this).val();
      if (companyID) {
        $.ajax({
          type: 'POST',
          url: 'ajaxData.php',
          data: 'company_id=' + companyID,
          success: function(html) {
            $('#address').html(html);
            $('#city').html('<option value="">Select first</option>');
          }
        });
      } else {
        $('#address').html('<option value="">Select first</option>');
        $('#city').html('<option value="">Select first</option>');
      }
    });
  });
</script>

And the dropdown part:

           <?php
           include_once 'dbConfig.php';
           $query = "SELECT * FROM receiver WHERE status = 1 ORDER BY company_name ASC";
           $result = $db->query($query);
           ?>                
        <select type="text" class="form-control" name="companyName" id="companyName" autocomplete="off" >

          <option value="">Select Receiver</option>
          <?php
          if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
              echo '<option value="' . $row['company_name'] . '">' . $row['company_name'] . '</option>';
            }
          } else {
            echo '<option value="">Company not available</option>';
          }
          ?>
        </select>
        <textarea class="form-control" rows="3" name="address" id="address" placeholder="Address"></textarea>
Dip Hasan
  • 225
  • 3
  • 9
FPVMVP
  • 1
  • You seem to be using the company _name_ when trying to look up by _id_. Assuming that `$row['company_name']` is a text string and not a number, that's not going to work. Perhaps instead of `value="' . $row['company_name'] . '"`, you actually want `value="' . $row['company_id'] . '"`? – Patrick Q Jan 08 '20 at 19:24
  • @PatrickQ, but then it saves id value to DB, but i want to save the name instead. I want to change from company_id to company_name. – FPVMVP Jan 08 '20 at 20:40
  • Nothing in the code you've shown above "saves" anything, so I can't really comment on that. My comment addresses the error that you mentioned in your question. When you do `WHERE company_id = ".$_POST['company_id']."` you are injecting the `company_id` value posted from the ajax, which in turn comes from `'company_id=' + companyID`. When you do `companyID = $(this).val();` you are referencing the value of the `#companyName` element. So you can't expect to end up with an ID in the SELECT query if you are putting a name in the option element. – Patrick Q Jan 08 '20 at 20:46
  • You might be interested in [data-* attributes](https://api.jquery.com/data/#data-html5) – Patrick Q Jan 08 '20 at 20:50
  • @PatrickQ Thanks for your explanation! ehhh okay, got it. But still i want to have option value as company_name, not an id. dunno how to rewrite the code to make it work, as i want. – FPVMVP Jan 09 '20 at 00:16

0 Answers0