如何使用php将多个复选框值插入数据库
通过这段代码,我可以将复选框值插入数据库。但是我需要在phpmyadmin中添加一列,在该列中我需要存储值,例如,如果我选中2个复选框,我需要将该复选框值存储在一列中,在另一列中,我需要将选中复选框的值存储为是,将未选中的值存储为否。但是,请参见我想要这两个列 这是我的密码如何使用php将多个复选框值插入数据库,php,mysql,sql,checkbox,Php,Mysql,Sql,Checkbox,通过这段代码,我可以将复选框值插入数据库。但是我需要在phpmyadmin中添加一列,在该列中我需要存储值,例如,如果我选中2个复选框,我需要将该复选框值存储在一列中,在另一列中,我需要将选中复选框的值存储为是,将未选中的值存储为否。但是,请参见我想要这两个列 这是我的密码 <input type="checkbox" name="checkbox[]" value="Home" id="pageHome" onChange="toggleVisibility('home');" />
<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/>
您可以向每个复选框添加
value='YES'
属性。
然后创建一个数组,假设没有选中复选框。因为当我们稍后迭代时,只有选中的复选框将通过$\u请求发送
$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查询中,将变量替换为数组中的项。
即
安全提示:
此外,直接插入用户输入可能会带来巨大的安全风险。对计划在SQL查询中使用的所有用户输入值使用mysql\u real\u escape\u string($value)
你应该查看$u POST。W3Schools将帮助您:。您必须使用3个表格,并相互关联
表1->客户(id、姓名等)
表2->所需页面(id、名称)
-在这里,您将从复选框(主页、关于我们等)添加选项
表3->customers\u required\u pages(id、customer\u id、required\u pages\u id)
保存数据时,必须按顺序保存:
您的客户:
$sql = "INSERT INTO customers (field1, field2, fieldn...)"
$qry = mysql_query($sql);
检索您的客户Id:
$sql = "SELECT LAST_INSERT_ID() AS customerId";
$qry = mysql_query($sql);
$customerId = mysql_fetch_assoc($qry);
保存客户与所需页面之间的关系
<?php
foreach ($_REQUEST['checkBox'] as $oneCheckbox => $checkboxValue) {
$sql = "INSERT INTO customers_required_pages (customer_id, required_page_id) VALUES ({$customerId['customerId']}, '{$checkboxValue}')";
}
?>
作为一个额外的部分,考虑使用MySQL或PDO而不是MySQL作为数据库库,因为MySQL LIB的使用已经被阻止了
< P>我有问题将多重复选框存储到MySQL表中,但是现在它用这个代码工作得很好:
<html>
<head>
<title>Registration Page</title>
</head>
<body>
<?php
require("config.php");
?>
<?php
if(isset($_POST['submit'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$gender = $_POST['gender'];
$hobbies = implode(",",$_POST['hobbies']);
echo $hobbies;
$country = $_POST['country'];
// echo $hobbies;
//foreach($hobbies as $hobbies1){
//echo $hobbies1;
//$implode = implode(',',$hobbies1);
$sql = "INSERT INTO `userinfo`(`fname`,`lname`,`gender`,`hobbies`,`country`)
values('$fname','$lname','$gender','$hobbies','$country')";
//}
//$sql = "INSERT INTO `userinfo`(`fname`,`lname`,`gender`,`hobbies`,`country`)
// values('$fname','$lname','$gender','$hobbies1','$country')";
$query = mysql_query($sql) or die(mysql_error());
if
($query){
echo "<h1>Data inserted .. Happy coding</h1>";
}else{
echo "<h1>Please check the problem</h1>";
}
}
//}
?>
<form name="registration" method="post" action="">
<table border="1" cellpadding="2" cellspacing="2" align="center">
<tr>
<td>First Name:</td><td><input type="text" name="fname" required=""></td>
</tr>
<tr>
<td>Last Name:</td><td><input type="text" name="lname" required=""></td>
</tr>
<tr>
<td>Gender:</td><td><input type="radio" name="gender" value="male" required="">Male <input type="radio" name="gender" value="female" >Female</td>
</tr>
<tr>
<td>Hobbies:</td><td><input type="checkbox" name="hobbies[]" value="tennis">Tennis <br>
<input type="checkbox" name="hobbies[]" value="cricket">Cricket <br>
<input type="checkbox" name="hobbies[]" value="dance">Dance7 <br>
<input type="checkbox" name="hobbies[]" value="sketching">Sketching
</td>
</tr>
<tr>
<td>Country:</td>
<td>
<select name="country">
<option >----select----</option>
<option>India</option>
<option>Pakistan</option>
<option>UAE</option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Register me"></td>
</tr>
</table>
</form>
</body>
</html>
注册页
请把你的问题分成两个以上的句子和一个较长的句子。很难理解:)如何通过$复选框
-数组将循环值插入数据库。正如我刚才所说(就在安全提示的正上方):)您可以看到,$checkbox数组相当于代码中的$required_页面。但我需要数据库中的两列。一列具有复选框值,另一列具有“是”或“否”复选框值为“是”或“否”。然而,复选框名称是在SQL查询中插入到请求(
)之后设置的。我只是懒得添加所有字段:)这是您问题的解决方案;你们并没有解释这是如何应用于,或者如何解决这个问题的。
<?php
foreach ($_REQUEST['checkBox'] as $oneCheckbox => $checkboxValue) {
$sql = "INSERT INTO customers_required_pages (customer_id, required_page_id) VALUES ({$customerId['customerId']}, '{$checkboxValue}')";
}
?>
$sql = "SELECT rp.id, rp.name, IF(crp.customer_id IS NULL, 'NO', 'YES') AS checked FROM required_pages rp LEFT JOIN customers_required_pages crp ON rp.id = crp.required_page_id";
<html>
<head>
<title>Registration Page</title>
</head>
<body>
<?php
require("config.php");
?>
<?php
if(isset($_POST['submit'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$gender = $_POST['gender'];
$hobbies = implode(",",$_POST['hobbies']);
echo $hobbies;
$country = $_POST['country'];
// echo $hobbies;
//foreach($hobbies as $hobbies1){
//echo $hobbies1;
//$implode = implode(',',$hobbies1);
$sql = "INSERT INTO `userinfo`(`fname`,`lname`,`gender`,`hobbies`,`country`)
values('$fname','$lname','$gender','$hobbies','$country')";
//}
//$sql = "INSERT INTO `userinfo`(`fname`,`lname`,`gender`,`hobbies`,`country`)
// values('$fname','$lname','$gender','$hobbies1','$country')";
$query = mysql_query($sql) or die(mysql_error());
if
($query){
echo "<h1>Data inserted .. Happy coding</h1>";
}else{
echo "<h1>Please check the problem</h1>";
}
}
//}
?>
<form name="registration" method="post" action="">
<table border="1" cellpadding="2" cellspacing="2" align="center">
<tr>
<td>First Name:</td><td><input type="text" name="fname" required=""></td>
</tr>
<tr>
<td>Last Name:</td><td><input type="text" name="lname" required=""></td>
</tr>
<tr>
<td>Gender:</td><td><input type="radio" name="gender" value="male" required="">Male <input type="radio" name="gender" value="female" >Female</td>
</tr>
<tr>
<td>Hobbies:</td><td><input type="checkbox" name="hobbies[]" value="tennis">Tennis <br>
<input type="checkbox" name="hobbies[]" value="cricket">Cricket <br>
<input type="checkbox" name="hobbies[]" value="dance">Dance7 <br>
<input type="checkbox" name="hobbies[]" value="sketching">Sketching
</td>
</tr>
<tr>
<td>Country:</td>
<td>
<select name="country">
<option >----select----</option>
<option>India</option>
<option>Pakistan</option>
<option>UAE</option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Register me"></td>
</tr>
</table>
</form>
</body>
</html>