Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 580869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:31:08+00:00 2026-05-13T14:31:08+00:00

Okay… So, I came across the following problem: I have one table that works

  • 0

Okay… So, I came across the following problem: I have one table that works properly – it holds users’ IDs, posts’ IDs, posts’ content, time that post was made.

I use it to have the following printed out: the content of the post, who is the author of the post and when the post was made. So far everything works just fine.

Now… I have a button that allows the users to vote up for a given post – everything is working just fine here as well. However, now I want to make one more feature that will show who and when has voted. You know, so that beneath each post there will be a list of people who have liked the post and when they’ve pressed the button.

My idea was to have two tables – the one holding the post’s info post_table, and one more table voted_table that will have the user IDs, the time the button was pressed and the post_id that the vote has been made for… But I have no idea how to actually make it.
So pretty much my question is:

How can I insert into a table information from another table + some other information on a button’s click?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:31:08+00:00Added an answer on May 13, 2026 at 2:31 pm
    CREATE TABLE `user` (
      `id` INTEGER UNSIGNED NOT NULL auto_increment,
      `username` varchar(32) default NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB;
    
    CREATE TABLE `post` (
      `id` INTEGER UNSIGNED NOT NULL auto_increment,
      `owner_id` INTEGER UNSIGNED default NULL,
      `content` TEXT default NULL,
     PRIMARY KEY (`id`),
     KEY `post_I_owner` (`owner_id`),
     CONSTRAINT `post_FK_user`
       FOREIGN KEY (`owner_id`)
       REFERENCES `user` (`id`)
       ON DELETE SET NULL
    ) ENGINE=InnoDB;
    
    CREATE TABLE `vote` (
      `id` INTEGER UNSIGNED NOT NULL auto_increment,
      `user_id` INTEGER UNSIGNED default NULL,
      `post_id` INTEGER UNSIGNED,
      `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
       PRIMARY KEY (`id`),
       UNIQUE KEY `vote_IU_user_post` (`user_id`,`post_id`),
       CONSTRAINT `vote_FK_user`
         FOREIGN KEY (`user_id`)
         REFERENCES  `user` (`id`)
         ON DELETE SET NULL,
       CONSTRAINT `vote_FK_post`
         FOREIGN KEY (`post_id`)
         REFERENCES  `post` (`id`)
         ON DELETE CASCADE
    ) ENGINE=InnoDB;
    

    So with the schema above we can do the following…

    in your html/php for the viewPost page you could have

    <form name="vote_form" action="postVote.php" method="POST">
      <input type="hidden" name="post_id" value="1" />
      <input type="submit" value="Vote for this Post" />
    </form>
    

    So then in votePost.php you could do something like:

    if(isset($_SESSION['user']['user_id']))
    {
       $userid = $_SESSION['user']['user_id'];
    }
    else
    {
      // redirect to a login or something
    }
    
    $sql = "INSERT INTO vote (user_id, post_id) VALUES (%s,%s)"
    $sql = sprintf($sql, $userid, $_POST['post_id']);
    mysql_query($sql);
    

    This way mysql will take care of the timestamp for you. Note that you need to verify/validate the input data (which i didnt really do here) before inserting it into the db and all that fun security stuff. Id also recommend using Mysqli or PDO Mysql instead of the old mysql libararies and using a prepared stament (that will take care of most of your value quouting/escaping functionality on its own)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, next PHPExcel question. I have an HTML form that users fill out and
okay, i'm setting up a multi-user chat system. i have a messages table, that
Okay so what I have is a table that keeps track of history on
Okay, I have the following create action #posts_controller, nested resource under discussions def create
Okay, so I have the weirdest problem right now. My code is fine! It
Okay I have created an application where in one of the screens I have
Okay, I have hundreds of .net controls with text attributes that needs to be
Okay, so I have a chat feature on my website that I've mentioned in
Okay, here's the scenario. I have a utility that processes tons of records, and
Okay, we know that the following two lines are equivalent - (0 == i)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.