having probem inserting data into mysql

i have a form with an add-more button which lets users to insert as many data into the database (at least that’s the idea) the problem that i face is that irrespective of how many data is typed in only two sets are inserted into the database. please help me fix this problem

i think the problem is with my javascript or the hidden field value in the form.

Please advice on what i am doing wrong, thanks in advance

please see my code below:

[code]

Add Courses
Management Medical Dental Nursing Ayurvedic Homeopathy Unani Physiotherapy Pharmacology Naturopathy & Yoga Engineering Information Technology Computer / IT Training Institutes Animation Polytechnic Maritime ITI Biotechnology Law Fashion Technology Hotel Management Mass Media Distance Learning Library Science Degree Colleges B.Ed Fine Arts Film Institutes Tutorials Entrance Coaching Vocational Courses Beautician Courses Language Courses Certificate Courses Schools International Schools Private Schools Nursery / Play Homes Flying Institutes Call Center Training Sports Academy 10 + 2 1 - 5000 5001 - 10000 10001 - 20000 20001 - 30000 30001 - 40000 40001 - 50000 50001 - 60000 60001 - 70000 70001 - 80000 80001 - 90000 90001 - 1 Lac 1 Lac - 1.5 Lac 1.5 Lac - 2 Lac 2.5 Lac - 3 Lac 3.5 Lac - 4 Lac 4.5 Lac - 5 Lac 5.5 Lac - 6 Lac 6.5 Lac - 7 Lac 7.5 Lac - 8 Lac 8.5 Lac - 9 Lac 9.5 Lac - 10 Lac 10.5 Lac - 11 Lac 11.5 Lac - 12 Lac 12.5 Lac - 13 Lac 13.5 Lac - 14 Lac 14.5 Lac - 15 Lac 15 Lac & Above
[/code]

and this is my php code used to putting data into mysql database:

[php]<?php
$dbc = mysql_connect(‘localhost’, ‘root’, ‘root’);
if (!$dbc)
{
die('could not connect: '.mysql_error());
}
mysql_select_db (‘a360’, $dbc);
if(isset($_POST[‘submit’]))
{
$length = mysql_real_escape_string($_POST[‘len’]);
//$institute_id= 1;
for($loop=0; $loop<=$length; $loop++)
{
$category = “txtcategory”.$loop;
$course_name = “txtcourse_name”.$loop;
$seat = “txtseat”.$loop;
$fees = “txtfees”.$loop;
$cat = mysql_real_escape_string($_POST[$category]);
$cou = mysql_real_escape_string($_POST[$course_name]);
$sea = mysql_real_escape_string($_POST[$seat]);
$fee = mysql_real_escape_string($_POST[$fees]);
$store = “INSERT INTO institute_courses (course_id, category, course_name, seats, fees) VALUES (NULL, ‘$cat’, ‘$cou’, ‘$sea’, ‘$fee’)”;
$go = mysql_query($store);
}
}
?>[/php]

Hi!

You’re using the hidden field’s value in PHP to determine how many rows you need to insert, but this isn’t incremented/decremented in the javascript.

I have updated the html/javascript code for you.
What I have done is added the line

document.getElementById('len').value = tbl.rows.length;

at the end of the addRow() and removeRow() functions; this make sure the value of the “len” field is always updated to the correct value.
Also I added an id of “len” to the hidden field because I needed a way to refer to it from javascript.

