I have a simple HTML form with three fields name,school and city. Also within the form, I have a button which I am using to insert a sub form with values neighbourhood and population. The crud mechanism is using jquery append.
In my database, I am saving the main form data in one table and the sub form data in another table.
My question is, during the creation of data in the main form, if I insert the data of the main form into one table and get the last inserted Id and use the id as a reference when inserting data into the second table, will this approach always work in both mysql and ms SQL?
If not is there a better approach I can adopt?
It will work, as long as your dynamically created input has the proper name attribute..
For example: name="txt_neighbourhood"
Related
Edited Question:
Thank You for your answers. Sorry for that i am not really looking as per your answers. I want multiple insertion from textarea box while form submission.
i have this result. i want to insert multiple data (both column and rows) into mysql table using (textarea box) (on form submission)
when we copy above data to textarea box its look like below
following source link already done with single column values.. But i want to insert two column values same time in mysql.
https://www.webslesson.info/2019/06/how-to-insert-multiple-data-into-mysql-in-php-using-textarea-field.html
Any one help me for code
i found two discussion page here below same like my question
How to insert multiple row from textarea to multiple column?
How to insert multiple value and Multiple rows from textarea to MySQL using PHP?
---- but from above no answer posted properly...
For eg:
I want to insert following two columns (state,distance(miles) in mysql using textarea box.
Firstly, you have to use <form></form> tags around the table.
Then to get values of each columns in a row, you can use some <input> tags inside the <td> tags. The reason why doing this is because <td> tags don't have a name attribute and thus cannot use php $_GET or $_POST variables
Once you have the name attribute, use $_POST or $_GET to capture and transfer to database
You can check this link. This will give you some insight
Link here
I am trying to write a survey system with the ability to generate different types of questions with the help of a drag & drop HTML form builder. A client will generate its survey form and the HTML body of that form will be stored in a database table. If that client wants to show that form somewhere he will just call a service from my system and I will provide him that form. On the other hand I also want to store the results of that form in a table. But as I mentioned before that generated forms fields and database tables fields may not match.
The dumbest (well, not so practical and it makes me feel dumb) solution I come up with is generating a matching table for each form.I think it will also be costly because in this case I have to have 1 more table for matching form table (which stores html form of form data) and auto generated table (which stores data that comes from submitting that html form). So, what kind of design should I implement, what do you suggest?
If the types of questions are known and limited (single-choice, multiple-choice, free-text) then I would suggest you to create additional table and update it when user creates a new survey form.
Questions
- id
- question_type
- question_text
- form_id -- reference to forms table
Then you can store the results in the second table
Results
- id
- question_id -- reference to Questions
- user_id -- some identification of who submitted that result
- result -- text or 1/0 or something meaningful for that question type
I am constructing html table with data queried from database using php. I also need to perform edit,delete operations on that table. I am saving the
database row id as hidden input field in each table row.
What I am doing is when user clicks on the particular row I getting the row id using jquery and performing edit/delete operations on the database table
using ajax,php
But the problem here is when user inspects element he can see the ids of each row. So if the user is technical expert he can edit the row id and change the
value of other rows in database table.
Generally how to handle this type of situations without populating database row id in table.
If the user is that kind of expert to look in inspector and manipulate that hidden info, and that is something that has to be strictly not possible to change, you could send guid or some custom backend function that encrypt / decrypt the info from user table..
Populating the row id is a correct, but if the user can change the values of some records but not others, the correct solution would be check server-side if the user has the privileges to edit the row that is currently trying to edit
You can take more info from the any cell of the row.
You save the id in your hidden input then take with jquery any cell and if the data cell and the id are not same in DB don't do anything if have relation then Delete.
hope it will be useful
For more security you have to create hash of each row and send to ajax page and verify both the hash . since it is a one way encryption so you hash for each row is unique..
Here is what i am saying to you
https://www.owasp.org/index.php/How_to_protect_sensitive_data_in_URL's
I have a page on my site that allows users to create a custom form, e.g. they can edit the name of the input field so that it can hold any value that they name it with.
By doing so they create a new table in the database with the name of the input field as the name of the column
By doing so I am going to end up with a huge database, is there a different way of doing this?
I am sorry if my question sounds ridiculous but I am self-taught in MySql.
I would save the input fields in one table with an id linking to a form. The form will be save in an other table with an id.
Form:
- 1 | Test
Input:
- 1 | Name
- 1 | SomeThing
You can always find the input field by searching in INPUT where formId = $form['id'].
Whats the best way to allow users to create tabels in your db with php and mysql?
The best way is for sure: never allow that.
For the such a vague question I see the only not-so-ugly solution: one XML file per form.
Well, you can create a table "page" that can contain Form id as field. Another table will be "form" table, that will have all attributes of form and its "id"(primary key) will be used as "foreign"key in "page" table.
In this way, you can save extra table creation as well as can identify each form uniquely.
I would favour a noSQL database for this purpose, such as MongoDB, but as this isn't available, I would either go for MasterCassims solution or just use a TEXT field in mySQL to store serialized representations of the forms.
I'm sorry if this a stupid question, but I am new to this. I want to store all the information from all my customers' different contact forms in a database so they can retrieve the information later. Each contact form has a different number of fields. Ideally I'd store all of this info in one MYSQL table, but as I said each customer's contact form has a different number of fields.
I was thinking of creating a database with the following fields
ID, customerID, dateTime, data
The data would be the html of the contact form. However I am sure that's not best practice. Also it wouldn't allow me to offer a download option to the customer of all their contact forms as a csv or similar.
I don't want to have a different table for each customer either, and I am sure having a table with 'field1', 'field2', 'field3' etc isn't that good either.
Any ideas?
If you don't need it to be relational, that would be fine. You wouldn't be able to query it very easily.
You could store the answers in another table using keys to link back to the form and field.
Or use a KeyValue store type DB like MongDB, and you could store all your data without worrying about the schema.
Make another table called user_data. In it, have:
id, user_id, data
Then, if they fill out a form with 10 fields, you can enter 10 entries into this table. When you pull data for the user, just also pull any data from the user_data table that has user_id = their id.
If you need more than that, you could also add a "field_name" field or something:
id, user_data, field_name, data
You can use the MySQL database to have a schema for your desired information that relates many tables to one another.
Here is another discussion about database design:
Database Design Best Practices
If you must have only one table, the other choice I would mention is to have the ID created automatically, and then make the fields be not required. This way when a customer fills out one form before the other it won't mess you up. You can then store the ID as a Session Variable and reuse it for different INSERT statements to your database.
You can make a download function for the customer that will output in the desired format that queries your table and then generates the file.