2012-08-25 136 views
3

我知道这里有很多关于这个问题,关于这个问题,我已经看过他们,但没有得到一个令人满意的答案。所以这里是我的问题,插入多个复选框值到MySQL

我有一个窗体,它由几个文本框和复选框组成。它看起来像这样,

enter image description here

用户可以选择多个复选框。我试图将这些复选框的(不是显示文本字符串)插入到MySQL表中。它应该是这样的,

enter image description here

一个服务ID(SID)可以有多个位置(Loc_Code)。这些位置代码(CO,GQ)是复选框的值。

到目前为止,我已经写了下面的代码。

<html> 
<head> 
</head> 
<body> 
<?php 
require_once("db_handler.php"); 

$conn = iniCon(); 
$db = selectDB($conn); 

/* Generating the new ServiceID */ 
$query = "SELECT SID FROM taxi_services ORDER BY SID DESC LIMIT 1"; 
$result = mysql_query($query, $conn); 
$row = mysql_fetch_array($result); 
$last_id = $row["SID"]; 

$id_letter = substr($last_id, 0, 1); 
$id_num = substr($last_id, 1) + 1; 
$id_num = str_pad($id_num, 3, "0", STR_PAD_LEFT); 
$new_id = $id_letter . $id_num; 

//Selecting locations 
$query = "SELECT Loc_Code, Name FROM districts"; 
$result = mysql_query($query, $conn); 

$count = mysql_num_rows($result); 
?> 

<?php 
if(isset($_POST["savebtn"])) 
{ 
    //inserting the new service information 
    $id = $_POST["sid"]; 
    $name = $_POST["name"]; 
    $cost = $_POST["cost"]; 
    if($_POST["active"] == "on") $active = 1; else $active = 0; 

    $query = "INSERT INTO taxi_services(SID, Name, Cost, Active) VALUES('$id', '$name', '$cost', '$active')"; 
    $result = mysql_query($query, $conn); 

    //inserting the location details 
    for($j = 0; $j < $count; $j++) 
    { 
     $loc_id = $_POST["checkbox2"][$j]; 
     $query = "INSERT INTO service_locations(SID, Loc_Code) VALUES('$id', '$loc_id')"; 
     $result5 = mysql_query($query, $conn); 
    } 

    if (!$result || !$result5) 
    { 
     die("Error " . mysql_error()); 
    } 
    else 
    { 
?> 
     <script type="text/javascript"> 
      alert("Record added successfully!"); 
     </script> 
<?php 
    } 
    mysql_close($conn); 
} 
?> 

<div id="serv"> 
<b>Enter a new taxi service</b> 
<br/><br/> 
    <form name="servForm" action="<?php $PHP_SELF; ?>" method="post" > 
     <table width="300" border="0"> 
      <tr> 
      <td>Service ID</td> 
      <td><input type="text" name="sid" readonly="readonly" value="<?php echo $new_id; ?>" style="text-align:right" /></td> 
      </tr> 
      <tr> 
      <td>Name</td> 
      <td><input type="text" name="name" style="text-align:right" /></td> 
      </tr> 
      <tr> 
      <td>Cost</td> 
      <td><input type="text" name="cost" style="text-align:right" onkeypress="return isNumberKey(event)" /></td> 
      </tr> 
      <tr> 
      <td>Active</td> 
      <td><input type="checkbox" name="active" /></td> 
      </tr> 
     </table> 
</div> 

<div id="choseLoc"> 
Locations <br/><br/> 
    <table border="0"> 
     <?php 
     $a = 0; 
     while($row = mysql_fetch_array($result)) 
      { 
      if($a++ %5 == 0) echo "<tr>"; 
      ?> 
      <td align="center"><input type="checkbox" name="checkbox2[]" value="<?php echo $row['Loc_Code']; ?>" /></td> 
      <td style="text-align:left"><?php echo $row["Name"]; ?></td> 
     <?php 
     if($a %5 == 0) echo "</tr>"; 
      } 
     ?> 
    </table> 
