0

Hi I already asked this question but unfortunately I didn't get appropriate answer for that. I have two tables in MySQL database called Items and Item as bellow:

enter image description here

I have two .php files as bwlow; the index.php and results.php the index.php is like:

 <html>
 <head></head>
 <body>
 <?php
   $mysqli = new mysqli('localhost', 'root', '', 'moviedb');
    if ($mysqli->connect_errno) 
     {
      die('Unable to connect!');
     }
    else{
         $query = 'SELECT * FROM tblItems';
         if ($result = $mysqli->query($query)) {
            if ($result->num_rows > 0) {
   ?>  
    <p> Select a Genre
       <ul>
    <?php     
       while($row = $result->fetch_assoc()){
     ?>      
  <li><div class="selectGenre"><?php echo $row['item']; ?></div></li>     
  <?php           
    }
   ?>
        </ul>
</p>
<p id="result"></p>
<?php
}
else 
{
    echo 'No records found!';
}
$result->close();
}
else 
{
echo 'Error in query: $query. '.$mysqli->error;
 }
}
$mysqli->close();
?>
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"           type="text/javascript"></script>
 <script type="text/javascript">
    $(document).ready(function()
    {
        $('.selectGenre').click(function()
        {
          if($(this).html() == '') return;
            $.get(
                'results.php',
                { id : $(this).html() },
                function(data)
                {
                    $('#result').html(data);
                }
            );
        });
    });
   </script>
  </body>
  </html>

and the results.php is:

<?php
  $mysqli = new mysqli('localhost', 'root', '', 'moviedb');
  $resultStr = '';
   $query = 'SELECT type FROM tblItem where id='.$_GET['id'];
   if ($result = $mysqli->query($query)) 
   {
    if ($result->num_rows > 0) 
   {
    $resultStr.='<ul>';
     while($row = $result->fetch_assoc())
     {
    $resultStr.= '<li><strong>'.$row['id'].'</strong> - '.$row['type'];
   '</li>';
  }
   $resultStr.= '</ul>';
  }
  else
  {
$resultStr = 'Nothing found';
  }
 }
echo $resultStr;
?>

well, the first part(index.php) is populating the list base on the tblItems table but clicking on the list not returning any values to the page from the results.php file, not even an error message. Can you please let me know what I am doing wrong here?

1
  • how do you know nothing is returned... from inspecting request in console? Do you know if an ajax request is being made? There is no click handler code or ajax code shown. Would help a lot to narrow down if the problem is server code or javascript related . Provide as much detail as you can Commented Nov 14, 2012 at 2:53

3 Answers 3

1

this will be easier for you: Try this one, i edit your index.php code:

 <html>
 <head></head>
 <body>
 <?php
   $mysqli = new mysqli('localhost', 'root', '', 'moviedb');
    if ($mysqli->connect_errno) 
     {
      die('Unable to connect!');
     }
    else{
         $query = 'SELECT * FROM tblItems';
         if ($result = $mysqli->query($query)) {
            if ($result->num_rows > 0) {
   ?>  
    <p> Select a Genre
       <ul>
    <?php     
       while($row = $result->fetch_assoc()){
     ?>      
  <li><div class="selectGenre" onclick="return printSearch('<?php echo $row['id']; ?>');"><?php echo $row['item']; ?></div></li>     
  <?php           
    }
   ?>
        </ul>
</p>
<p id="result"></p>
<?php
}
else 
{
    echo 'No records found!';
}
$result->close();
}
else 
{
echo 'Error in query: $query. '.$mysqli->error;
 }
}
$mysqli->close();
?>
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"           type="text/javascript"></script>
 <script type="text/javascript">
    function printSearch(idVal)
    {

            $.get(
                'results.php',
                { id : idVal },
                function(data)
                {
                    $('#result').html(data);
                }
            );
    }
   </script>
  </body>
  </html>

Ok here is for result.php

<?php

$mysqli = new mysqli('localhost', 'root', '', 'moviedb');
$resultStr = '';
 $query = 'SELECT * FROM tblItem where id='.$_GET['id'];
if ($result = $mysqli->query($query)) 
{
    if ($result->num_rows > 0) 
    {
        $resultStr.='<ul>';
        while($row = $result->fetch_assoc())
        {
            $resultStr.= '<li><strong>'.$row['id'].'</strong> - '.$row['Name'].
                            '</li>';
        }
        $resultStr.= '</ul>';
    }
    else
    {
        $resultStr = 'Nothing found';
    }
 }
echo $resultStr;
?>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Sephoy, I just copy and paste your code and again I m not getting any thing on the page!
@user1760110: just copy this in both files
Thanks Sephoy08 I just test them and they work fine.just wondering why we are not able to use the jQuery document ready method in your example?
@user1760110: its not that we can't use it... i just made it easier, but if you want you can play with it and use your preferred method.
0

Your problem is:

$query = 'SELECT type FROM tblItem where id='.$_GET['id'];

when

$_GET['id'] = 'Drama'; // Action, etc.

2 Comments

Hi KeyboardSmasher, Thanks for ur comment I update the select clause to $query = 'SELECT name FROM tblItem where id='.$_GET['id']; but i did,nt get your point by $_GET['id'] = 'Drama'; // Action, etc. can you please let me know more what is happening? thanks
@user1760110: KeyboardSmasher means that, in your tblItem, id field is integer and not varchar, so watever you select, you dont have id that is string!
0

Try the below query in results.php

$sql  = "SELECT tblItem.* FROM tblItems 
  INNER JOIN tblItem USING(id)
  WHERE tblItems.item = '".$mysqli->real_escape_string($_GET['id'])."'";

Also change $row['type'] to $row['Name'].

1 Comment

Hi air4x, I update the result.php code as ` $query = "SELECT tblItem.* FROM tblItems INNER JOIN tblItem USING(id) WHERE tblItems.item = '".$mysqli->real_escape_string($_GET['id'])."'"; if ($result = $mysqli->query($query)) { if ($result->num_rows > 0) { $resultStr.='<ul>'; while($row = $result->fetch_assoc()) { $resultStr.= '<li>'; $resultStr.= '<div><pre>'.$row['name'].'</pre></div>'; '</li>'; } $resultStr.= '</ul>'; } else { $resultStr = 'Nothing found'; } } echo $resultStr; ?>` but this is still doing nothing!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.