通过此代码,我可以将复选框值插入数据库。但是我需要在 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 许可协议
您可以为每个复选框添加
value='YES'
属性。然后创建一个数组,假设您没有选中任何复选框。因为当我们稍后迭代时,只有选中的复选框将通过$_REQUEST
发送。在您的 SQL 查询中,将变量替换为数组中的项目。 IE
安全提示:
此外,直接插入用户输入可能会带来巨大的安全风险。对您计划在 SQL 查询中使用的所有用户输入值使用
mysql_real_escape_string($value)
。您应该查看 $_POST。 W3Schools 将为您提供帮助: http ://www.w3schools.com/php/php_forms.asp。