dousui7410 2018-01-31 18:13
浏览 20
已采纳

获取数组值并根据其自己的列存储它,但值存储在不同的列中

The html checkbox before query, I chose option 1 and option 4

The checkbox value

the data where the values should be stored, but it stores into option 1 and option 2 columns instead while it should store it inside both 'opt1' and 'opt4'

Result inside the table

PHP

if( isset( $_POST['picked'] ) )
        {
            $keepdata = array();

            $keepdata = $_POST['pick'];

            $sqlinsert = "INSERT INTO traincheckbox VALUES ( null, '$keepdata[0]', '$keepdata[1]', '$keepdata[2]', '$keepdata[3]', '$keepdata[4]' )";

            mysqli_query( $sqlnrg, $sqlinsert );

            header( "Location: pick.php" );
        }

HTML

<html>
    <form method="post">
        <input type="checkbox" name="pick[]" value="PAID"><label>Activity 1</label></input>
        <input type="checkbox" name="pick[]" value="PAID"><label>Activity 2</label></input>
        <input type="checkbox" name="pick[]" value="PAID"><label>Activity 3</label></input>
        <input type="checkbox" name="pick[]" value="PAID"><label>Activity 4</label></input>
        <input type="checkbox" name="pick[]" value="PAID"><label>Activity 5</label></input>
        <button type="submit" name="picked">Pick activities</button>
    </form>
</html>
  • 写回答

1条回答 默认 最新

  • duanlan4801 2018-01-31 18:31
    关注

    Using name="pick[]" causes only to index checked checkboxes from 0 to n.

    If you need explicit indexes, then you should force them:

    <input type="checkbox" name="pick[0]" value="PAID"><label>Activity 1</label></input>
    

    and so on...

    You should remove $keepdata = array(); line from your code. It does nothing, because then it is initialized by $_POST['pick']. Also have on mind, that values not marked as checked will not be sent in any form. There will be errors because of non-existent keys in array (if you check 1 and 4, then 2 and 3 will not be set). You need to handle this situation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题