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

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.

Related

Simple way to insert foreign key automatically into a table

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.

PHP/MySQL cell showing info from another recordset

I am creating a PHP/MySQL web site. On one page there is a table which is showing a recordset of users (Recordset1). One of the users field is company (idempresa), where I am storing the id field from the table companies. The table shows the user fields: username, email, company. As you may guess, the cell for the company value shows the company value (idempresa) and not the company name which is part of Recordset2. This the code for the cell:
<?php echo $row_Recordset1['idempresa']; ?>
How could I update the cell content to show the company name?
Best place would be in your query as a Left Join. Then you will have both record sets in one. Here is a link to an example: w3schools.com/sql/sql_join_left.asp

Get dropdown name instead of value when editing user

I am in need of some help, I have a table where it shows the people that have signed into a patient program.
Here is how it looks:
Right now if you look in the Location column, it says 8 instead of the last option in the dropdown being Manalapan.
Here is how the database table looks for offices location:
I would like to grab the office_name data from the database while retrieving the patient information in the table,from the table patients:
It is stored in the database as pat_loc and this one is 8.
In the end, I want it to say the name of the office, not the number it is stored at, first getting it from the patient table then going to the office table for the name.
Is there such a thing that can be done?
If needed, I will show my current code to see where it needs to be added.
You could try doing an inner join so you can grab all the data you need from both tables
SELECT * FROM patients INNER JOIN offices ON office_id = pat_loc
And then accessing it through your row array like $row['office_name]

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.

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