本文介绍了将复选框值插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一段时间解决这个问题的帮助(我是PHP的新手).我有一个带有多个复选框的表单,这些复选框的值是从数据库中提取的.我设法以表格的形式显示它们,为每个值分配一个适当的值,但是无法将它们的值插入其他数据库中.

I need help for this problem that i'm trying to solve for a while (i'm new in PHP).I have a form with several checkboxes which values are pulled from a database.I managed to display them in the form, assign an appropriate value to each, but cannot insert their values into other database.

代码如下:

<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's 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());
     }
}


?>

我的问题是循环遍历所有复选框,对于每个选中的复选框,在数据库中填充单独的记录.实际上,我不知道如何识别选中了哪个框,并在db中放入适当的值.

my problem is to loop through all checkboxes, and for each checked, populate a separate record in a database.actually i don't know how to recognize the which box is checked, and put the appropriate value in db.

我希望有人能帮助我解决这个问题或给我一些指导.

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

谢谢.

推荐答案

您可以知道是否选中了一个复选框,因为该复选框具有一个值.如果未选中,它将完全不会出现在PHP的request/get/post中.

You can tell if a checkbox is selected because it will have a value. If it's not selected, it won't appear in the request/get/post in PHP at all.

您可能想要做的是检查它的值,然后根据该值工作.该值默认为字符串"on",但可以通过HTML中的value =''属性进行更改.

What you may want to do is check for the value of it and work based on that. The value is the string 'on' by default, but can be changed by the value='' attribute in HTML.

以下几段代码可能会有所帮助(虽然不完全是生产质量,但将有助于说明问题):

Here are a couple snippets of code that may help (not exactly production quality, but it will help illustrate):

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)

(assuming a table with a ShowCloseWindowLink column that will accept a 1 or 0)

这篇关于将复选框值插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 19:57
查看更多