</div> 
<br/> 
<div id="buttons"> 
    <input type="reset" value="Clear" /> <input type="submit" value="Save" name="savebtn" /> 
    </form> 
</div> 

</body> 
</html> 

它正确插入服务的详细信息。但是,当它插入的位置数据,像这样的问题时,

enter image description here

我选择4个复选框和保存。 4个位置代码与服务ID一起保存。但是从上面的屏幕截图中可以看到,还插入了一堆空行。

我的问题是我该如何阻止这种情况发生?我如何才能从复选框插入数据,只有我选择?

谢谢。

+5

**你的代码很容易受到SQL注入**你真的* *应使用[预处理语句(HTTP://计算器。com/a/60496/623041),将您的变量作为参数传递到其中,而这些参数不针对SQL进行评估。如果你不知道我在说什么,或者如何解决它,请阅读[Bobby Tables]的故事(http://stackoverflow.com/questions/332365/xkcd-sql-injection-please-explain) 。 – eggyal

+4

另外,正如[介绍](http://www.php.net/manual/en/intro.mysql.php)所述,在PHP手册中介绍了'mysql_ *'函数:*不推荐使用此扩展名用于编写新的代码。相反,无论是[mysqli](http://www.php.net/manual/en/book.mysqli.php)还是[PDO_MySQL](http://www.php.net/manual/en/ref.pdo应该使用-mysql.php)扩展名。请参阅[MySQL API概述](http://www.php.net/manual/en/mysqlinfo.api.choosing.php)以获得进一步的帮助,以便选择MySQL API。* – eggyal

+0

您是否尝试过检查'$ _POST '变量来检查提交什么? –

回答

8

一种方法是只环比分别提交的复选框:

//inserting the location details 
foreach($_POST["checkbox2"] as $loc_id) 
{ 
    $query = "INSERT INTO service_locations(SID, Loc_Code) VALUES('$id', '$loc_id')"; 
    $result5 = mysql_query($query, $conn); 
} 

我在这里重申上面给出的SQL注入警告:你会最好准备一个INSERT语句,然后执行它带有参数。使用PDO,它看起来像这样:

//inserting the location details 
$stmt = $dbh->prepare(' 
    INSERT INTO service_locations(SID, Loc_Code) VALUES(:id, :loc) 
'); 
$stmt->bindValue(':id', $id); 
$stmt->bindParam(':loc', $loc_id); 
foreach($_POST["checkbox2"] as $loc_id) $stmt->execute(); 
+0

美丽的作品。 :)你能解释一下这一行吗? 'foreach($ _ POST [“checkbox2”]为$ loc_id)'$ loc_id'如何获取复选框的值? |我目前正在学习PDO :)当我掌握它时,我一定会在使用PHP和MySQL时使用它。谢谢。 – Isuru

+0

@ nK0de:阅读['foreach'](http://php.net/manual/en/control-structures.foreach.php)。 – eggyal

-4

发生这种情况的原因是未勾选的复选框仍然发布,它们只有空值。在你插入到service_locations之前,只需检查$loc_id是否为空,如果不是,则只进行插入。

+1

不正确。这是因为循环受到数据库中位置数量的限制。 – eggyal

+1

不,没有勾选的复选框不会被浏览器提交。 –

+0

你们都很正确,我的错误。现在删除按钮在哪里... – Tieran

1

从这些句子:

for($j = 0; $j < $count; $j++) 
    { 
     $loc_id = $_POST["checkbox2"][$j]; 
     $query = "INSERT INTO service_locations(SID, Loc_Code) VALUES('$id', '$loc_id')"; 
     $result5 = mysql_query($query, $conn); 
    } 

我觉得问题是,loc_code的值必须是你最后一次选择的网站位置。因为在这个循环中,loc_code的价值将每次更换。如果要插入的所有位置,你应该把它放在一个句子,像INSERT INTO service_locations(SID,Loc_Code)VALUES( '的$ id', '$ loc_id'),为$ loc_id值应为CO,GQ,GL