Php / mysql query to update data table through foreign key - php

I have 3 php tables, which are response, question and answer. the response table is dynamically retrieved from mysql and displayed on my website. the response table displays values from the question and answer table through foreign keys.
I want to edit a field in the answer table through the response table which is being displayed, however i do not know how to build the query for it to be able to do this. the description field in the answer table is the one which i want to edit.
I have tried researching on how to do this, but as i am new to php / Mysql i have not found a solution.
Please see below a designer view of my tables

Related

how to use the primary key of a table in database as a foreign key in table 2 while inserting the records from a single form simultaneously in php

I am fairly new to web development and currently working on a website using an MVC framework that can capture maintenance work conducted. I have managed to make the forms and it correctly displays any errors in filling the form and if there aren't any errors successfully inserts it into the database. What I would like to achieve is having the main table with the general details of the maintenance such as (date, time, technician, department, location, recommendations) and another table for which records what tasks were done during the maintenance such as sweeping, mopping, wiping the windows, cutting grass, etc. I have a single form that requires all the details required in both the tables to be filled. both tables will have primary keys that will be auto-increment. I would then like to simultaneously insert the data into the relevant tables only while inserting data into the tasks table I would like to have a foreign key to the main table for that particular record so it corresponds accordingly. How can I achieve this without manual input by the user if the primary key of the main table is an auto increment?
This isn't a big problem. It can't be done as a single query, but using transactions you can achieve an all-or-nothing result.
In pseudocode:
Validate data
Start a transaction
Insert data into main record
Get the last inserted ID
Insert one or more records into the child table, using the ID retrieved above
Commit the transaction (or roll back if some error occurred)
The exact mechanics vary between MySQLi and PDO, but the principle is the same.

Storing arrays/lists in one mysql entry

I am working on a project that requires a MySQL database.
The project is a quiz/study site and it needs to store a users login data and then a list of the quizes they have saved, and their score for each question on each quiz they have saved. So say we have user 1. I want to go to a table called "user_content" and then be able to see which quizes they have saved and their scores for those quizes.
The quizes will be made in a unique database, and referenced via a foreign key.
The issue is, I want to have a fixed number of columns in the "user_content" section, so I need just one column that has all the foreign keys for the quizes that a user has saved. I don't want a system where you have column "quiz-1" and "quiz-2" is there a way to just have one column that stores a list or array of all the quiz foreign keys? The same goes for storing the user's scores for their favorite quizes, I want a single column that will store an array/list of all their scores. Is this possible in MySQL and what is the best way to achieve this?
(This is using php, not javascript)
So as in the comments this could be accomplished in a well defined RDBMS structure for example: table for the quiz ID and then line item entries for each quiz foreign key back to the quiz ID table.
To answer your question you can store an array as json into a MySQL database by passing your PHP array into json_encode() then inserting that into the MySQL field. In the long run this could impact performance and flexibility when writing queries. You would have to run json_decode() back in PHP to restore the structure of the array. But this would solve the requirements of the question.

What is the correct way to store, increment and reuse an index

I am in the process of building my first bespoke PHP/MySQL app. It is a kind of survey application for vehicle testing.
The survey itself is built via queries to the database where I have stored the questions and using a loop they get outputted to the page each with a unique ID and name.
All is working so far. I can successfully submit results and access the $_POST array and can see answers stored against the correct keys.
I am at the stage now where I wish to save the results.
My intention is to use a table to record the results using one row per answer. The columns I plan on using are survey_id, question_id (the key from the array), question_answer.
My question is how do I correctly store and increment the survey ID itself? If I was recording the survey in such a way that a single row would contain all of the results for a single survey I could auto increment the survey_id field but when I was designing the app I did not think this would be the most robust way to store results. Each survey will be in excess of 100 rows each and any changes to the layout of the survey down the line would be tricky to implement (I suspect).
My initial thought was to create a stand alone table to record and increment survey ID's and then to query this to populate the survey_id in the results table.
I would love to hear the best practise to achieving this.
The best way to do this is to have three tables:
Surveys
Questions
Answers
Surveys would probably have a survey ID, user that created the survey, so on. Questions would have a question ID, the survey ID that the question is for, and what the question is asking. Lastly, Answers would have the answers to the questions, most likely having an answer ID, question ID, user who answered it and their answer.
In essence, your original idea of
create a stand alone table to record and increment survey ID's and then to query this to populate the survey_id in the results table.
is the right one in this circumstance.

Merge two tables to a new table using php and mysql

Hello this is my first time i post but hopefully i won't mess up to much.
Basically i'm trying to to copy two tables into a new table, the data in table 2 and 3 are temp data that i update with two csv files. It's just basic data that share the same ID so thats the Primary Key and i want these to be combined into a new table. This is supposed to be done just once a day handling about 2000 lines Below follows a better description of what i'm looking for.
3 tables, Core, temp_data1, temp_data2
temp_data1 has id, name, product
temp_data2 has id, description
id is a unique since it's the product_nr of the product
First copy the data from temp_data1 to Core. Insert new line if the product does not exist, if it do exist it should update the row with the information
Next update Core with the description where id=id and do not insert if id do not exist (it should not exist)
I'm looking for something that can be done in one push of a button, first i upload the csv file into the two different databases (two different files) next i push a button to merge the two tables to the Core one. I know you can do this right away with the two csv files and skip the two tables but i feel like that is so over my head it's not even funny.
I can handle programming php it's all the mysql stuff that's messing with my head.
Hopefully you guys can help me and in return i will help out any other place i can.
Thanks in advance.
I'm not sure I understand it correctly, but this can be done using only sql script, using INSERT INTO...SELECT...ON DUPLICATE KEY UPDATE... - see http://dev.mysql.com/doc/refman/5.6/en/insert-select.html

How do I link data on Phpmyadmin

First I don't know what a good title for this question
Example
On postid and wordid I can click and it will go to another table.
I want to know what code to make data in phpmyadmin can be click and link to related table?
that are just foreign keys and will let you know which table has this key so that it will be easy for end user (developer) to know yes this field has the relationship with this field of this table.
If you don't use innoDB, you'll have to use the database designer tool in phpmyadmin to set the relations between your tables. After that, you will be able to click on foreign keys as you want.
Please show other table views. postid looks like the Primary key and wordid comes from another table.
This is a basic sql join here: http://en.wikipedia.org/wiki/Join_%28SQL%29

Categories