如何从数据库中的下拉菜单中插入id

时间:2015-03-11 05:30:55

标签: php mysql sql mysqli

我有2个表类别和子类别。我希望从下拉列表中选择一个类别后,将子类别添加到数据库中。在子类别表中,我能够保存catname和subcatname,但是我也希望在子类别表中保存与所选catname相对应的catid(类别表的自动增量id),而不向用户显示。任何人都可以告诉我如何做到这一点

类别表

id catname
1    C1
2    C2

子类别表

id  catid   catname   subcatname
1            C1        S1

添加子类别的表单

<form action="a_insert_subcategory.php" method="post">
    <label>Category</label>
        <select name="catname">
            <?php
                require 'connection.php';
                $sql = "SELECT * FROM category";
                $result = mysqli_query($con, $sql);
                if (mysqli_num_rows($result) > 0) 
                    {
                        while($row = mysqli_fetch_assoc($result)) 
                            {
                                $catname=$row["catname"];
            ?>

            <option value="<?echo $catname;?>"><? echo $catname;?></option>
                          <?}
                    }?>
        </select>

    <label>Subcategory</label>
    <textarea rows="2" name="subcategory"></textarea>

    <input type="submit" name="submit" value="Submit" class="alt_btn">
</form>
a_insert_subcategory.php的

代码是

<?php
require 'connection.php';

$catname= mysqli_real_escape_string($con, $_POST['catname']);
$subcategory = mysqli_real_escape_string($con, $_POST['subcategory']);

$sql="INSERT INTO subcategory(catname,subcatname) VALUES ('$catname','$subcategory')";

if (!mysqli_query($con,$sql)) 
    {
        die('Error: ' . mysqli_error($con));
    }

mysqli_close($con);
?>

4 个答案:

答案 0 :(得分:0)

您可以尝试使用jQuery获取$ _POST [&#39; catid&#39;]

<form action="a_insert_subcategory.php" method="post">
    <div class="module_content">
        <fieldset>
            <label>Category</label>
                <select name="catname" id="catname">
                    <?php
                        require 'connection.php';
                        $sql = "SELECT * FROM category";
                        $result = mysqli_query($con, $sql);
                        if (mysqli_num_rows($result) > 0) 
                            {
                                while($row = mysqli_fetch_assoc($result)) 
                                    {
                                        $catname=$row["catname"];
                                        $catid=$row["catid"];
                    ?>

                    <option data-id=<?php echo $catid; ?> value="catname"><? echo $catname;?></option>
                                  <?}
                            }?>
                </select>
                <input type="hidden" name="catid" id="catid">
                <script>
jQuery(document).ready(function($) {
    jQuery(document).on('change', '#catname', function(event) {
       jQuery('#catid').val(jQuery('#catname option:selected').data('id'));
    });
});
                </script>
        </fieldset>     

        <fieldset>
            <label>Subcategory</label>
            <textarea rows="2" name="subcategory"></textarea>
        </fieldset>

    </div>
    <footer>
        <div class="submit_link">
            <input type="submit" name="submit" value="Submit" class="alt_btn">
        </div>
    </footer>
</form>

记下脚本标记以及隐藏类型的新表单输入。

答案 1 :(得分:0)

从你的位置开始

<option value="catname"><? echo $catname;?></option>

您需要将其更改为以下内容:

<option value="<? echo $row['id']; ?>"><? echo $catname;?></option>

这会将类别的ID放在select语句的每个选项中。

然后在您的a_insert_subcategory.php中,您可以使用$_POST['catname']访问类别ID(您可能希望重命名您的选择)。请注意,您将无法再通过$_POST变量访问名称类别(实际上,我认为您现在也没有)。因此,您需要从数据库中查找(尽管要正确规范化并避免数据冗余,您应该只在类别表中存储类别名称,然后按ID查找)。

答案 2 :(得分:0)

您可以将$ _POST [&#39;类别&#39;]存储在变量中,将其传递给查询类别表并返回其ID的函数。

function getCatId($Catname)
{
  $query = "select id from CATEGORY_TABLE where catname='$Catname' ";
  $result = mysqli_query($query);
  while($row = mysqli_fetch_array($result))
      {
         $CatID = $row['id'];
      }
  return $CatID;
}

答案 3 :(得分:0)

有两种解决方案:

  1. <option value="catname"><? echo $catname;?></option>行,您可以使用<option value="<? echo $row['id'];?>"><? echo $catname;?></option>和insert_subcategory.php文件

     <?php
          require 'connection.php';
    
          $category_id = mysqli_real_escape_string($con, $_POST['category']);
          $subcategory = mysqli_real_escape_string($con, $_POST['subcategory']);
    
          $catQuery = "SELECT catname FROM category WHERE id = ".$category_id." LIMIT 1";
          $catRes = mysqli_query($catQuery);
          $catRow = mysqli_fetch_assoc($catRes); 
    
          $sql="INSERT INTO subcategory(catid,catname,subcatname) VALUES ($category_id,'".$catRow['catname']."','$subcategory')";
    
          if (!mysqli_query($con,$sql)) 
             {
                 die('Error: ' . mysqli_error($con));
             }
    
          mysqli_close($con);
         ?>
    
  2. 如果您不想在值中使用id,请使用<option value="<? echo $catname;?>"><? echo $catname;?></option>和insert_subcategory.php文件中的第一个解决方案仍然不会向用户显示ID

     <?php
          require 'connection.php';
    
          $category = mysqli_real_escape_string($con, $_POST['category']);
          $subcategory = mysqli_real_escape_string($con, $_POST['subcategory']);
    
          $catQuery = "SELECT id FROM category WHERE catnme = '".$category."' LIMIT 1";
          $catRes = mysqli_query($catQuery);
          $catRow = mysqli_fetch_assoc($catRes); 
    
          $sql="INSERT INTO subcategory(catid,catname,subcatname) VALUES (".$catRow['id'].",'$category','$subcategory')";
    
          if (!mysqli_query($con,$sql)) 
             {
                 die('Error: ' . mysqli_error($con));
             }
    
          mysqli_close($con);
         ?>