<script type="text/javascript">
	function addRow() {
		var tbl = document.getElementById('tbltest');
		var lastRow = tbl.rows.length;
		// if there's no header row in the table, then iteration = lastRow + 1
		var iteration = lastRow;
		var row = tbl.insertRow(lastRow);

		var cell = row.insertCell(0);
		var sel = document.createElement('select');
		sel.name = 'txtcategory' + iteration;
		sel.id = 'id_str' + iteration;
		sel.onchange = function() {
			streamChange(iteration, this)
		};
		sel.options[0] = new Option('Stream', '');
		sel.options[1] = new Option('Management', 'Management');
		sel.options[2] = new Option('Medical', 'Medical');
		sel.options[3] = new Option('Dental', 'Dental');
		sel.options[4] = new Option('Nursing', 'Nursing');
		sel.options[5] = new Option('Ayurvedic', 'Ayurvedic');
		sel.options[6] = new Option('Homeopathy', 'Homeopathy');
		sel.options[7] = new Option('Unani', 'Unani');
		sel.options[8] = new Option('Physiotherapy', 'Physiotherapy');
		sel.options[9] = new Option('Pharmacology', 'Pharmacology');
		sel.options[10] = new Option('Naturopathy & Yoga', 'Naturopathy & Yoga');
		sel.options[11] = new Option('Engineering', 'Engineering');
		sel.options[12] = new Option('Information Technology',
				'Information Technology');
		sel.options[13] = new Option('Computer / IT Training Institutes',
				'Computer / IT Training Institutes');
		sel.options[14] = new Option('Animation', 'Animation');
		sel.options[15] = new Option('Polytechnic', 'Polytechnic');
		sel.options[16] = new Option('Maritime', 'Maritime');
		sel.options[17] = new Option('ITI', 'ITI');
		sel.options[18] = new Option('Biotechnology', 'Biotechnology');
		sel.options[19] = new Option('Law', 'Law');
		sel.options[20] = new Option('Fashion Technology', 'Fashion Technology');
		sel.options[21] = new Option('Hotel Management', 'Hotel Management');
		sel.options[22] = new Option('Mass Media', 'Mass Media');
		sel.options[23] = new Option('Distance Learning', 'Distance Learning');
		sel.options[24] = new Option('Library Science', 'Library Science');
		sel.options[25] = new Option('Degree Colleges', 'Degree Colleges');
		sel.options[26] = new Option('B.Ed', 'B.Ed');
		sel.options[27] = new Option('Fine Arts', 'Fine Arts');
		sel.options[28] = new Option('Film Institutes', 'Film Institutes');
		sel.options[29] = new Option('Tutorials', 'Tutorials');
		sel.options[30] = new Option('Entrance Coaching', 'Entrance Coaching');
		sel.options[31] = new Option('Vocational Courses', 'Vocational Courses');
		sel.options[32] = new Option('Beautician Courses', 'Beautician Courses');
		sel.options[33] = new Option('Language Courses', 'Language Courses');
		sel.options[34] = new Option('Certificate Course', 'Certificate Course');
		sel.options[35] = new Option('Schools', 'Schools');
		sel.options[36] = new Option('International Schools',
				'International Schools');
		sel.options[37] = new Option('Private Schools', 'Private Schools');
		sel.options[38] = new Option('Nursery / Play Homes',
				'Nursery / Play Homes');
		sel.options[39] = new Option('Flying Institutes', 'Flying Institutes');
		sel.options[40] = new Option('Call Center Training',
				'Call Center Training');
		sel.options[41] = new Option('Sports Academy', 'Sports Academy');
		sel.options[42] = new Option('10 + 2', '10 + 2');
		cell.appendChild(sel);

		var cell2 = row.insertCell(1);
		var el = document.createElement('input');
		el.type = 'text';
		el.name = 'txtcourse_name' + iteration;
		el.id = 'id_cname' + iteration;
		cell2.appendChild(el);

		var cell4 = row.insertCell(2);
		var el = document.createElement('input');
		el.type = 'text';
		el.style.width = '40px';
		el.name = 'txtseat' + iteration;
		el.id = 'id_seats' + iteration;
		cell4.appendChild(el);

		var cell8 = row.insertCell(3);
		var sel = document.createElement('select');
		sel.name = 'txtfees' + iteration;
		sel.id = 'id_elig' + iteration;
		sel.options[0] = new Option('Specify Fees', '');
		sel.options[1] = new Option('1 - 5000', '1 - 5000');
		sel.options[2] = new Option('5001 - 10000', '5001 - 10000');
		sel.options[3] = new Option('10001 - 20000', '10001 - 20000');
		sel.options[4] = new Option('20001 - 30000', '20001 - 30000');
		sel.options[5] = new Option('30001 - 40000', '30001 - 40000');
		sel.options[6] = new Option('40001 - 50000', '40001 - 50000');
		sel.options[7] = new Option('50001 - 60000', '50001 - 60000');
		sel.options[8] = new Option('60001 - 70000', '60001 - 70000');
		sel.options[9] = new Option('70001 - 80000', '70001 - 80000');
		sel.options[10] = new Option('80001 - 90000', '80001 - 90000');
		sel.options[11] = new Option('90001 - 1 Lac', '90001 - 1 Lac');
		sel.options[12] = new Option('1 Lac - 1.5 Lac', '1 Lac - 1.5 Lac');
		sel.options[13] = new Option('1.5 Lac - 2 Lac', '1.5 Lac - 2 Lac');
		sel.options[14] = new Option('2.5 Lac - 3 Lac', '2.5 Lac - 3 Lac');
		sel.options[15] = new Option('3.5 Lac - 4 Lac', '3.5 Lac - 4 Lac');
		sel.options[16] = new Option('4.5 Lac - 5 Lac', '4.5 Lac - 5 Lac');
		sel.options[17] = new Option('5.5 Lac - 6 Lac', '5.5 Lac - 6 Lac');
		sel.options[18] = new Option('6.5 Lac - 7 Lac', '6.5 Lac - 7 Lac');
		sel.options[19] = new Option('7.5 Lac - 8 Lac', '7.5 Lac - 8 Lac');
		sel.options[20] = new Option('8.5 Lac - 9 Lac', '8.5 Lac - 9 Lac');
		sel.options[21] = new Option('9.5 Lac - 10 Lac', '9.5 Lac - 10 Lac');
		sel.options[22] = new Option('10.5 Lac - 11 Lac', '10.5 Lac - 11 Lac');
		sel.options[23] = new Option('11.5 Lac - 12 Lac', '11.5 Lac - 12 Lac');
		sel.options[24] = new Option('12.5 Lac - 13 Lac', '12.5 Lac - 13 Lac');
		sel.options[25] = new Option('13.5 Lac - 14 Lac', '13.5 Lac - 14 Lac');
		sel.options[26] = new Option('14.5 Lac - 15 Lac', '14.5 Lac - 15 Lac');
		sel.options[27] = new Option('15 Lac &amp; Above', '15 Lac &amp; Above');
		cell8.appendChild(sel);

		document.getElementById('len').value = tbl.rows.length;
	}
	function removeRow() {
		var tbl = document.getElementById('tbltest');
		var lastRow = tbl.rows.length;
		if (lastRow > 1)
			tbl.deleteRow(lastRow - 1);

		document.getElementById('len').value = tbl.rows.length;
	}
