douci1677 2015-09-08 18:20
浏览 25
已采纳

选项输入形式不通过

I am trying to use a form to fill my db. I use a table from the database to fill the options in the form and it works, but the value is not being passed through, I ran through my code and can't find a typo, is there an error I am not seeing?

<?php
require("db.php");
?>
<html>
   <head>
      <title>Insert a Record in MySQL Database</title>
   </head>

   <body>
      <?php
         if(isset($_POST['insert']))
         {            
            $user_added = $_POST['user_added']; 
            $age_added = $_POST['age_added'];

            $insert_query = "INSERT INTO userList ";
            $insert_query .= "(user, Age) ";
            $insert_query .= "VALUES ";
            $insert_query .= "('$user_added', '$age_added')";

            $retval = mysqli_query($connection, $insert_query);

            if(!$retval )
            {
                 die ("The error is: " . mysqli_error($connection));
            }
            else
            {
            echo "Database Updated " . $age_added . " " . $user_added;
            }
         }
         else
         {
            ?>
               <form method="post" action="<?php $_PHP_SELF ?>">
                  <table width="400" border="0" cellspacing="1" cellpadding="2">
                     <tr>
                        <td width="100">User to Add</td>
                        <td><input name="user_added" type="text" id="user_added"></td>
                     </tr>
                     <tr>
                        <td width="100">Age</td>
                        <td><select name="age_added" id="age_added">
                        <?php
                            while ($selection = mysqli_fetch_assoc($age_query))
                            {
                                echo "<option value=\"{$selection['id']}\">{$selection['age']}</option>
";
                            }
                            ?>
                            </select>
                        </td>
                     </tr>
                     <tr>
                        <td width="100"> </td>
                        <td>
                           <input name="insert" type="submit" id="insert" value="Insert">
                        </td>
                     </tr>                  
                  </table>
               </form>
            <?php
         }
      ?>
   </body>
</html>
<?php
    mysqli_close($connection);
?>

The output if I type Test in the user and select a number in the age, the age is not sent through, and it shows Database Updated Test with no age. What am I missing?

----- ADDED INFORMATION 1 -----

This is the query that creates the $age_query as requested

<?php
//Perfom DB query
$query = "SELECT * FROM PermittedAges ";
$query .= "ORDER BY Age asc";
$age_query = mysqli_query($connection, $query);

//Test for query error
if (!$age_query) {
    die ("Database query failed.");
}
?>

But I am not sure how this could be the problem since this only populates the options in the drop-down field an it works

----- ADDED INFORMATION 2 -----

Here is the var_dump($_POST) output

array(3) { ["user_added"]=> string(4) "Test" ["Age_added"]=> string(0) "" ["insert"]=> string(6) "Insert" }
  • 写回答

2条回答 默认 最新

  • duanjiwu0324 2015-09-09 13:08
    关注

    @Alex thank you. I realized my mistake was in my syntax. The value was not being passed and I changed it from

    echo "<option value=\"{$selection['id']}\">{$selection['age']}</option> ";

    to

    echo "<option value=\"{$selection['age']}\">{$selection['age']}</option> ";

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示