Having probem inserting data into mysql

hi all,
I am facing a problem with this script. i want my users to put in as many data into the table using the addmore function. please do have check on my script

this is the form i am using

<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="<?php echo $length.$loop; ?>" /></div>
<input name="submit" type="submit" value="submit" />
</fieldset>
</form>

this is the Javascript for add more functionality

<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);
}
function removeRow()
{
  var tbl = document.getElementById('tbltest');
  var lastRow = tbl.rows.length;
  if (lastRow > 1) tbl.deleteRow(lastRow - 1);
}
</script>

and this is the php i am using

<?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);
	}
}

?>

i have a hidden field in the form when i set the value to one 1 it lets me put 2 sets of data and if i were to set a value of 10, it lets me put up to 11 sets of data. here’s that part of the code

<input type="hidden" name="len" value="1" />

i would like users to put as many or as little data as they please without any error.
Please help me resolve this issue. Thanks in advance

If I understand correctly you want to add rows on the fly to a html form.

The standard and logical way to work out how to do this is to make a mini-model using straight html and JS - get that working and then recreate the output using PHP.

However, essentially on this line:


	sel.name = 'txtcategory' + iteration;

you probably want to be doing this


	sel.name = 'txtcategory[' + iteration + ']';

in order to create form elements in html which look like this

test.htm


<form action=process.php method=post>
<select name=input[0]>
<option value=1a>pick me!</option>
<option value=2a>OR, pick me!</option>
</select>
<select name=input[0]>
<option value=1b>pick me!</option>
<option value=2b>OR, pick me!</option>
</select>
<input type=submit>
</form>

process.php


<?
var_dump($_POST);

foreach( $_POST['input'] as $key => $value ){
echo "KEY $key is $value " . PHP_EOL ;

}

?>

hi cups,
thanks for your answer, i have tried changing the javascript to what you suggested and the problem persists. i know there are two other things that you have suggested which i couldnt follow. i am a beginner at this. please be kind to explain the last two suggestion, it will help me immensely in my learning as well as resolving the issue at hand.

thanks in advance

I did not knowingly suggest any javascript.

The process you need to understand is that you can send an array of elements with one name from HTML to PHP, iterate through them using PHP and then do things like store them in your Mysql database.

You need to first of all understand that first process without using Javascript, hence my 2 example files test.htm and process.php. Put them in the same folder and play with the form and see what gets echoed out.
test.htm


<form action=process.php method=post>
<select name=input[0]>
<option value=1a>pick me!</option>
<option value=2a>OR, pick me!</option>
</select>
<select name=input[1]> <-Gaaah! was wrong in my initial code
<option value=1b>pick me!</option>
<option value=2b>OR, pick me!</option>
</select>
<input type=submit>
</form>

NEXT, make a really simple table and iterate through those 2 form elements and insert the simple test data into a database.

THEN edit test.htm and try and get your JS to add input[2], input[3] and so on.

If you do that you should prove to yourself why this is what you need to do.


sel.name = 'txtcategory[' + iteration + ']';