Jump to content

inserting data from drop-down list & text field


mtvaran

Recommended Posts

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

 

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

 

 

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++;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.