【问题标题】:Getting checkbox-selected data from Table A inserted into Table B从插入表 B 的表 A 中获取复选框选择的数据
【发布时间】:2017-05-23 06:12:30
【问题描述】:

我有一个包含数千条记录的表 A。在每个显示的页面上,仅显示 20 行,包含以下字段:

check
last_name 
first_name 
gr 
eth 
sex 
id_num (a unique value)
reason 
start_date

“check”字段表示用户可以勾选的复选框。每页 20 条记录的底部是选择按钮。我想从用户从表 A 中选择的行中获取唯一的“id_num”并将它们插入表 B。(方法是 post。)

复选框输入代码:

$html = <input type='checkbox' name='checkbox[]'
                      id = '' value='<?PHP echo $aRecords [ \"id_num\" ]; ?>' />" ;

页面上的行被用户选中后的SQL:

include_once "db_login.php" ;
if ( isset ( $_POST [ checkbox] ) ) 
   { 
      foreach ( $_POST [ 'checkbox' ] as $checkbox )
         {
            $sql = "INSERT INTO sap_id_select ( select_id )
                    VALUES ( '<???>' ) " ;
            mysql_query ( $sql ) or ( "Error " . mysql_error () ) ;
         }
   }

也试过了,除此之外还有很多:

foreach ( $_POST [ 'checkbox' ] as $checkbox )
         {
            $id_list = implode(',',$_POST['checkbox']);
            $sql = "INSERT INTO sap_id_select ( select_id )
                    VALUES ( '{$id_list}' ) " ;
         }

如何将 id_num 字段放入 $_POST 数组,然后放入表 B?

【问题讨论】:

    标签: php html mysql checkbox


    【解决方案1】:
     $html = <input type='checkbox' name='checkbox[]'
                      id = '' value='<?PHP echo $aRecords [ \"id_num\" ]; ?>' />" ;
    

    你错过了公开报价,不是吗?所以上面没有给你一个错误,这意味着你的开场白在这条线之上,换句话说,这一切都搞砸了。使用为您的语法着色的编辑器。

    假设你想写

     $html = "<input type='checkbox' name='checkbox[]'
                      id = '' value='<?PHP echo $aRecords [ \"id_num\" ]; ?>' />" ;
    

    然后 php 将逐字解释

     $html = "<input type='checkbox' name='checkbox[]'
                      id = '' value='".$aRecords["id_num"]."' />" ;
    

    或者你写的不是你代码的 php 执行部分,那么我所能建议的就是放

    【讨论】:

    • 是的,马丁,你是对的。我有点放弃了这个,但现在回去纠正这个,它正在工作。谢谢!我原以为这是一个难以理解的 SQL 查询问题。
    【解决方案2】:

    看起来你已经设置好了。试试

      $sql = "INSERT INTO sap_id_select ( select_id )
                    VALUES ( '.$checkbox.' ) " ;
    

    也许您可以说出您遇到的具体错误。

    【讨论】:

    • 谢谢,文森特。仍然没有成功。有趣的是,我没有收到任何错误。
    • Vincent,id_num 字段是 CHAR(9) 类型。这是插入的内容:.
    • 在浏览器中查看你页面的html源码,查看复选框代码,值是什么样子的?它应该看起来像 具有相关的 id 值。如果不是,那么你的 php 输出是错误的。
    【解决方案3】:

    Vincent -- 我将 html 复选框输入代码更改为

    $html .= "<input type='checkbox' name='checkbox[]' id = ''
                value='$aRecords ['id_num']' />" ;
    

    假设这是正确的 (?),我的 SQL INSERT 语句会将唯一记录号 (id_num) 插入表 B 中吗?当然,下面的 SQL 是行不通的。

    include_once "db_login.php" ;
    if ( isset ( $_POST [ checkbox] ) ) 
       {  
          foreach ( $_POST [ 'checkbox' ] as $checkbox )
             {  
                $sql = "INSERT INTO sap_id_select ( selected_id )
                        VALUES ( '{ $_POST [ checkbox ]}'
                               ) " ;
                mysql_query ( $sql ) or ( "Error " . mysql_error () ) ;
             }
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-04
      • 2013-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多