Simple way to insert foreign key automatically into a table - php

I have two table program(p_id(pk)AI, program_name and another table graduate_survey(id(pk)AI,total_PO1,total_PO2,session,p_id(fk). In the program table values are already inserted as
p_id Program_name
1 B.tech CSE
2 B.tech IT.
..so on. I select from a drop down list B.tech CSE and then redirect to the survey form. I enter the total_PO1, total_PO2 there and submit it.Now i want to insert in the graduate_survey table where in the fk field the p_id of B.tech CSE should automatically enter so that i can know the survey is done for which program..Is there any query in MySQL to do that? The insert operation should be done in php code.Please suggest any query.

Your approach seems to be incorrect. When you generate your dropdown menu in HTML, then the display value (the value that the user actually sees and selects) should be the program name, but the underlying value should be the id of the same record.
When you send the data to the server, then you should send the underlying id value, not the display value. Using HTML's standard dropdown menu, you would create sg like this as an output:
<select name="programs">
<option value="1">B.tech CSE</option>
<option value="2">B.tech IT</option>
</select>
If this control is submitted via a form, then the browser will send the value of the selected option automatically. If you use ajax, then your code has to retrieve the selected value of the select element. In the php code you just insert the underlying value into the graduate survey table.

Related

Changing the database value based on dropdown list (PHP, MYSQLi)

Since I am new to an advanced side of PHP, I would like to know how to change the database value by using a dropdown option list.
Users view of the page: This is what the user will see on the webpage. The users should be able to assign the sensors by selecting the sensors with the checkbox and using the drop down list to assign the sensor to a particular school.
SenSG Table: This is the table, Sensg, where all the sensors' information are stored. The school_id is the value which I would want to change when the users checkbox a sensor and choose a school in the dropdown.
School Table: This is the table, School, where all the schools with their respective school_id. The school_id is not auto-incremented.
How do I go about in changing the school_id of SenSG table when I assign a particular or more sensors to a respective school using a dropdown list and it will change its value based on the dropdown list.
For example,
If I check-boxed the first sensor and choose the dropdown box of option 'Republic Poly', the school_id in the SenSG table should change according to the dropdown value.
How do I go about doing this in PHP as I am completely lost.
Thank you.

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

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.

Fill list from different table based on primary id

I have 3 table which have relation with each others... In 1st table, i have student details contains studentid (PK), studentname, dob, etc...
Table 2 contain course consist of coursecodeid(PK), studentid(FK), coursename, teachername and etc..
Table 3 contain grade consist of studentid(fk), courseid(fk), id(pk_autoincrement) and grade.
When system starts, studentid will be load into a dropmenu. Onchange this dropmenu, second dropmenu will be shown and load with courseid take by selected student (based on studentid)
When user select courseid onchange a label will be visible to show student grade.
Everything done in 1 page... Anybody can give me sample code either in php or javascript since i am using php and mysql database.
Ajax. That is the way to go. Check here for ideas, this is doing something similar to what you are trying to achieve. Check this for more abstract code reference. This is a possible duplicate. And generally, searching for loading combobox based on other combobox jquery will give you pretty decent results.

how to populate a drop down list and save it to your database?

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!" />
Steps to accomplish this
Select the moods from a moods table
Loop through the results and put them into a select input
Upon submitting, update the given mood into the user table
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.
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.

Showing selected country in a dropdown using PHP

On my page I have a dropdown menu with a list of countries, this is a page for users to update their address details so I would like to have the "selected" value when they visit the page set to what the entered upon signup.
I have
<option value="254" >Afghanistan</option><option value="255" >Albania</option>
Etc as my HTML markup and in the database the user's country is stored as the code like in the markup.
Normally this would be simple but as it is such a vast list of countries I can't write php code in each one manually.
Thanks
You tagged the question as MySQL.
The answer is there:
Make a table called countries
Table countries
---------------
id integer auto_increment
name varchar(255)
code char(3)
....
And run a query like
SELECT name FROM countries ORDER BY name
Use the output in your dropdown.
If you are using PHP to create the drop-down, simply insert the "select" test into the loop. (If you aren't, then you might want to - create an array of country vals or get from DB and create the options with a PHP loop.)

Categories