reactivating a query after executing an insert statement in PHP/MYSQL - php

I have 2 forms. The first form the user would select a department (DEPQ) from a drop down menu list. Once that is done they would submit that form and the results would be displayed in a drop down menu in the second form. The user would then use this second form to insert data into a table in msql database using the derived values. What I am trying to do is once the system has completed the insertion of the data, the system automatically re populates the drop down menu with the results derived from first query. I know this can be simply achieved with multiple linked drop down menu's but I have been unable to find a suitable tutorial for this function. Any help you all can give me would be greatly appreciated.

Well you have to implement any opportunity to identify the user. Maybe a user system, maybe only a cookie/localstorage or something else. Important is, that the user can by identified with an unique id.
In the next step you create a mysql table where you save the first selection of the user may be in this format
id | user_id | department
-------------------------
1 | 3 | depA
2 | 15 | depB
Then when the user visits your page again, you identifie the user and populate the select element.

Related

Storing a list of songs linked to event mysql

I have a database in MySQL that currently lists approximately 1500 concerts and events. Now, the plan is to add setlists (list of the songs performed at the concerts) for all the concerts in the database. Basically this will mean a lot of repeated values (songs performed at many concerts), and I would really appriciate some input on what the best approach would be.
I initially started out with a database similar to this;
| eventID | edate | venue | city | setlist |
The field setlist was basically text data, where I could paste the list of songs and parse through it to put each song on a new line with php. This works, and editing the text and running order was like editing a text document. Now, obviously this was pretty simple, but has drawbacks and limitations. Simple things like getting stats on songs performed is probably very difficult, right?
So, what is the best way to store the setlist value?
Create a new table that adds a new row for each song performed, and that has a foreign key linking to eventID? How would I best retain (and edit, if needed) the running order of the songs in that table? Any other suggestions?
Thanks for any input or advice on this, as I would love to get some help before I start adding all the data.
I would create a table that holds each song performed at a specific event:
| songId | eventID | song |
Where eventID can be duplicated in multiple rows to show each song performed at that event.
This way you can query all the times a specific song was performed, and also get all songs (the setlist) for a specific event by querying on the eventID.

SQL boolean relationship

I have a table dislikes which contains two columns, idone and idtwo.
These are unique ids' from users, for example:
| idone | idtwo |
-----------------
| 5 | 4 |
This means that user with id=5 does not like user with id=4. What I have in PHP is an array containing the ids' of all the users that the current user has selected as not liking them.
So say dislikes={1,2,3}, this means that the current user does not like user 1,2, or 3. There is an unknown number of users in the database.
So if user 1 chooses to dislike user 2 and user 3 (this is done via HTML dropdown), I pass dislike={2,3} to a PHP page which processes this data.
I want the PHP page to then add entries (1,2) and (1,3). Here is the first problem, how can I make sure only to add unique entries?
Also say that user 1 changes the fact that he dislikes user 2. Then I pass dislike={3} to the php page and must somehow remove all entries (1,!3), i.e. all entries in which user 1 dislikes anyone except user 3. How can I achieve this? Or is there a better way?
Since you're using MySQL the easiest thing is probably to use REPLACE INTO instead of INSERT with a primary key or unique index on the pair of columns (idone, idtwo).
Alternatively, on update, you can run a transaction that does any one of:
Remove existing rows for this user, add all rows, commit
Select existing rows, remove the rows from your local set that you would duplicate, add only new rows, commit

Setting up a MySQL table to track user link clicks

