如何使用PHP将多个复选框值插入数据库 [英] how insert the multiple checkbox values into database using php

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

问题描述

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

By this code i can able insert the checkbox values to database. But i need add one column in phpmyadmin and in that column i need store the values like, if i select 2 checkbox, i need store that ckeckbox values in one column in another column i need store for selected checkbox as YES and unselected values as NO. But see i want both column

这是我的代码

<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());


推荐答案

您可以添加值='YES'属性分配给您的每个复选框。
然后创建一个数组,假设您未选中任何复选框。因为当我们稍后进行迭代时,只有选中的复选框会通过 $ _ REQUEST 发送。

You can add a value='YES' attribute to each of your checkboxes. Then make an array where you assume no checkbox has been checked. Because when we iterate later, only the checked checkboxes will be sent via $_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

And in your SQL-Query, replace the variables with items from the array. I.E.

$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

You should look into $_POST. W3Schools will help you out: http://www.w3schools.com/php/php_forms.asp.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