-3

I would like to populate a drop down list of moods and save it to my database with a time stamp. And at the same time connect the moods with the current user. How should I approach this?

In my database, I have a table called "checkin" which includes: ID, Time, FKMoods (foreign key to list of moods) and FKUsers(foreign key to users).

Right now I have a form called checkin in "page.php":

<form id="checkin" action="checkin/checkin.php" method="POST">
<label for="checkinMood">Select your current mood :</label>
<select id="checkinMood" class="SelectMood"></select><br />
<input type="submit" id="checkin-button" value="Update!" />
needhelp
  • 35
  • 5
  • Where are you stuck? What's the problem you're facing? – gen_Eric Dec 06 '12 at 16:13
  • 4
    Your best bet is 1) Create an HTML form with a PHP action - just like you've done, 2) your PHP action (checkin/checkin.php) will 3) read the POST variables and 4) update the database. – paulsm4 Dec 06 '12 at 16:13
  • 1
    [What have you tried?](http://whathaveyoutried.com) – jacoz Dec 06 '12 at 16:15
  • @needhelp: What ways have you tried? – gen_Eric Dec 06 '12 at 16:16
  • I've tried this tutorial: http://www.aleixcortadellas.com/main/2009/02/13/277/ but i would also like a time stamp on that – needhelp Dec 06 '12 at 16:18
  • @needhelp: Use MySQL's `NOW()` or PHP's `date()` to get a timestamp. – gen_Eric Dec 06 '12 at 16:20
  • This question shows no research effort. You are essentially asking someone to write all the code for you. As desperate as people are for internet points, I don't think anyone will do this for you. Your best bet is to check out http://php.net/manual/en/ and start from there. – Kevin Brydon Dec 06 '12 at 16:22
  • I'm asking how to approach it not the code =__= – needhelp Dec 06 '12 at 16:23

3 Answers3

0

Steps to accomplish this

  1. Select the moods from a moods table
  2. Loop through the results and put them into a select input
  3. Upon submitting, update the given mood into the user table
Community
  • 1
  • 1
twodayslate
  • 2,803
  • 3
  • 27
  • 43
0

Don't save the html option list to the database. It's too hard to edit when you want to change the options. Store a list of moods like this:

CREATE table moods
mood_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
mood VARCHAR(45);

To make the option list, query the mood table and loop through the results creating options as you go:

<option value='{$mood["mood_id"}'>{$mood["mood"]}</option>

To store the user's mood, update the users table with the mood_id.

dnagirl
  • 20,196
  • 13
  • 80
  • 123
0

Do a joined sql query to pull the foreign keyed values/rows from other tables first - assuming you are using mysql - using very basic functions everyone knows (without going into whether you are using mysqli or any pdo or lib) :

SELECT * FROM checkin c LEFT JOIN listofmoods lm ON c.FKMoods = lm.FKMoods LEFT JOIN listofusers lu ON c.FKUsers = lu.FKUsers ORDER BY c.ID ASC

(or however you want to sort)

Now you got all the fields you need. then run a mysql fetch to create the list, something like :

$query=mysql_query(-thequery listed above-);

while($fetch=mysql_fetch_assoc($query))
{

   $moodselect.='<option value="'.$fetch['FKMoods'].'">'.$fetch['whateverfieldlabelthemoodnameyouhaveonmoodstable'].'</option>';


}

this will pop you a $moodselect with the necessary options being there. you can then shove this in to the select you have in your example.

you would probably have to put in the user id in some hidden field in your form. or if your user system recognizes the user automatically from session you can just use it when the form is posted to checkin.php.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
unity100
  • 802
  • 10
  • 17