Insert flag values ​​into the database

I need help for this problem, which I have been trying to solve for a while (I am new to PHP). I have a form with several flags whose values ​​are retrieved from the database. I managed to display them in a form, assign each of them an appropriate value, but they cannot insert their values ​​into another database.

Here is the code:

<form id="form1" name="form1" method="post" action="">
<?php
$info_id = $_GET['info_id'];
$kv_dodatoci = mysql_query("SELECT * FROM `dodatoci`") or die('ERROR DISPLAYING: ' . mysql_error());
while ($kol = mysql_fetch_array($kv_dodatoci)){
    $id_dodatoci = $kol['id_dodatoci'];
    $mk = $kol['mk'];


    echo '<input type="checkbox" name="id_dodatoci[]" id="id_dodatoci" value="' . $id_dodatoci . '" />';
    echo '<label for="' . $id_dodatoci.'">' . $mk . '</label><br />';
  }
?>
<input type="hidden" value="<?=$info_id?>" name="info_id" />
<input name="insert_info" type="submit" value="Insert Additional info" />
</form>
<?php
if (isset($_POST['insert_info']) && is_array($id_dodatoci)) {
    echo $id_dodatoci . '<br />';
    echo $mk . '<br />';

    // --- Guess here the problem  ----- //
    foreach ($_POST['id_dodatoci'] as $dodatok) {
         $dodatok_kv = mysql_query("INSERT INTO `dodatoci_hotel` (id_dodatoci, info_id) VALUES ('$dodatok', '$info_id')") or die('ERROR INSERTING: '.mysql_error());
     }
}


?>

my problem is to iterate over all the checkboxes, and for each checked one - fill out a separate record in the database. I don’t really know how to recognize which cell is checked and put the corresponding value in db.

I hope someone can help me solve this problem or give me some recommendations.

Thanks in advance.

+1
6

, , . , /get/post PHP.

, . 'on', value = '' HTML.

, ( , ):

HTML:

<input type='checkbox' name='ShowCloseWindowLink' value='1'/> Show the 'Close Window' link at the bottom of the form.

PHP:

if (isset($_POST["ShowCloseWindowLink"])) {
    $ShowCloseWindowLink=1;
} else {
    $ShowCloseWindowLink=0;
}

        .....


$sql = "update table set ShowCloseWindowLink = ".mysql_real_escape_string($ShowCloseWindowLink)." where ..."

( ShowCloseWindowLink, 1 0)

+3

!

, , POST , .

(-array), , .

:

<input type="checkbox" name="my_checkbox[<?=$id_of_checkbox?>]">
<input type="hidden" name="array_checkboxes[<?=$id_of_checkbox?>]" value="is_on_page">

, $_POST:

array(2){
 array(1){"my_checkbox" => array(1){[123]=>"1"}}
 array(1){"array_checkboxes" => array(1){[123]=>"is_on_page"}}
}

, , - :

foreach ($_POST["array_checkboxes"] as $key => $value)
{
  if($value=="is_on_page")
  {
    $value_of_checkbox[$key] = $_POST["my_checkbox"][$key];
    //Save this value
  }
}

, !:)

,

0

: HTML <label>. <label> "" ID, . . , , .

, . , a htmlspecialchars() htmlentities() mysql_real_escape_string(), .

0

2- :

- :

HTML:

echo '<input type="checkbox" name="id_dodatoci[]" value="'.$id_dodatoci.'" />';

PHP:

if ( !empty($_POST["id_dodatoci"]) ) {
    $id_dodatoci = $_POST["id_dodatoci"];
    print_r($id_dodatoci);
    // This should provide an array of all the checkboxes that were checked.
    // Any not checked will not be present.
} else {
    // None of the id_dodatoci checkboxes were checked.
}

, , php . , post/value.

:

http://www.php-mysql-tutorial.com/php-tutorial/using-php-forms.php

0

, -, , HTML, , :

<input type="hidden" name="my_checkbox" value="N" />
<input type="checkbox" name="my_checkbox" value="Y" />

! - ...!

0

, . , $i.

if(isset($_POST['id_dodatoci'])){
    $id_dodatoci=$_POST['id_dodatoci'];
    $arr_num=count($id_dodatoci);
    $i=0;
    while ($i < $arr_num)
    {
        $query="INSERT INTO `dodatoci_hotel`(id_dodatoci,info_id) 
            VALUES ('$id_dodatoci[$i]','$info_id')";
        $res=mysql_query($query) or die('ERROR INSERTING: '.mysql_error());
        $i++;
    }
}
0

All Articles