我无法从复选框中获取值,以便从下面的代码生成SQL语句

时间:2012-03-16 09:04:48

标签: php mysql

我无法从复选框中获取值,以便从下面的代码中生成SQL语句。我一直在尝试查找代码中的缺陷,但我根本不知道。
< / p>

<?
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 PART
这是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 个答案:

答案 0 :(得分: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操作。这是错误的。
对于你想要的,你应该做这样的事情:

<?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";

}
?>

此外,您必须在每个复选框的HTML代码中使用此方式:

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

顺便说一下,在您的应用中始终使用<?php而不是<?。我保证你的项目在任何地方都没有任何问题。

答案 1 :(得分:0)

一个显而易见的问题是:

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

HTML复选框仅在选中时发布任何值。删除if

答案 2 :(得分:0)

正如人们已经指出的那样,你正在混合使用Javascript和PHP。由于您没有报告此代码会在问题中提供的许多错误消息,因此我猜测您已禁用错误报告 - 您应该在开发时将其调高到最大值,并且只在生产中将其关闭。您可以通过在PHP代码的顶部放置以下行来轻松完成此操作:

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

现在,转到代码:

首先,您需要删除脚本顶部的isset()树。如果选中了复选框,则只会在POST数据中出现一个复选框 - 在执行之前检查它们是否全部设置,代码表示只有在 all 选中复选框时才会执行代码,想象不是你想要的。

接下来,您应该避免将数据库ID硬编码到您的代码中。您可以使用从页面传回代码的那些值,只要在SQL中使用它们之前清理它们即可。因为它们都是整数,所以你只需要将它们转换为整数。

接下来,让我们从SQL中抽象$_POST数组。首先,我们将查看$_POST中的值并构建您在SQL中使用的数据数组,然后我们将构建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:

<!DOCTYPE html>
<html>
...