仅将选定的复选框添加到数据库-PHP [英] Adding only selected check boxes to the database-PHP
问题描述
我具有以下形式:
<form action="done.php" method="POST">
<!-- This hidden check box is used to check if submit is clicked.-->
<input type="hidden" name="check_submit" value="1" />
<!--All the applications are added to an array which we can then use in done.php-->
<applications><h2>Applications</h2><br><table border="0" cellspacing="10" cellpadding="10">
<tr>
<td><b>Application</b></td>
<td><b>UK</b></td>
<td><b>Spain</b></td>
<td><b>France</b></td>
<td><b>Benelux</b></td>
<td><b>Germany</b></td>
<td><b>Sweeden</b></td>
</tr>
<tr>
<td>hey</td>
<td><center><input type = "checkbox" name="hey[]" value ="heyuk"/></center></td>
<td><center><input type = "checkbox" name="hey[]" value ="heyspain"/></center></td>
<td><center><input type = "checkbox" name="hey[]" value ="heyfrance"/></center></td>
<td><center><input type = "checkbox" name="hey[]" value ="heybenelux"/></center></td>
<td><center><input type = "checkbox" name="hey[]" value ="heygermany"/></center></td>
<td><center><input type = "checkbox" name="hey[]" value ="heysweeden"/></center></td>
</tr>
<input type="submit" value="Update">
</submitb>
</form>
然后我的done.php在发布时接收到数据。
Then my done.php receives the data when it is posted.
<?php
// First we execute our common code to connection to the database and start the session
require("common.php");
//Check whether the form has been submitted
if (array_key_exists('check_submit', $_POST)) {
//Converts the new line characters (\n) in the text area into HTML line breaks (the <br /> tag)
if ( isset($_POST['hey']) ) {
$print = implode(', ', $_POST['hey']); //Converts an array into a single string
echo $print;
echo('<br>');
}
因此,到目前为止,这一切都非常有效。但是,我有一个名为hey的数据库,其外观如下:
So this all works perfectly up to now. However, I have a database called hey which looks as follows:
接下来要发生的是,如果为该应用程序选择了一个国家/地区,则会添加一个1到相关字段中的数据库。
What I want to happen next is that if a country is selected for that application, it adds a 1 to the database in the relevant field.
我不确定如何进行此操作,因为我不能保证当前每个元素在数组中的位置。请你能给我建议吗?谢谢。
I am unsure how to proceed with this as I can't guarantee currently what position in the array each element is in. Please could you advise me on this? Thank you.
推荐答案
我在走时遇到了相同的问题,并且解决了以下问题,我使用PDO插入数据库...
I had the same problem while a go and I solve it as follows, I use PDO to Insert to database...
<td><center><input type = "checkbox" name="hey[0][UK]" value ="UK"/></center></td>
<td><center><input type = "checkbox" name="hey[0][SPAIN]" value ="SPAIN"/></center></td>
<td><center><input type = "checkbox" name="hey[0][FRANCE]" value ="FRANCE"/></center></td>
<td><center><input type = "checkbox" name="hey[0][BENELUX]" value ="BENELUX"/></center></td>
<td><center><input type = "checkbox" name="hey[0][GERMANY]" value ="GERMANY"/></center></td>
<td><center><input type = "checkbox" name="hey[0][SWEEDEN]" value ="SWEEDEN"/></center></td>
然后在您的查询中执行以下操作...
Then do the following on your query...
$arr = $_POST["hey"];
try {
// depend on your server setting, you might need this to put it on.
// $glb_connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
//Loop through all row-value from the forms
foreach($arr as $key=>$value){
if(count(array_filter($value)) >0){
$data = array( 'UK' => $value['UK'], 'SPAIN' => $value['SPAIN'], 'FRANCE' => $value['FRANCE'], 'BENELUX' => $value['BENELUX'], 'GERMANY' => $value['GERMANY'], 'SWEEDEN' => $value['SWEEDEN'] );
$query = $glb_conn->prepare("INSERT INTO HEY (UK, SPAIN, FRANCE, BENELUX, GERMANY, SWEEDEN) VALUES (:UK, :SPAIN, :FRANCE, :BENELUX, :GERMANY, :SWEEDEN)");
$query->bindValue(':UK', $value['UK']);
$query->bindValue(':SPAIN', $value['SPAIN']);
$query->bindValue(':FRANCE', $value['FRANCE']);
$query->bindValue(':BENELUX', $value['BENELUX']);
$query->bindValue(':GERMANY', $value['GERMANY']);
$query->bindValue(':SWEEDEN', $value['SWEEDEN']);
$query->execute($data);
}
}
} //end try
catch(PDOException $e) {
echo $e->getMessage();
}
这应该像一种魅力,我已经对其进行了测试。
This should work like a charm, I have already test it.
这篇关于仅将选定的复选框添加到数据库-PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!