2013-10-24 38 views
0

我有以下形式:只添加選中的複選框到數據庫,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接收數據時,它被張貼。

<?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>'); 
    } 

所以這一切都完美的工作到現在。但是,我有一個名爲哎數據庫,如下所示: So this all works perfectly up to now. However, I have a database called hey which looks as follows:

我想接下來發生的是,如果選擇該應用程序的一個國家,它增加了一個1到相關領域的數據庫。

我不確定如何處理這個請按我不能保證什麼當前數組每個元素都是在位置,請你能告訴我這個?謝謝。

回答

2

我而走同樣的問題,我解決它,如下所示,我使用PDO來插入到數據庫...

<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> 

然後執行以下您的查詢...

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

這應該像一個魅力,我已經測試它。

+0

非常感謝你。 – Programatt

+0

非常歡迎,我是空地,我可以幫忙! –

+0

對不起,再次發佈 - 這拋出未定義的變量:價值爲我的任何想法? – Programatt

0

只需將國家/地區名稱指定爲複選框的值即可。當你發佈這樣的表單時,作爲值你會得到只有選定國家的數組(複選框),所以稍後你只需要更新合適的列。 所以你的形式checkobxes輸入應聲明如下

<td><center><input type = "checkbox" name="hey[]" value ="SPAIN"/></center></td> 

西班牙等

$updateClause = ""; // this is the part betwean `SET` and `WHERE` ofc. 
foreach($country as $hey){ // hey would be an array of selected country names. 
    $updateClause.="$country = $country+1" // dont forget to add `,` or you will get SQL syntax error. 
} 

應該很容易足夠多。

+0

對不起,我不太明白這一點。我非常喜歡初學者。編輯了 – Programatt

+0

。現在檢查新內容。 – Antoniossss

相關問題