我知道這裏有很多關於這個問題,關於這個問題,我已經看過他們,但沒有得到一個令人滿意的答案。所以這裏是我的問題,插入多個複選框值到MySQL
我有一個窗體,它由幾個文本框和複選框組成。它看起來像這樣,
用戶可以選擇多個複選框。我試圖將這些複選框的值(不是顯示文本字符串)插入到MySQL表中。它應該是這樣的,
一個服務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>
它正確插入服務的詳細信息。但是,當它插入的位置數據,像這樣的問題時,
我選擇4個複選框和保存。 4個位置代碼與服務ID一起保存。但是從上面的屏幕截圖中可以看到,還插入了一堆空行。
我的問題是我該如何阻止這種情況發生?我如何才能從複選框插入數據,只有我選擇?
謝謝。
**你的代碼很容易受到SQL注入**你真的* *應使用[預處理語句(HTTP://計算器。com/a/60496/623041),將您的變量作爲參數傳遞到其中,而這些參數不針對SQL進行評估。如果你不知道我在說什麼,或者如何解決它,請閱讀[Bobby Tables]的故事(http://stackoverflow.com/questions/332365/xkcd-sql-injection-please-explain) 。 – eggyal
另外,正如[介紹](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
您是否嘗試過檢查'$ _POST '變量來檢查提交什麼? –