There are number of records which I gather them from mysql say thease:
id name mark
1234 john 18
53 smith 12
324 mike 15
...
I want to build a form to give ability to edit(update) all marks at once
I know that I can show them at the form using textbox value property.
But how can I Identify the exact same record when I want to process the posted form, in order to update the correct filed? and surly, I don't know how many records are there in the form.
The idea might be identifying the records through the id field.
but how to do that?
If this application is for administrative use only:
<input type="hidden" name="id" id="id" value="_ID_VALUE_" />
<input type="text" name="name" id="name" value="_NAME_VALUE_" />
<input type="text" name="mark" id="mark" value="_MARK_VALUE_" />
then
UPDATE table SET mark = '_SANITIZED_MARK_VALUE_' WHERE id = "_SANITIZED_ID_VALUE_";
If it's an application for the end user, you don't want to trust that he/she won't change the value of the hidden input. In this case, you'll most likely want to store the id, name, and mark in a $_SESSION variable and do comparisons on the post to figure out which record pertains to which id, and then build your update statement accordingly.
When you build your form, use the ID from the database as part of each text field's NAME attribute. Then when you receive your array of posted values, you can process the keys to find out the ID of the record you should be updating. For example, you could have your fields named "record-1234", "record-53", etc. Then you would iterate over the keys in $_POST, split them on "-" and use the resulting IDs in your UPDATE query.
Related
Sorry if this is a stupid question but im really not sure how to tackle this, perhaps im overthinking. I need to retrieve both the name attributes value and the value attributes value. Have a look at img below:
echo'<input type="radio" name="'.$eventId[].'" value="'.$team1.'">';
The name contains the event_id and the value contains user selection. I need the name Ids value to insert event Id into db along with user selection.
I know how to retrieve the rad value attribute but not sure about the name, maybe Im overthinking it or need to change my logic. Any ideas?
If user select Australia radio button than in PHP you will get value 'Australia' but you want the value 'Australia_80' to store in db. Change all your radio button values and names like
<input type="radio" value="Australia_80" name="getRadio">
<input type="radio" value="Canada_81" name="getRadio">
I have a form that, upon being submitted via POST, needs to be inserted into two separate tables based on the field. Each field needs to be added as its own line.
Imagine a survey; there are four sections total. For each question within a section, the user selects a value between 1 and 5. There is also an optional notes text area at the bottom of each section.
Each question has its own unique ID in the database in a "questions" table. These questions contain the ID of the section ("sections" table) it belongs to for reference.
Question 1:
How can I insert each answer as its own row in a table called "answers" with the ID of the question?
The structure for "answers" looks like:
id (AI) | question_id | value (user submitted, 1-5) | response_id
Question 2:
How can I then insert each note for each section into a table called "notes" with the id of each section?
The structure for "notes" looks like:
id (AI) | section_id | value (user submitted) | response_id
Response_id is the resulting ID of inserting the user's response into a table called "responses." This table ties it all together for outputting the results for each user submitted response.
Thanks in advance.
You might want to use an array to keep track of the ids you need.
Your HTML block should look something along the lines of:
<h3>Question '.$questionId.': Your question title here</h3>
<input type="hidden" name="question_id" value="'.$questionId.'" /> //Not necessary, but just to show you that a hidden field can be useful too
<input type="text" name="answer['.$questionId.']" />
Then you just use the $_POST data as a regular array, in a for loop or in a big query (better performance depending on the amount of fields/data you are dealing with).
More info on multidimensional arrays in this post: Submitting a multidimensional array via POST with php
You can do one thing like this may be. You said you have question id for each question right so when user selects answer give name to each question like this
name = "question[<?php echo $question_id ?>]"
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
For the not just give simple name. When user submits it take the entire question[$ids] array loop through each id and insert into answers table using question id. You need to little bit of array operations.
For Notes you just use section id for in the name field and insert into database normally.
I have a HTML table base invoice for collecting the details of a order. New table rows can be added or deleted to this as the user needs them. Table has columns like item name, type, color, unit price, qty. I have place <input> and <select> tags inside <td> tags to collect data.
I need to submit a complete invoice to a database. The problem I'm having is I'm not sure how to get input values to my $_POST variables since the number of <input> and <select> vary from time to time based on the items on the invoice.
I can give dynamic or static names to input fields based on the table row id when they are generated using javascript. But how can I collect these data in the submit end to my php arrays or variables?
This is one table row. All other are similar to this.
<tr class="table_row_blue">
<td class="table_data_index">1</td>
<td><select>
<option>Test</option>
</select></td>
<td class="table_data_item"><select>
<option> 2GB </option>
</select></td>
<td><input type="text" id="price_field" size="6"></td>
<td><input type="text" id="qty_field" size="6"></td>
<td><span class="table_subtotal">125,200.00</span></td>
</tr>
Please give me some ideas to implement my idea.
encapsulate all the varying parts in separate tables in the database. so you can store them separately and relate to each other with foreign keys.
also it's a good idea to use array naming for you HTML inputs like
<input name="data[Invoice][field1]" />
<input name="data[Invoice][field2]" />
this gives you data, which is more structured and easy to iterate.
Add a hidden form value, which will hold the number of rows you are going to submit.
On the PHP end, first of all check the number of rows and then get all values using
a row index.
You can have input fields which submit to an array:
<input type="text" id="price_field" name="data[0][price_field]" size="6">
which will appear in you $_POST as
$_POST['data'][0]['price_field']
I would suggest you use the unique id of an entry as differentiator (replacing the 0 in the above example)
I'm trying to create a small web app that is used to remove items from a MySQL table. It just shows the items in a HTML table and for each item a button [delete]:
item_1 [delete]
item_2 [delete]
...
item_N [delete]
To achieve this, I dynamically generate the table via PHP into a HTML form. This form has then obviously N [delete]-buttons. The form should use the POST-method for transfering data.
For the deletion I wanted to submit the ID (primary key in the MySQL table) of the corresponding item to the executing php skript. So I introduced hidden fields (all these fields have the name='ID' that store the ID of the corresponding item.
However, when pressing an arbitrary [delete], it seems to submit always just the last ID (i.e. the value of the last ID hidden field).
Is there any way to submit just the ID field of the corresponding item without using multiple forms? Or is it possible to submit data from multiple forms with just one submit-button? Or should I even choose any completly different way?
The point why I want to do it in just one single form is that there are some "global" parameters that shall not be placed next to each item, but just once for the whole table.
<input type="submit" name="delete[1]" value="delete">
if (isset($_POST['delete'])) $id=key($_POST['delete']);
it seems to submit always just the last ID
It submits all of them, but since the name doesn't end with [], PHP discards all by the last.
Is there any way to submit just the ID field of the corresponding item without using multiple forms?
No. At least not without some unfortunate JavaScript. All (non-disabled) hidden inputs (with names and values) will be successful. You can't limit based on proximity to a clicked input element.
If I understand your goals correctly, you have two main options.
Put one form per row (in the cell with the delete button)
Encode the id value into the name of the submit button
You could get rid of the hidden fields and name your submit buttons like this:
<input type="submit" name="delete[1]" />
<input type="submit" name="delete[2]" />
<input type="submit" name="delete[3]" />
and then
<?php
if (isset($_POST['delete'])) {
$toDeleteId = key($_POST['delete']);
}
Level: PHP Learner
I'm stuck with a checkbox problem. I have a db that contains names and unique id numbers.
Using a query, I am pulling a selection of students and showing them to a user in an ultra simple HTML table on a form. Each row begins with a checkbox. The method is POST. So far, so good. My table looks like this:
+-----------+----------+----------+
| SELECT | NAME | ID |
+-----------+----------+----------+
| [] | John | 2233 |
+-----------+----------+----------+
| [] | Susie | 5577 |
+-----------+----------+----------+
[-SUBMIT-]
My problem is that I cannot seem to make the checkbox associate with each record's unique ID. Once the user has selected rows and clicked submit, the $_POST array remains empty.
None of my beginners books reference this specific issue. They go through the "regular" checkbox routines that don't involve interacting with rows from a db. I also could not find an issue on Stackoverflow that addresses this. Also tried Google: plenty of stuff on checkboxes, but I couldn't find any that helped me on this problem.
Just do something like this:
<input type="checkbox" name="ids[]" value="2233" /> ... rest of row
<input type="checkbox" name="ids[]" value="5577" /> ... rest of row
Now, in PHP, you can get the selected ids like this:
$ids = $_POST['ids'];
if( empty($ids) ) $ids = array();
This sets $ids to an empty array if the form was submitted without any of the checkboxes checked.
Edit: If your form doesn't get more complex as you describe, take Doug Neiner's approach, as it is way simpler. This approach is right if a table is likely to have a number of columns.
I like to do it this way:
Number the checkboxes sequentially (1 to 100) and add a hidden field connecting the row number to a real database ID:
<input type="checkbox" name="row_1" value="checked">`
<input type='hidden' name='row_1_id' value='2233'>`
Store the total number of rows in another hidden field
<input type='hidden' name='row_total' value='99'>
Then, in the receiving script, iterate from 1 to the total number of rows using for, check whether this row was selected, and get the associated database ID:
for ($i = 1; $i <= $number_of_rows; $i++)
{
if ($_POST["row_$i"] == "checked")
{
$database_id_unsafe = $_POST["row_{$i}_id"];
...
the latter, of course, needs to be properly sanitized and escaped in case it is processed further.