I have a problem with this code. I am not sure if it's jquery's or php's problem.
I am using jquery and php to implement [Add More..]
first I'll list the code:
here is my jquery function:
<script type="text/javascript">
$(function() {
var counter = 0;
$('#add_industry').click(function() {
counter++;
var newField = $('#cboIndustry').clone().
attr({id: 'cboIndustry_' + counter,
// name: 'cboIndustry_' + counter});
name: 'cboIndustry[]'});
$('#container').append(newField);
});
});
</script>
then here:
<tr>
<td valign="middle">Industry</td>
<td valign="middle">:</td>
<td valign="middle">
<div id="container">
<select id="cboIndustry" name="cboIndustry" style="width: 100%" required>
<option value="" selected>[Industry..]</option>
<?php
$mysql_command = "CALL sp_populate_industry()";
$mysql_query = $mysql_connection->prepare($mysql_command);
$mysql_query->execute();
while($mysql_row = $mysql_query->fetch())
{
?>
<option value="<?php echo $mysql_row['industry_id']; ?>" <?php if ($mysql_row['industry_id'] == $company_industry) { echo 'selected'; } ?>><?php echo $mysql_row['industry_name']; ?></option>
<?php } ?>
</select>
</div>
</td>
</tr>
<tr>
<td valign="middle"></td>
<td valign="middle"></td>
<td valign="middle"><p id="add_industry"><a href="javascript:void(0)"><span>Add Industry</span></a></p></td>
</tr>
and in my php code I am inserting into mysql using:
if ($_POST['cboIndustry'])
{
foreach ( $_POST['cboIndustry'] as $key => $value )
{
$mysql_query = $mysql_connection->prepare('CALL sp_add_new_company_industry(:param_member_guid, :param_company_guid, :param_company_industry, :param_created_ip_address)');
$mysql_query->bindParam(':param_member_guid', $_SESSION["xoompage_member_guid"], PDO::PARAM_STR);
$mysql_query->bindParam(':param_company_guid', $company_guid, PDO::PARAM_STR);
$mysql_query->bindParam(':param_company_industry', $value, PDO::PARAM_STR);
$mysql_query->bindParam(':param_created_ip_address', $_SERVER['REMOTE_ADDR'], PDO::PARAM_STR);
$mysql_query->execute();
}
}
now the problem is... if I am adding two industries then only one will be inserted in the database and if I add 10 only 9 will be added.. why?
Thanks,
Jassim