I know there are multiple questions here on SO regarding this same issue already and I\’ve looked into them but didn\’t quite get a satisfying answer. So here goes my question,
I have a form which consists of a few textboxes and checkboxes. It looks like this,
The user can select multiple checkboxes. I\’m trying to insert the values(not the displaying text string) of those checkboxes into a MySQL table. It should look like this,
One Service ID(SID) can have multiple Locations(Loc_Code). Those location codes (CO, GQ) are the values of the checkboxes.
I\’ve written this following code so far.
<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>
It inserts the Service details correctly. But when it inserts location data, a problem like this occurs,
I selected 4 checkboxes and saved. The 4 location codes gets saved along with the service ID. But as you can see from the screenshot above, a bunch of empty rows gets inserted too.
My question is how can I stop this from happening? How can I insert the data from the checkboxes only I select?
Thank you.





Rating:
The post php – Inserting multiple checkbox values to MySQL appeared first on Javascript ASK.
via Javascript ASK http://javascriptask.phpfogapp.com/php-inserting-multiple-checkbox-values-to-mysql.html
No comments:
Post a Comment