mtvaran Posted November 12, 2010 Share Posted November 12, 2010 Hi guys, basically here pull out the data from database then creating taxt field automatically and submit into anther table. everything works fine but data not inserting in to the table. could you guys check my code please? <?php $con = mysql_connect("localhost","root",""); mysql_select_db("uni", $con)or trigger_error('MySQL error: ' . mysql_error()); ?> <?php $result = mysql_query("SELECT * FROM course") or trigger_error('MySQL error: ' . mysql_error()); echo '<select name ="cid[]">'; while($row = mysql_fetch_array($result)) { echo '<option value="' . $row['CourseID'] . '">'. $row['CourseID'] .'</option>'; } echo '</select>'; //------------------ ?> <?php if(!empty($_POST["submit"])) { $value = empty($_POST['question']) ? 0 : $_POST['question']; ?> <form name="form1" method="post" action="result.php"> <?php for($i=0;$i<$value;$i++) { echo 'Question NO: <input type="text" name="qno[]" size="2" maxlength="2" class="style10"> Enter Marks: <input type="text" name="marks[]" size="3" maxlength="3" class="style10"><br>'; } ?> <label> <br /> <br /> <input type="submit" name="Submit" value="Submit" class="style10"> </label> </form> <?php } else{ ?> <form method="POST" action="#"> <label> <span class="style10">Enter the Number of Question</span> <input name="question" type="text" class="style10" size="2" maxlength="2"> </label> <input name="submit" type="submit" class="style10" value="Submit"> </form> <?php }?> result.php <?php $con = mysql_connect("localhost","root","") or die('Could not connect: ' . mysql_error()); mysql_select_db("uni",$con) or die('Could not connect: ' . mysql_error()); foreach ($_POST['cid'] as $c) {$cid [] = $c;} foreach($_POST['qno'] as $q){$qno[] = $q;} foreach($_POST['marks'] as $m){$marks[] = $m;} $ct = 0; for($i=0;$i<count($qno);$i++) { $sql="INSERT INTO examquesion (CourseID,QuesionNo,MarksAllocated) VALUES('$cid[$i]','$qno[$i]','$marks[$i]')"; mysql_query($sql,$con) or die('Error: ' . mysql_error()); $ct++; } echo "$ct record(s) added"; mysql_close($con) ?> Link to comment https://forums.phpfreaks.com/topic/218519-inserting-data-from-drop-down-list-text-field/ Share on other sites More sharing options...
BlueSkyIS Posted November 12, 2010 Share Posted November 12, 2010 i would try some basic debugging: is the insert query being called? if yes, what is the query? etc., etc. echo is your friend. Link to comment https://forums.phpfreaks.com/topic/218519-inserting-data-from-drop-down-list-text-field/#findComment-1133612 Share on other sites More sharing options...
mtvaran Posted November 12, 2010 Author Share Posted November 12, 2010 basically im displaying a column field as drop-down list. then ask enter No of question' (if i enter 2 then 2 text field will be created automatically) after i enter data into it. then all data have to be stored into another table. Eg: cid, q#,marks c1,1,50 c1,2,50. here evrything has been displayed well but when i click submit, the data doesn't insert into table Link to comment https://forums.phpfreaks.com/topic/218519-inserting-data-from-drop-down-list-text-field/#findComment-1133621 Share on other sites More sharing options...
BlueSkyIS Posted November 12, 2010 Share Posted November 12, 2010 i would try some basic debugging: echo "before loop <br />"; echo "count(qno): ".count($qno)."<br />"; for($i=0;$i<count($qno);$i++) { echo "in loop. i = $i <br />"; $sql="INSERT INTO examquesion (CourseID,QuesionNo,MarksAllocated) VALUES('$cid[$i]','$qno[$i]','$marks[$i]')"; echo "sql: $sql <br />"; mysql_query($sql,$con) or die('Error: ' . mysql_error()); $ct++; } Link to comment https://forums.phpfreaks.com/topic/218519-inserting-data-from-drop-down-list-text-field/#findComment-1133623 Share on other sites More sharing options...
mtvaran Posted November 12, 2010 Author Share Posted November 12, 2010 no dear, still error. im bit new with php so the problem is i couldn't find error data easily. Link to comment https://forums.phpfreaks.com/topic/218519-inserting-data-from-drop-down-list-text-field/#findComment-1133630 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.