0

私は次のフォームを持っています:

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

したがって、これは今まで完全に機能しています。ただし、次のような hey というデータベースがあります。 したがって、これは今まで完全に機能しています。 ただし、次のような hey というデータベースがあります。

次にやりたいことは、そのアプリケーションで国が選択された場合、データベースの関連フィールドに 1 が追加されることです。

現在、各要素が配列内のどの位置にあるかを保証できないため、これをどのように進めればよいかわかりません。これについて教えてください。ありがとうございました。

4

2 に答える 2

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

これは魅力のように機能するはずです。私はすでにテストしています。

于 2013-10-24T15:10:06.180 に答える
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.
}

十分に簡単なはずです。

于 2013-10-24T10:53:26.953 に答える