This is a followup to a question I posted a few days ago.
basically, I have a site with six links. In order to access the site, users must log in using LDAP authentication. When they do this, I grab some of their account credentials (username, firstname, lastname), and store it in a PHP $_SESSION variable.
That works; the user can log in, and the session data is being stored successfully.
Now, I want to set up a way to track which links have been clicked by what users. Basically just store a time stamp in the database of when they clicked the link. I want to be able to see who has (or has not) clicked each link, and when.
Can I do this in a single table / would that be a bad idea? I was thinking setting up the table like this:
TABLE (each bullet indicative of a column)
auto-incrementing ID
user account name: abc1234
user account first name: John
link 1: Last Accessed 5/2/2012 at 4:15PM
link 2: NULL
link 3: NULL
link 4: Last Accessed 5/1/2012 at 2:20PM
link 5: NULL
link 6: NULL
basically the above would say that "John" had only clicked the first and 4th links. The rest are null because he has never accessed them. If he were to click #1 again, it would overwrite with the more recent date/time.
Can I do this in a single table? or will that create complications? I feel like the thing I will have the hardest time with is checking to see if the user is already in the database before adding the info (ie so that if John logs in a second time, a whole new row isn't created for him)
Thanks for any help!
That would be a bad idea. What if you wanted to have a seventh link? What if the user format would change?
This solution requires 3 tables:
Users - contains user data (And a user ID).
Links - contains link data (And a link ID).
Clicks - many-to-many relationship between users and links.
That third table would look like this:
user_id | link_id | timestamp
-----------------------------
1 | 2 | ...
2 | 2 | ...
1 | 3 | ...
............
why not just have
increment_ID
Account_ID
Link_URL
Timestamp
Then just insert a new record for each click. You also don't need to manage links since you'll store the entire URL path

Publish submitted form content directly to page

I have a very basic form consisting of only two dropdown boxes (though the contents of the second dropdown vary depending on the selection made in the first). I then have a bit of text that displays under the second dropdown box once a selection has been made, and what the text says is unique to whatever selection is made.
What I want to be able to do is have this information directly publish into a table that will be included at the bottom of the same page the form is located on, once a user hits the submit button. In other words, when a user submits the form, I don't want it to simply spit the user's info back to them; I want the info to actually publish to the page in a table, so that anyone who logs in can see it, and can add their own published info to the table as well.
You could think of it sort of like a blog commenting system that automatically publishes new content to the page every time a visitor submits the form, except, instead of text areas that allow users to insert whatever they want, I have dropdown boxes that only allow them to select a certain option, and then have that selected info publish to a table.
How would I go about doing this? I have a beginner's knowledge of PHP, and almost no knowledge of javascript. But depending on how detailed and helpful of an answer that may be provided, I could work with either.
This form is only accessible to members who are logged in, and I have 6 bits of info that I want to have published into a row of 6 columns in the table every time a user hits Submit. Those 6 things are: (1) Username, (2) Time Stamp (Date & Time at which the form was submitted), (3) Selection result of 1st Dropdown, (4) Selection result of 2nd Dropdown, (5) The unique text that is displayed according to whichever selection is made in the 2nd dropdown, and (6) a second time stamp which is calculated as 30 days beyond the time generated in the 2nd column.
Any help would be appreciated. I'm experimenting with PHP a bit but my knowledge is really not advanced enough yet to really allow me to progress much of anywhere on my own.
EDIT:
I did some playing around earlier and have made a few changes. First, I'm now wanting 1 dropdown box, not 2. Second, there are 9 columns in the table, not 6. Third, I have a database with the following 4 tables:
table: members
member_id
firstname
lastname
login
passwd
rank_id
table: ranks
id
name
table: jobs
id
name
requirement_1
requirement_2
requirement_3
salary
rank_id
table: workorders
date
username
rank
job
requirement_1
requirement_2
requirement_3
salary
due_date
Here is the current site I am working on: http://www.kiithsoban.com/membership
You may use a guest login to gain access:
Username: username
Password: password
This is a personal project made for an online gaming clan (in EVE Online), with all of maybe 20 unique visitors a month, so quality and proper coding technique is not much of an issue (not at this point anyway). The primary concern is simply that it will work.
The reason I suggested posting a PHP table rather than taking the javascript route is because I already have a table created in a database (and I'm also more familiar with PHP and just want to get the first version of this online ASAP), and I already have it posting to the website as well, as you'll see on the above page. This is the "workorders" table.
You'll notice upon logging in that there are instructions on the index page referring to a dropdown box, which does not yet exist. The dropdown box will consist of 49 "jobs" to select from. When a user selects an option and submits the form, I want their selection to insert into the "job" field of the workorders table on my database; and, I also want the site to detect the additional information in the other 8 fields in order to automatically insert them as well.
I've been experimenting with one idea, but I'm not sure if it would work, or how to do it exactly. In my members and jobs tables, I've included a "rank_id" field, in which each entry will have a matching value to the "id" field in the ranks table. Now, there are 49 "jobs" to choose from, however, depending on what rank a user is, their job salary will vary (given that there are 7 ranks, 49 * 7 = 343 total possible job/salary combinations that can result from the form submission). What I want the site to do is detect the logged in user's "rank_id" (in the members table), and after the user selects a job from the drop down, find the corresponding job in the jobs table that has the same "rank_id". That, I'm assuming, would allow the site to gather the "User", "Rank", "1st Requirement", "2nd Requirement", "3rd Requirement", and "Salary" info (all of which are contained in the members and jobs tables), and then insert that info into the appropriate fields in the workorders table, so that the info will then post to the site.
At that point all I would need to worry about are the "Date" and "Due By" columns - the "date" being automatically generated as the current date when the form is submitted, and the "due by" being automatically calculated as the current date plus 30 days.
I hope that's a more helpful explanation, and not too confusing.
You will need to create an OnSubmit event for the form which will return false inside the javascript function. The function will then need to add the content to the page. I would give more detailed code if you provided a layout.

Store dynamic form fields in database

I have read other answers on this (or at least near to this) subject but I couldn't get a clear view of it so I'm asking for help again.
I have a complex dynamic HTML form that I would like to submit to database using PHP. The form is split into multiple tabs and in each tab I got checkboxes that trigger other parts of the form. Example: at a point in my form I got a checkbox group that has options of: "hotel" and "restaurant". If I check hotels, I get another part of the form displayed, specific for "hotels". Same thing for "restaurant". So it's very dynamic here and I don't know which would be the best approach for storing every form field in database. Because it could contain 15 fields or 20, depending on the selection. Any example would be appreciated as I'm not that advanced with database design.
Thank you!
So it's very dynamic here and I don't
know which would be the best approach
for storing every form field in
database.
I apologise if I have misunderstood you here but I believe that you should design the database according to the data and not the form. It is difficult to comment without knowing the exact details of your situation so here is an example:
If you usually dump all the data from a form into a single table, but because sometimes this will involve submitting 5 values and other times this will involve submitting 10 and so you are unsure how many columns your table should have, then I think the problem is in the database design.
Work out what pieces of data are dependent on other pieces of data. For example, you mention checking "hotel" might open up more fields specific to that choice. Let's assume this involves things like "en-suite", "bed type" etc. Then you should have 3 tables, a registration table (assuming the user is using the form to buy these services), a hotel table and a registration_hotel table. The registration table will record a number of details specific to the registration only such as the customer's name and a unique id number. The hotel table will hold information specific to the hotel only, such as how many rooms have en-suite. The registration_hotel table will hold details specific to that registration at that hotel. You might want a column of type bool to record whether the user requested "en-suite".
When submitting the form, check which pieces the user entered with if(isset($_POST['hotel']) && !empty($_POST['hotel'])). Then only send stuff to the registration_hotel table if that condition is true.
If this design results in making too many separate calls to the database, you might want to look into transactions which will help you to manage the speed and security of these calls.
If you can post in a specific example of something you don't know how to do, that would be useful.
You didn't specify how you can manage this dynamic form. Can you edit it's PHP/HTML source? One great thing would be if you can label your different variables like hotel[], restaurant[], etc.
If your submitted form is clear enough (i mean semantically correctly structured) you can store the whole submitted form serialized.
Note: this method only working when you don't need to search for specific items in your database.
Edit: maybe i'm misunderstood your problem.
You can create a 'metadata' table like this:
form_id | option_name | option_value
---------------------------------------
1 | hotel | true
1 | restaurant | false

Categories