-1

I can post my form into database but I'm getting only 0 as value for the select tag, how can I get value for each select option in my database?

<select class="span6" name="test_name">
<?php
include ('include/connect.php');

$query1 = "SELECT test_name FROM mst_test ORDER BY test_name ASC";
$result = mysql_query($query1);

while(list($test_name)=mysql_fetch_array($result))
{
  echo "<option value ='$test_name' selected>$test_name</option>";
}
?>
</select>
Ivan
  • 34,531
  • 8
  • 55
  • 100

2 Answers2

0
<select class="span6" name="test_name">
  <?php
  include ('include/connect.php');
  $query1 = "SELECT test_name FROM mst_test ORDER BY test_name ASC";
  $result = mysql_query($query1);
  while ($row = mysql_fetch_array($result)) {?>
    <option value ="<?php echo $row['test_name'];?>"><?php echo $row['test_name'];?></option>
  <?php }?>
</select>

NOTE: [The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead ]

Community
  • 1
  • 1
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
  • Pls whats the difference between using the mysql and mysqli syntax, is it just the character changes or there is more to it? – Adeyeun Imoleayo Aug 18 '16 at 10:58
  • Pls am still getting only 0 inside my db, dou my db column is int cos i only want the numeral value for each selected. What am i missing pls? – Adeyeun Imoleayo Aug 18 '16 at 11:03
  • this is my insert script if($_POST[submit]=='submit' || strlen($_POST['test_name'])>0 ) $msg=array(); foreach($_REQUEST as $key=>$val){ $$key=$val; } include ('include/connect.php'); $sql="INSERT INTO mst_question (test_id,que_desc,ans1,ans2,ans3,ans4,true_ans) VALUES ('$test_name','$question','$ans_1','$ans_2','$ans_3','$ans_4','$correct_ans')"; $result= mysql_query($sql) or die (mysql_error()); $res='Question Successfully Created.'; } – Adeyeun Imoleayo Aug 18 '16 at 11:21
0
<select class="span6" name="test_name">
<option value="">Please select</option>
  <?php
  include ('include/connect.php');
  $query1 = "SELECT test_name FROM mst_test ORDER BY test_name ASC";
  $result = mysql_query($query1);
  while ($test_name = mysql_fetch_assoc($result)) {

    echo "<option value='{$test_name['test_name']}'>{$test_name['test_name']}</option>";
  }
  ?>
</select>

I strongly suggest that you switch to MySQLi or PDO

Ivan
  • 34,531
  • 8
  • 55
  • 100
Saurabh
  • 776
  • 1
  • 5
  • 15