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">&nbsp;&nbsp;&nbsp;&nbsp;
        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)
?>

is result.php actually being called and if so, have you tried to see what your queries look out. Try and echo out the $sql variable.

Also are you are looping thru the $qno array, try print_r($qno); to see if there is actually data in there

is result.php actually being called and if so, have you tried to see what your queries look out. Try and echo out the $sql variable.

Also are you are looping thru the $qno array, try print_r($qno); to see if there is actually data in there

im bit new with php so the problem is i couldn't find error data easily.:?:
as you can see 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

Hi,

No need to use array for dropdown name cid[]. use only cid. then it should be work.

-----------------------

<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("uni", $con)or trigger_error('MySQL error: ' . mysql_error());
$result  = mysql_query("SELECT * FROM course") or trigger_error('MySQL error: ' . mysql_error());

if(!empty($_POST["submit"]))
{
 $value = empty($_POST['question']) ? 0 : $_POST['question'];

echo '<form name="form1" method="post" action="result.php">';
echo '<input type="hidden" name="cid" value="'.$_REQUEST['cid'].'">';
 for($i=0;$i<$value;$i++)
 {
  echo 'Question NO: <input type="text" name="qno[]" size="2" maxlength="2" class="style10">&nbsp;&nbsp;&nbsp;&nbsp;
        Enter Marks: <input type="text" name="marks[]" size="3" maxlength="3" class="style10"><br>';
  }

echo '<input type="submit" name="Submit" value="Submit" class="style10">';
echo '</form>';

 }
else{
?>

<form method="POST" action="#">
<?php

echo '<select name ="cid">';
while($row = mysql_fetch_array($result))
{
  echo '<option value="' . $row['CourseID'] . '">'. $row['CourseID'] .'</option>';
}
echo '</select>';


?>

<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());

$cid = $_POST['cid'];
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','$qno[$i]','$marks[$i]')";
 mysql_query($sql,$con) or die('Error: ' . mysql_error());
 $ct++;
 }
echo "$ct record(s) added";
mysql_close($con)
?>

thank you every much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.