dynamically add rows and then insert date into database - php

i want to add rows using loop and then want user to fill the rows then i want these rows' data (tds contain textboxes and selection lists)to be saved into database? i need to know how is it possible? jquery and php will be preferred.
DatA not datE.

run this command in loop. rows automatically added in your table
mysql_query("INSERT INTO table_name(feild1, feild2) VALUES ('value1', 'value2')");

the easiest way to do this is to create a php file which will take arguments from jquery and execute it.
while assigning each element of form a unique id and counting total rows will give us a chance to use loop and we can send each row elements' data using loop. php file will get the arguments and execute it .the ajax request will be send count-row times.

Related

Inserting dynamically generated form data into mysql

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"

PHP Insert / Update a mysql table from HTML form

This is my first mysql table.
I use HTML form in order to insert datas into a mysql table.
When submited, the form calls a php script in order to add datas into the form.
Then, mySQL table could look as follow :
From this mySQL table how could i generate and display a HTML table sorted by "NEXT_EVENT" column ?
If i want go on step ahead, is there a way a let user click on an upward / downward arrow placed next to each column header (links) in order to get the HTML table sorted as the desired column ?
--
Once the table sorted, i need to generate a raw simple TXT file where the ID on very FIRST row and NEXT_EVENT values will be copied to.
ie, sort.txt :
the 2 fields are comma separed.
2, 1499005140
Many thanks for your help,
To have such a link next to the heading you can use jQuery plugin tablesorter. You can refer at
http://tablesorter.com/docs/
And for just executing a query for one column, you can use ORDER BY clause
Hope it helps

Manipulate a text box and displaying it dynamically?

I want to create a dynamic number display of texbox, there will be a a textbox and a submit button, then the numeric value of my first textbox shall display also the number of textbox. Which will allow me to input my records in my database. I am using MySQL for my database and how would I insert the records to my table if the first set records of my textbox is not the same as the second (example I input 3 on my first textbox then three textbox shall appear and a submit button, on my second set of records I decided to have 6 on the first textbox then six textbox shall appear in the page) my question again how am I going to do this?
If ever I use array in fetching records and indexing the value of my dynamic textbox, what type of array shall I use to this? How will I going to name the value of my textbox dynamically? and lastly the I wanted the restrictions also that will set the maximum number of textbox.
Ok. Multiple textboxes, let's call them columns. The first record the user enters in each row is the number of columns they require. The submit button would just call a javascript function to create the required amount of columns for that row on the page. This would have the restriction as to how many can be created and the PHP would also ignore extras if the user works out how to cheat it.
Jumping to your last question (because the second question needs this info). You have decided what the maximum number of columns is so the easiest way if that number isn't large would just be to create a table that can satisfy that many columns. Some fields would be NULL on some rows.
It doesn't matter that the number of columns is different when you want to insert because, for each row, you expect up to a certain amount and only deal with the values you are given, those missing are NULL. When you come to output them you only output those that aren't NULL.
The bit about using an array in fetching records and indexing doesn't make sense to me sorry, but I suspect you're fishing for what php database class to use get the data back from the database? I think the trend is mysqli. Personally I use a bespoke one.

Updating related tables in MySQL

I need to update two tables in MySQL with PHP. The second table needs the ID of the row being inserted in the to first table.
At the moment I have some PHP code that loops through this process for each of the items in an array:
Check if record exists by attempting to get it's ID.
If the record doesn't exist insert it and get the last insert ID.
Update the second table using the ID we found as a foreign key.
This is very inefficient as multiple database calls are made. I would rather store the data in two arrays, one for each table, then batch insert them when the loop is done. The problem is I need to get the ID of the row in the first table before I can do this.
This is a problem I come across a lot. What is the most efficient / 'best practice' way of doing this?
Thank you
Create stored procedure for inserting whole hierarchy in one server call. Supply all parent-child records as XML and parse it/insert records inside procedure (afaik MySql should have XML-functions similar to MS SQL). This will result in the same number of INSERT statements however they will execute on server side which should improve performance. E.g.
exec MySp #myHierarchy = '<Recs><Parent Name="P1"><Child Name="C1" /><Child Name="C2"/></Parent></Recs>'

Retrieving Autogenerated content in mysql using php

Well, here's my problem. I've got a php script which posts a message to a mysql database. The mysql database generates two values automatically, one is a timestamp and the other one is a unique id for the message.
I need those values to be able to update my page using ajax (json). I want to know how do I get those values from the same php script from where i've posted my message to be stored in the mysql database?
mysql_insert_id will give you the last autoincrement value. Then you can use a SELECT to retrieve the timestamp.
You could use mysql_insert_id to retrieve last inserted ID from the table

Categories