I have two tables, Post and Category.
Post
pid title result content cid
1 Option1 Opt content1 2
2 Option2 Opt content2 2
3 Option3 Opt content3 3
Category
cid cname
1 Cat 1
2 Cat 2
3 Cat 3
This is my insert.php .
mysql_connect('localhost','root','');
mysql_select_db("database") or die("Unable to select database");
$title=$_POST['post_title'];
$result=$_POST['post_result'];
$content=$_POST['post_content'];
$sid=$_POST['cid'];
$date=$_POST['date'];
$insertquery="INSERT INTO review (post_title,post_result,post_content,cid,date)
VALUES('$title','$result','$content',$sid,NOW())";
$result=mysql_query($insertquery);
if(! $result )
{
die('Could not enter data: ' . mysql_error());
}
else {
echo "Entered data successfully\n";
header('Location:home.php');
}
This is my cat.php which displays posts of particular category.
$link = mysql_connect("localhost","root","");
mysql_select_db("database");
$query="SELECT * FROM category WHERE cid='$cid'";
$result = mysql_query($query) or die("Query to get data failed with error: ".mysql_error());
while($row = mysql_num_rows($result)) {
echo //details goes here
}
This is my category menu list on home page .
<ul>
<li><a class="my-button" href="#">Cat 1</a></li>
<li><a class="my-button" href="#">Cat 2</a></li>
<li><a class="my-button" href="#">Cat 3</a></li>
</ul>
This is javascript.
<script>
$(document).ready(function() {
$('body').on('click', '.my-button', function() {
$("#display").load("cat.php");
});
});
</script>
Everything is getting inserted to database correctly . But im not able to display the posts of specific category onclick.
I have given the complete code. What I want is, when I click on Cat 1 or Cat 2 or Cat 3 on home page I need to display all data from post table of the selected category in the #display div.
I am getting an error saying:
cid is undefined variable
Please help me find the proper code.
that is because looking at your script
$cid, which is used in your query is not defined anywhere. Kudos for at least ahving error reporting on. Next time you really should read and understand the error messages you get, as this oftentimes tells you exactly what the problem is.Also, you really need to look at learning mysqli or PDO. The
mysql_*are depreacted and should no longer be used. You also have sever SQL injection vulnerability, so you should read up on what to do about that.