2012-03-16 10 views
-1

以下のコードからSQL文を生成するためにチェックボックスから値を取得できません
私のコードでこの欠陥を探していますしかし、私はまったく考えていません。
以下のコードからSQL文を生成するためにチェックボックスから値を取得できません

<? 
session_start(); 

if(isset($_POST['swimming'])&& 
    isset($_POST['driving'])&& 
    isset($_POST['cooking'])&& 
    isset($_POST['cycling'])&& 
    isset($_POST['bacom'])&& 
    isset($_POST['baacc'])) 
{ 

$sql = "SELECT position_id FROM `jobskill_info` 
    where skill_id in (1"; 

if (document.match.swimming.checked) { 
    $sql .= ",10"; 
}elseif (document.match.driving.checked) { 
    $sql .= ",11"; 
}elseif (document.match.cooking.checked) { 
    $sql .= ",12"; 
}elseif (document.match.cycling.checked) { 
    $sql .= ",13"; 
}elseif (document.match.bacom.checked) { 
    $sql .= ",14"; 
}elseif (document.match.baacc.checked) { 
    $sql .= ",15"; 
} 

$count = 0; 

if (document.match.swimming.checked) { 
    $count = $count+1; 
}elseif (document.match.driving.checked) { 
    $count = $count+1; 
}elseif (document.match.cooking.checked) { 
    $count = $count+1; 
}elseif (document.match.cycling.checked) { 
    $count = $count+1; 
}elseif (document.match.bacom.checked) { 
    $count = $count+1; 
}elseif (document.match.baacc.checked) { 
    $count = $count+1; 
} 

$sql .= ") group by position_id having count(*) = ".$count.""; 

echo "$sql"; 
} 

?> 

HTML部分
は、ここで私は、チェックボックスから値を取得したいHTMLの一部です。

<html> 
<title>Matching Skill Systems</title> 
<form name=match method=post action=test2.php onSubmit="return checkData()"> 
<table width=100% border=1 cellpadding="20"> 
    <tr bgcolor=red> 
     <td colspan=3><font size=10 color=orange><center></center></font></td> 
    </tr> 
    <tr height= 300px> 
     <td width=200px></td> 
     <td><center><h1>Matching Skills</h1><br> 
       <table border=3> 
       <tr> 
        <td colspan=5><center>Personal Skill</center></td> 
       </tr> 
       <tr> 
        <td></td> 
        <td><input type=checkbox name=swimming value="10" id=swimming >Swimming</td> 
        <td><input type=checkbox name=driving value="11" id=driving >Driving</td> 
        <td><input type=checkbox name=cooking value="12" id=cooking >Cooking</td> 
        <td></td> 
       </tr> 
       <tr> 
        <td></td> 
        <td><input type="checkbox" name=cycling value="13" id=cycling>Cycling</td> 
        <td><input type=checkbox name=bacom value="14" id=bacom>Basic computer</td> 
        <td><input type=checkbox name=baacc value="15" id=baacc>Basic accounting</td> 
        <td></td> 
       </tr> 
       </table><br> 
       <input type=submit name=match value=Matching> 
       <input type=button value=Back onclick="location.href='applicantprofile.php'"><br> 
       <br> 
       <table border=5> 
        <tr> 
         <td width=400px><center><h4>Job Decription</h4></center></td> 
         <td width=150px><center><h4>Available Position</h4></center></td> 
        </tr> 
       </table> 
      </center> 
     </td> 
     <td width=200px></td> 
    </tr> 
</table> 
</form> 
</html> 
+3

.. if(document.match.swimming.checked)$ sql。= "、10"; ' - if文はjavascriptで、連結はphpです – mishu

+0

ところで、' isset'は複数の引数を受け付けます: 'isset($ _ POST ['swimming ']、$ _POST [' driving ']、$ _POST [' cooking ']、...) ' – deceze

答えて

0

コードに大きな間違いがあるようです。
JavaScriptとPHPを間違った方法で組み合わせました。

if (document.match.swimming.checked) { 
    $sql .= ",10"; 
}elseif (document.match.driving.checked) { 
    $sql .= ",11"; 
}elseif (document.match.cooking.checked) { 
    $sql .= ",12"; 
}elseif (document.match.cycling.checked) { 
    $sql .= ",13"; 
}elseif (document.match.bacom.checked) { 
    $sql .= ",14"; 
}elseif (document.match.baacc.checked) { 
    $sql .= ",15"; 
} 

あなたはJavaScriptのIF文を持って、各ブロックのためにあなたは、PHPのアクションを持っている:あなたはこれを行うことはできません。これは間違っています。また、各チェックボックスのためにあなたのHTMLコードでこの方法に従わなければならない

<?php 
session_start(); 

if(isset($_POST['swimming'])&& 
    isset($_POST['driving'])&& 
    isset($_POST['cooking'])&& 
    isset($_POST['cycling'])&& 
    isset($_POST['bacom'])&& 
    isset($_POST['baacc'])) 
{ 
    $sql = "SELECT position_id FROM `jobskill_info` where skill_id in (1"; 

    if ($_POST['swimming'] == '1') { 
     $sql .= ",10"; 
    } elseif ($_POST['driving'] == '1') { 
     $sql .= ",11"; 
    } // Next lines in this way ... 


    $sql .= ") group by position_id having count(*) = ".$count.""; 

    echo "$sql"; 

} 
?> 

<input type=checkbox name="swimming" value="1" id="swimming" />Swimming 

ところで、代わりに常に<?phpを使い、あなたが欲しいもののために
は、あなたがこのような何かを行う必要がありますあなたのアプリケーションでは<?です。私はあなたのプロジェクトがどこでも問題なく働くことを保証します。

0

1つの明白な問題は、このいずれかになります。それがチェックされている場合

if(isset($_POST['swimming'])&& 
    isset($_POST['driving'])&& 
    isset($_POST['cooking'])&& 
    isset($_POST['cycling'])&& 
    isset($_POST['bacom'])&& 
    isset($_POST['baacc'])) 

HTMLチェックボックスのみどんな値をポストします。 ifを削除します。

0

あなたはすでに人々が指摘しているように、JavascriptとPHPを混在させています。このコードがあなたに与える多くのエラーメッセージを報告しないので、エラー報告が無効になっていると推測しています。開発中は最大にして、運用中はオフにしてください。コードの上、

error_reporting(E_ALL); 
ini_set('display_errors', 1); 

今:あなたが簡単にあなたのPHPコードの先頭に次の行を配置することによって、これを行うことができます。まず

、あなたはの上部にそのisset()ツリーを削除する必要がありますスクリプト。チェックボックスがチェックされている場合にのみチェックボックスが表示されます。実行前にすべて設定されているかどうかをチェックし、コードはコードが実行されることを意味します。すべてチェックボックスがチェックされています。欲しいです。

次に、コードにデータベースIDをハードコードするのは避けてください。 SQLで使用する前にそれらをサニタイズするかぎり、コードから渡された値をページから使用することができます。それらはすべて整数なので、整数にキャストするだけです。

次に、$_POSTの配列をSQLから抽象化しましょう。まず、$_POSTの値を見て、SQLで使用するデータの配列を作成します。、次にを作成します。これにより、可読性と保守性が大幅に向上します。

このPHPコードを試してみてください。

<?php 

    // Always use a full <?php tag, the shortened <? is not widely supported 

    session_start(); 

    // An array of all the skill_id values we will search for 
    $skillIds = array(1); 

    // An array of all the checkbox names 
    $skillChecks = array (
    'swimming', 
    'driving', 
    'cooking', 
    'cycling', 
    'bacom', 
    'baacc' 
); 

    // Loop the checks and get the values from $_POST 
    foreach ($skillChecks as $skill) { 
    if (!empty($_POST[$skill])) { 
     $skillIds[] = (int) $_POST[$skill]; // Cast them to ints so they are SQL-safe 
    } 
    } 

    // Get the value for $count 
    $count = count($skillIds) - 1; 

    // Build the query 
    $sql = " 
    SELECT `position_id` 
    FROM `jobskill_info` 
    WHERE `skill_id` IN (".implode(", ", $skillIds).") 
    GROUP BY `position_id` 
    HAVING count(*) = $count 
    "; 

    echo "$sql"; 

してくださいまた、HTML内のすべての属性値が引用されるべきことに注意してください。だから、:

<td><input type=checkbox name=swimming value="10" id=swimming >Swimming</td> 

は...次のようになります。

<td><input type="checkbox" name="swimming" value="10" id="swimming">Swimming</td> 

はまた、HTMLドキュメントの先頭にDOCTYPEを追加する必要があります:あなたはPHPとJavaScriptを混合している

<!DOCTYPE html> 
<html> 
... 
関連する問題