By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,695 Members | 1,170 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,695 IT Pros & Developers. It's quick & easy.

How to input content for particular topic to database in php?

P: 36
I have recent topics displayed on my home page . When i click on any topic , it opens in a new page with full details of that topic . There is an option( a form ) where users can write review for that particular topic .

What i am looking for is , how do i insert data for only that topic , like , on my home page , in recent topics section , i have to display number of reviews of that topic only .

What i thought is , to take that topic's id and insert . But i do not know how to do that .

This is my 1st table for Topics.
Expand|Select|Wrap|Line Numbers
  1. Topic_Id  -  1 , 2 , 3  
  2. Title  -  First Topic , Second Topic , Third Topic 
  3. Category_ID  -   3 , 1 , 1  
  4. Date  -   yyyy-mm-dd
  5.  
This is my 2nd table for Category.
Expand|Select|Wrap|Line Numbers
  1. Category_ID  -  1 , 2 , 3
  2. Category_name   -  PHP , MYSQL , PDO
  3.  
This is to input .
Expand|Select|Wrap|Line Numbers
  1. try 
  2.    {
  3.    $query = $conn->prepare("INSERT INTO `topics`     (title,category_id) VALUES  (:title,:category_id)");
  4.  
  5.    $query->execute(array(
  6.         'title'       => $_POST['title'],
  7.                                     'category_id' => $_POST['category_id'],
  8.                                     )); 
  9.         }
  10.  
  11. catch (PDOException $e) 
  12.    {
  13.    error_log($e->getMessage());
  14.    die($e->getMessage());
  15.    }
  16.  
This is to display on home page .
Expand|Select|Wrap|Line Numbers
  1.    $sql = "SELECT * FROM review ORDER by pid DESC limit  0,3";
  2.  
  3.    $q    = $conn->query($sql);
  4.  
  5.    $q->setFetchMode(PDO::FETCH_ASSOC);
  6.  
  7.    while($row = $q->fetch()){
  8.       echo $row['title'];
  9.       echo $row['username'];
  10.    }
  11.  
This is my 3rd table for inserting reviews .
Expand|Select|Wrap|Line Numbers
  1. Topic_ID  -  
  2. Rating  -  
  3. Review  -  
  4. User_ID  -
  5.  
Problem 1 : How do i input topic-id of a particular topic when user fills and submits form into 3rd table ?
Problem 2 : And is there a need of user-id in 1st table because there is also an option for user to insert topics ?
Problem 3 : How do i select number of reviews for a particular topic to display next to it on home page ?
Jan 24 '13 #1
Share this Question
Share on Google+
3 Replies


Rabbit
Expert Mod 10K+
P: 12,427
We only allow one question per thread in this forum. Please split out questions 2 and 3 into a separate thread. I will respond to question 1 since that it the question in the title.

You will need to output the id to the page so you can pass that onto to page that will insert into the third table. You can use a hidden form element for that if you don't want to show the id on the page.
Jan 24 '13 #2

P: 36
In <input type="hidden" /> how do i get that id ?
Expand|Select|Wrap|Line Numbers
  1. <form action="ins.php" method="post">
  2.      <input type="hidden" ?? />
  3.      <input type="text" name="rating" />
  4.      <input type="text" name="review" />
  5.      <input name="submit" type="submit" value="Submit"/>
  6. </form>
  7.  
Jan 24 '13 #3

Rabbit
Expert Mod 10K+
P: 12,427
Expand|Select|Wrap|Line Numbers
  1. <input name="userid" type="hidden" value="use php to output the id" />
Jan 24 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.