The problem here is , when i type cat='hotels' manually , it displays the detail of that category .
But when i give cat=$category , (category will be selected by user) , it says that category is undefined variable .
What i want is , when i click on any category , the posts related to that category should get displayed .
Do help to solve this .
Below is the code .
Here's the script .
<script>
$(document).ready(function(){
$(".button").click(function(){
$("#display").load("cat.php");
});
});
</script>
Here's my html.
<ul>
<li class="button"><a href="#">Hotels</a></li>
<li class="button"><a href="#">Resorts</a></li>
<li class="button"><a href="#">Hills</a></li>
</ul>
<div id="display"></div>
This is my insert.php file.
$id = isset($_POST['post_id'])?$_POST['post_id']:'';
$category=$_POST['cat'];
mysql_connect('localhost','root','');
mysql_select_db("database") or die("Unable to select database");
$insertquery="INSERT INTO posts VALUES('$id','$category')";
$result=mysql_query($insertquery);
if(! $result )
{
die('Could not enter data: ' . mysql_error());
}
else {
echo "Entered data successfully\n";
header('Location:home.php');
}
mysql_close();
?>
This is the code (cat.php) to display it on my home page.
<?php
$row="";
$link = mysql_connect("localhost","root","");
mysql_select_db("database");
$query = "SELECT * FROM posts WHERE cat=$category";
$result = mysql_query($query) or die("Query to get data failed with error: ".mysql_error());
while($row = mysql_fetch_array($result)) {
echo $row['cat'];
}
mysql_close($link);
?>
Change
$query = "SELECT * FROM posts WHERE cat=$category";
To
$query = "SELECT * FROM posts WHERE cat='$category'";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.