</script>
</head>

<body>
<form name="addcourses" action="" method="post">
<fieldset><legend>Add Courses</legend>
<table width="500px" border="0" id="tbltest">
	<tr>
		<td><select name="txtcategory0" id="id_str">
			<option value="" selected="selected"></option>
			<option value="Management">Management</option>
			<option value="Medical">Medical</option>
			<option value="Dental">Dental</option>
			<option value="Nursing">Nursing</option>
			<option value="Ayurvedic">Ayurvedic</option>
			<option value="Homeopathy">Homeopathy</option>
			<option value="Unani">Unani</option>
			<option value="Physiotherapy">Physiotherapy</option>
			<option value="Pharmacology">Pharmacology</option>
			<option value="Naturopathy & Yoga">Naturopathy & Yoga</option>
			<option value="Engineering">Engineering</option>
			<option value="Information Technology">Information
			Technology</option>
			<option value="Computer / IT Training Institutes">Computer /
			IT Training Institutes</option>
			<option value="Animation">Animation</option>
			<option value="Polytechnic">Polytechnic</option>
			<option value="Maritime">Maritime</option>
			<option value="ITI">ITI</option>
			<option value="Biotechnology">Biotechnology</option>
			<option value="Law">Law</option>
			<option value="Fashion Technology">Fashion Technology</option>
			<option value="Hotel Management">Hotel Management</option>
			<option value="Mass Media">Mass Media</option>
			<option value="Distance Learning">Distance Learning</option>
			<option value="Library Science">Library Science</option>
			<option value="Degree Colleges">Degree Colleges</option>
			<option value="B.Ed">B.Ed</option>
			<option value="Fine Arts">Fine Arts</option>
			<option value="Film Institutes">Film Institutes</option>
			<option value="Tutorials">Tutorials</option>
			<option value="Entrance Coaching">Entrance Coaching</option>
			<option value="Vocational Courses">Vocational Courses</option>
			<option value="Beautician Courses">Beautician Courses</option>
			<option value="Language Courses">Language Courses</option>
			<option value="Certificate Courses">Certificate Courses</option>
			<option value="Schools">Schools</option>
			<option value="International Schools">International Schools</option>
			<option value="Private Schools">Private Schools</option>
			<option value="Nursery / Play Homes">Nursery / Play Homes</option>
			<option value="Flying Institutes">Flying Institutes</option>
			<option value="Call Center Training">Call Center Training</option>
			<option value="Sports Academy">Sports Academy</option>
			<option value="10 + 2">10 + 2</option>
		</select></td>
		<td><input type="text" name="txtcourse_name0" id="id_cname"
			value="" /></td>
		<td><input type="text" name="txtseat0" id="id_seats" value=""
			style="width: 40px;" /></td>
		<td><select name="txtfees0" id="id_elig">
			<option value="" selected="selected"></option>
			<option value="1 - 5000">1 - 5000</option>
			<option value="5001 - 10000">5001 - 10000</option>
			<option value="10001 - 20000">10001 - 20000</option>
			<option value="20001 - 30000">20001 - 30000</option>
			<option value=">30001 - 40000">30001 - 40000</option>
			<option value="40001 - 50000">40001 - 50000</option>
			<option value="50001 - 60000">50001 - 60000</option>
			<option value="60001 - 70000">60001 - 70000</option>
			<option value="70001 - 80000">70001 - 80000</option>
			<option value="80001 - 90000">80001 - 90000</option>
			<option value="90001 - 1 Lac">90001 - 1 Lac</option>
			<option value="1 Lac - 1.5 Lac">1 Lac - 1.5 Lac</option>
			<option value="1.5 Lac - 2 Lac">1.5 Lac - 2 Lac</option>
			<option value="2.5 Lac - 3 Lac">2.5 Lac - 3 Lac</option>
			<option value="3.5 Lac - 4 Lac">3.5 Lac - 4 Lac</option>
			<option value="4.5 Lac - 5 Lac">4.5 Lac - 5 Lac</option>
			<option value="5.5 Lac - 6 Lac">5.5 Lac - 6 Lac</option>
			<option value="6.5 Lac - 7 Lac">6.5 Lac - 7 Lac</option>
			<option value="7.5 Lac - 8 Lac">7.5 Lac - 8 Lac</option>
			<option value="8.5 Lac - 9 Lac">8.5 Lac - 9 Lac</option>
			<option value="9.5 Lac - 10 Lac">9.5 Lac - 10 Lac</option>
			<option value="10.5 Lac - 11 Lac">10.5 Lac - 11 Lac</option>
			<option value="11.5 Lac - 12 Lac">11.5 Lac - 12 Lac</option>
			<option value="12.5 Lac - 13 Lac">12.5 Lac - 13 Lac</option>
			<option value="13.5 Lac - 14 Lac">13.5 Lac - 14 Lac</option>
			<option value="14.5 Lac - 15 Lac">14.5 Lac - 15 Lac</option>
			<option value="15 Lac &amp; Above">15 Lac &amp; Above</option>
		</select></td>
	</tr>
</table>
<div><input type="button" onclick="addRow();" value="Add More" />
<input type="button" value="Remove" onclick="removeRow();" /> <input
	type="hidden" name="len" value="1" id="len" /></div>
<input name="submit" type="submit" value="submit" /></fieldset>
</form>

Hope this helps.

Cheers!

Sponsor our Newsletter | Privacy Policy | Terms of Service