且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-09-03 21:57:46

您可以添加值='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.