dongtuo5262 2013-11-19 01:13
浏览 83
已采纳

如何检查是否选中了复选框,并执行必要的查询以更新相应的表?

HTML

<label for="wall_category">Wall Category:</label>
    <input type="checkbox" name="wallpaper"> Wallpaper
    <input type="checkbox" name="photography"> Photography

PHP

//obtains last insert id
    $wallId = mysql_insert_id();
    echo $wallId;

I have categories table in my database in which the following data is stored:

category_id  category_name
1             Wallpaper
2             Photography

and I have another table called wall_categories in which:

wall_id (P, F)    category_id (P, F)

After getting the wall_id from last_insert_id, I want to check what category among the two is checked. For example, Wallpaper checkbox is checked, since the category_id is 1, I want to store that category_id along with whatever the wall_id will be to my wall_categories table. So my table will become (lets say wall_id is 16)

wall_id (P, F)    category_id (P, F)
 16                 1

And one last thing to add since Categories are checkboxes, when both are selected, I want to add both to my wall_categories table so my wall_categories table would become:

wall_id (P, F)    category_id (P, F)
     16                 1
     16                 2

I was trying some workarounds, but I'm still a rookie in PHP. Would like to ask your help on how to do this. Thanks! (The script will just be used internally.)

  • 写回答

2条回答 默认 最新

  • doujiu5464 2013-11-19 02:08
    关注

    HTML

    <form action="process.php" method="POST">
        <label for="category">Wall Category:</label>
        <input type="checkbox" name="category[]" value="1"> Wallpaper
        <input type="checkbox" name="category[]" value="2"> Photography
    </form>
    

    If you want to proceed checkboxes input, i suggest you to do it as the script above. category[] would be an array that could contain values such as 1 and 2 (your category id).
    So you have to parse it in server side(PHP) before you insert it into database. if you set the wall_id column value automatically generated you will get some errors, because wall_id would be different with previous wall_id. So i suggest you to set it automatic but manually defined(didn't get my words? I'll explain it).
    Here's the PHP :

    <?php
    $con = mysqli_connect("host","username","password","database_name");
    //First get the category from input
    $checked = $_REQUEST['category']; // it's an array
    
    //--------get last
        //Then you have to get the last wall_id from your wall_categories table
        //and add it 1 (+1)
        $q1 = mysqli_query($con,"SELECT MAX(wall_id) as last_wall_id from wall_categories"); // get last wall_id
        $last = mysqli_fetch_array($q1);
        $next_wall_id = $last['last_wall_id']+1;//+1 last wall_id for next wall_id
    //-------end of get last
    
    foreach($checked as $cat){
    mysqli_query($con,"INSERT INTO wall_categories (wall_id, category_id) values ('$next_wall_id','$cat') ");
    }
    ?>
    

    If your wall_id comes from another table, please skip get last step. and remove/change the $next_wall_id by the wall_id variable that you get from another table.
    That's all if you want to do it. Any question? found some errors? comment it below :D

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

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题