Manipulate a text box and displaying it dynamically? - php

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.

Related

Is there a way to add multiple values with same ID in two different tables?

Using PHP and mySQL, I need to add multiple values in 2 mysql tables : the first table would have the more important informations and the second one would have the less important informations about each items.
To be more clear, ONE element would have his informations split in two tables.
(I need this for some reasons but two of them are : having the first table the less weight possible, and the second table would store datas that will be erase after a short time (meanwhile the first table keeps all the datas it stored).)
In the best scenario, I'd like to add a row in each table about one item/element with the same id in each table. Something like this :
Table 1 id|data_1_a|data_1_b|...
Table 2 id|data_2_a|data_2_b|...
So if I add an element which get the ID "12345" in the table 1, it adds the datas in the table 2 with the same ID "12345".
To achieve this, I think of two solutions :
Create the ID myself for each element (instead of having an auto_increment on table 1). The con is that it would probably be better to check if the ID doesn't already exist in the tables everytime I generate an ID...
Add the element on table 1, get its ID with $db->lastInsertId(); and use it to add the element's datas on table 2. The con is that I have to add one element by one element to get all the IDs, while most of the time I want to add a lot of elements (like one, two or three hundreds !) at once
Maybe there's a better way to achieve this ?
lastInsertId() reports the first value generated by the last INSERT statement executed. It's reliable to assume that when you insert many rows, they are given consecutive id values following that first value. For example, the MySQL JDBC driver relies on this assumption, so it can report the set of id values generated.
This assumption breaks only if you deliberately set innodb_autoinc_lock_mode=2 (interleaved). See https://dev.mysql.com/doc/refman/8.0/en/innodb-auto-increment-handling.html for details about that.
But if it were my task, I would still choose to use a single table. When you find you don't need some of the columns anymore, use UPDATE to set them to NULL. This will eliminate the problems you're facing with assuring the same id is used across two tables.

how to insert multiple column/ row values from textarea into mysql

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

Update or insert only columns that changed

I am experimenting trying to find the best and most efficient way to alter the data in a given table through a form using PHP.
The scenario is a list of items in a table, if you right click->edit an item, a request is made to MySQL for all the data and the fields are populated.
The user can change or leave the data untouched in any of the fields, and then presses save which sends everything back to PHP.
The easy way would be to just update all the columns regardless of whether or not they have changed, i.e.:
$this->model->set('name', 'some name string from the form', $itemId);
$this->model->set('price', 'number from the form', $itemId);
...etc...
So potentially I could change just the name and needlessly update the rest of the columns with the same data as what was received. (As a side question, does MySQL know this and ignore the update behind the scenes?)
Would a good way to perform an intelligent update be to compare two arrays? One that contains the original data and another with the data from the user. If values of a given index don't match, then it must have changed and so do the update?
i.e. a very simplified example:
if($submittedValues['name'] != $originalValues['name'])
{
...Update...
}
I guess you answer your question, and you could compare two array, either in your PHP code or using javascript and instead of sending every thing to the server, only send the changed values.
But in general I wouldn't care if I reset all the data, the process of affecting all fields again could be faster than comparing between old and new data in arrays, I would take much care if I was making many queries to the database but its only one update query
What could be interesting in test is, when the user lefts the fields empty, then the request will send an empty string, at the end it the update request will insert an empty string where a NULL value would have a better signification

mySQL select only greater than zero columns

I have a form wich permits the users to leave inputs unfilled. I register those fields as decimal(2) "0.00" in the database.
After the form is inserted in the database, I want to permit the users to edit that form.
My question is, for performance's sake, how do I select only the columns different than 0.00 for that specific row corresponding to the user's form input?
By the way, I am using CodeIgniter's Active Record functionality but I don't get upset if I get what I need using a string sql query :D
A pseudocode I imagine:
select _columns-greater-than-zero_ from _table-name_ where id=_row-id_
So you want to select a variable list of columns based on what value a specific row has in that column? Why would you want to do that? It would not improve performance at all, and in fact, the extra checks it would need to run against the values of those columns would make the query slower.
If you're trying to figure out whether or not to display the column on a page, just add some conditions to your code to check if the value in the returned row equals 0.

dynamically add rows and then insert date into database

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.

Categories