如何使用php将多个复选框值插入数据库

新手上路,请多包涵

通过此代码,我可以将复选框值插入数据库。但是我需要在 phpmyadmin 中添加一列,并且在该列中我需要存储值,例如,如果我选择 2 个复选框,我需要将该 ckeckbox 值存储在另一列中的一列中,我需要将选中的复选框存储为 YES 并将未选中的值存储为 NO .但是看到我想要两列

这是我的代码

<input type="checkbox" name="checkbox[]" value="Home" id="pageHome" onChange="toggleVisibility('home');" /><label for="pageHome"> Home</label><img id="home" src="images/icon-tick.png"  style="visibility:hidden"/><br/>

<input name="checkbox[]" value="About_us" id="page_Aboutus" type="checkbox" onChange="toggleVisibility('aboutus');"><label for="page_Aboutus"> About Us</label><img id="aboutus" src="images/icon-tick.png"  style="visibility:hidden" /><br/>

<input name="checkbox[]" value="Services" id="pageServices" type="checkbox" onChange="toggleVisibility('services');"><label for="pageServices"> Services</label><img id="services" src="images/icon-tick.png"  style="visibility:hidden" /><br/>

<input name="checkbox[]" value="Products" id="pageProducts" type="checkbox" onChange="toggleVisibility('products');"><label for="pageProducts"> Products</label><img id="products" src="images/icon-tick.png"  style="visibility:hidden"/><br/><br>

<input name="checkbox[]" value="Enquiry" id="pageEnquiry" type="checkbox" onChange="toggleVisibility('enquiry');"><label for="pageEnquiry"> Enquiry</label><img id="enquiry" src="images/icon-tick.png"  style="visibility:hidden"/><br/><br>

<input name="checkbox[]" value="Contact_us" id="pageContact" type="checkbox" onChange="toggleVisibility('Contact');"><label for="pageContact">Contact Us</label><img id="Contact" src="images/icon-tick.png"  style="visibility:hidden" /><br/>

php代码

$required_pages = implode(',', $_REQUEST['checkBox']);

$sql="insert into request_quote(customer_name,organisation,phone_num,email,country,state,city,zipcode,project_type,website_url,website_purpose,website_keyword,Competitors,sample_websites,no_of_updation,required_pages,additional_page,other_details)
        values('$customer_name','$organisation','$phone_num','$email','$country','$state','$city','$zipcode','$project_type','$website_url','$website_purpose','$website_keyword','$Competitors','$sample_websites','$no_of_updation','$required_pages','$additional_page','$other_details')";
mysql_query($sql) or die(mysql_error());

原文由 user3422452 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 289
1 个回答

您可以为每个复选框添加 value='YES' 属性。然后创建一个数组,假设您没有选中任何复选框。因为当我们稍后迭代时,只有选中的复选框将通过 $_REQUEST 发送。

 $checkBoxes['customer_name']="NO";
$checkBoxes['organisation']="NO";
$checkBoxes['phone_num']="NO";
$checkBoxes['email']="NO";
$checkBoxes['country']="NO";
$checkBoxes['state']="NO";
$checkBoxes['city']="NO";
$checkBoxes['zipcode']="NO";
$checkBoxes['project_type']="NO";
$checkBoxes['website_url']="NO";
$checkBoxes['website_purpose']="NO";
$checkBoxes['website_keyword']="NO";
$checkBoxes['Competitors']="NO";
$checkBoxes['sample_websites']="NO";
$checkBoxes['no_of_updation']="NO";
$checkBoxes['required_pages']="NO";
$checkBoxes['additional_page']="NO";
$checkBoxes['other_details']="NO";
// Only the checked checkboxes wil be itterated below.
// This will mean the $value would be YES no matter what.
// So I write it literal for SQL-Injection-security
foreach ($_REQUEST['checkbox'] as $checkboxName=>$value){
  $checkBoxes[$checkBoxName]="YES";
}

在您的 SQL 查询中,将变量替换为数组中的项目。 IE

 $sql="insert into request_quote(customer_name)
    values('".$checkBoxes['customer_name']."')";


安全提示:

此外,直接插入用户输入可能会带来巨大的安全风险。对您计划在 SQL 查询中使用的所有用户输入值使用 mysql_real_escape_string($value)


您应该查看 $_POST。 W3Schools 将为您提供帮助: http ://www.w3schools.com/php/php_forms.asp。

原文由 Andreas Storvik Strauman 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题