I am struggling with inserting the following into a MySQL database:
To create a series of HTML checkboxes
$
q="Select id, subject From subjects";
$r=mysqli_query ($d, $q);
If (mysqli_num_rows($r)>0) {
while ($row =mysqli_ferch_array($r, MYSQLI_NUM)) {
Echo "<input type=\"checkbox\" name=\"$row[1]\" value=\"$row[0]\">$row[1]";
}}
This is fine.
However, what is the $_POST to validate?
I have validation for an individual $_POST value, however now as an array do I have to run the results through some form of foreach statement and just loop it through using the forearm statement?
Also, if I wish to then insert the resultant array into a different table in the ddatabase do I also have to run the result through a foreach as well?
Yes, if youâre posting the results of that code back into the database table, a foreach() through the $_POST array will probably do it, not least as you might want to change the default checkbox value from âonâ to something more suitable for the database.
How you insert it into the other table depends on the layout of the table - if each element of the array will go in a separate table row, then a loop is probably easiest. If theyâre all to go in the same row, then it will depend on how you align them to separate columns in the table.
you build the checkbox-names from the query, so just use them again to read from POST. first try iterating over them like you did above. second: grab the actual one from POST. $_POST[$the_key_you_got_from_the_statement]
Sorry I donât understand what you mean by keys I am getting into the code from a text and was able to figure out how to get the checkboxes to write to the screen as above however I canât figure out the next step
you iterate through the results of your SQL-query and put the keys in the name field of your input. so thatâs the keys you can expect to come back from POST. so afterwards, how do you know which keys you get from POST (if they got checked)? they are the same as the ones you got from the DB.
When you output the form, you will end up with a series of checkboxes, the name of each of those checkboxes is retrieved by your code, from your database, and will be whatever appears in $row[1] from your query. So presuming those are the subject names, then that will be the names that appear. You can check this easily by just running your code, and then viewing the HTML source that it produces in your browser. Presuming a subjects table like this:
id subject
1 French
2 German
3 Swahili
4 Welsh
5 English
and so on. So you can see, the names for the $_POST array in this case will be your subject names.
In my opinion, itâs a poor way of naming fields, for precisely the reason that youâve asked the question - it wonât be easy to know what the field names are. In your position I would probably use an array instead:
which would bring the selections back in an array within the $_POST array:
foreach ($_POST['lang'] as $lang) {
echo $lang;
}
At the start of your processing code, var_dump($_POST) and have a look at the values and field names that you get back from the form submission, which should point in the right direction to process them. I believe that what @chorn is driving at is that your processing code could re-run the same query to get the field names out into an array, and that would give you your list of field names. But that sounds a lot of work just to have dynamic field names.
This is what I tried in the interim:
I have a bit of a background in C, so I played with the $_POST variable as I would with an array until I got it to print to screen and then inserted the resultant values.
I have found this to work:
foreach ($_POST as $key => $value)
{
if ($value !=âSubmitâ) {
if (isset($key) && filter_var($key, FILTER_VALIDATE_INT, array(âmin_rangeâ =>1))) $insert[$key] = $value; // this is on one line on my computer
}
else
{
$errors = âPlease select a subjectâ;
}
// This produces an array variable $insert wiht the relevant $key and $value parameters
foreach ($insert as $key => $value)
{
// insert into mysql using variables $key and $value
}
I am thinking that your lang name up above would certainly bypass my solution. It was all in naming of the lang!
the only extra work he has to do is to copy the existing code (best if DRYâed) with a condition.
q="Select id, subject From subjects";
$r=mysqli_query ($d, $q);
If (mysqli_num_rows($r)>0) {
while ($row =mysqli_ferch_array($r, MYSQLI_NUM)) {
if(isset($_POST[$row[1]])) ... // insert
}}
if the checkboxes are just a recommendation, he may insert what the client gives. but at least for validation he somehow needs to get the fields from the beginning.