1
    $ID = $_GET['ID'];
      $results = $mysqli->query("SELECT * FROM PresidentialCandidate WHERE ID='$ID'");   
      if( $results->num_rows > 0 )
      {
      $row = mysqli_fetch_array($results,MYSQLI_ASSOC);

       //Instead of just echoing out the ID, you need to build the result/data that you want in the div right here. The success function of the AJAX call will append whatever you echo out here
echo $row['ID'];
      }

No idea what's wrong. This is a question based on the solutions posted on my original question: Using a href onclick to update div without reloading page? How do I define the ID variable?

Community
  • 1
  • 1
thomas
  • 163
  • 2
  • 13
  • 2
    Either `$_GET['ID'];` or `$row['ID'];` don't exist. – TbWill4321 Dec 16 '15 at 17:53
  • what do you mean "don't exist"? how do I define them and make them "exist"? – thomas Dec 16 '15 at 17:58
  • To find out what columns are returned from your SQL query, you can execute a simple loop: ` $len = mysqli_num_fields($this->resultset); $i = 0; for ($i=0; $i < $len; $i++) { $fieldArray[$i] = mysqli_fetch_field_direct($this->resultset, $i)->name;}` – Bob Dill Dec 16 '15 at 17:59
  • 2
    Try encapsulating this block with `if (isset ($_GET['id']))`. – razz Dec 16 '15 at 18:01
  • Check that your GET global contains ID, do `var_dump($_GET)` you should then see all variables which it contains. If your ID is in there, then loop through each of your rows returned, remember mysqli is fetching an array, accessing the key `$row["ID"]` is not correct syntax, you need to access a subscript first, for example `$row[0]["ID"]` – Halfpint Dec 16 '15 at 18:03
  • To just get the column data for each row returned in your result set, you can use a loop similar to the following (which creates an xml formatted stream): ` while ($row = mysqli_fetch_array($this->resultset, MYSQL_ASSOC)) { $assocArray = $assocArray.""; $i = 0; foreach ($row as $cell) {$assocArray = $assocArray."<".$fieldArray[$i].">".$cell."".$fieldArray[$i].">"; $i++; } $assocArray = $assocArray.""; }` – Bob Dill Dec 16 '15 at 18:04
  • changed it to `if(isset($_GET['id']))` and changed the variable to id. didn't get any errors after that as far as I can see. – thomas Dec 16 '15 at 18:11
  • I've updated my answer. **It's Working** Please have a look @thomas – Nana Partykar Dec 16 '15 at 19:09

2 Answers2

1

You code can cause 2 different problems. But you have named your variables identically.

1.) Check if your GET variable is set otherwise initialize them with a default value. But in your case its better to check if its set before and don't execute your query if its not set.

$ID = (int)$_GET['ID'];
if(isset($_GET['ID']) {
  $results = $mysqli->query("SELECT * FROM PresidentialCandidate WHERE ID='$ID'");   
  if( $results->num_rows > 0 ) {
      $row = mysqli_fetch_array($results,MYSQLI_ASSOC);

      echo $row['ID'];
  }
}

2.) If you don't have an exact field in your database that is called ID then you have the next error. You should check them before.

And the last thing is that you use mysqli in a wrong and very insecure way. You pipe your GET variable directly to your query without using a prepared statement or check if its an int.

https://secure.php.net/manual/en/mysqli.prepare.php

You should really take a look at that point.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
1

You need to set attribute like data-attrb (Not necessary with same name) in <a></a> and use this attribute in <script></script>. You can pass your value in data-attrb which will get store in var ID=$(this).attr('data-attrb'); in <script></script>. Declare one class name for <a></a>, use this class name to call that load() function. I've checked this code, It's working.

<a href="#" class="asd" data-attrb='1277'> Two   </a>
<a href="#" class="asd" data-attrb='1235'> Three </a>

<div id='myStyle'></div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){

    $(".asd").click(function(){
        var ID=$(this).attr('data-attrb');
        $("#myStyle").load("templatecode.php?id="+ID);
    });

});
</script>

templatecode.php (This page name is used in <script></script> tag too, so if you are changing page name here. Please change page name there too.)

<?php 
$ID = $_GET['id'];
$results = $mysqli->query("SELECT * FROM PresidentialCandidate WHERE ID='$ID'");   
if( $results->num_rows > 0 )
{
    $row = mysqli_fetch_array($results,MYSQLI_ASSOC);

    //Instead of just echoing out the ID, you need to build the result/data that you want in the div right here. The success function of the AJAX call will append whatever you echo out here
    echo $row['ID'];
}
?>
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77