dsdxlibt98300 2013-11-16 19:48
浏览 52
已采纳

将array_intersect_key数组数据插入数据库表

I'm new to programming, I have created a form with several check boxes and text fields. Each text field will activated if the check box is selected. The intersect key array is used to get all the checked check-box names and text field values. I want to insert these array data into a database table.

Can anyone give me some codes to do this. Thanks :)

This is function.php file

    global $usedTexts;
    $usedTexts = array();

    function postdata(){
        if ( isset($_POST['submit']) && array_key_exists("t", $_POST)  && is_array($_POST["t"]) && array_key_exists("cb", $_POST) && is_array($_POST["cb"])) {
             $usedTexts = array_intersect_key($_POST["t"], $_POST["cb"]);
        }
    }

and this is index.php file

  <?php include_once("function.php"); ?>
  <?php postdata(); ?>

  <form action="" method="post">
      <input type="checkbox" name="cb[1]" value="" onclick="document.getElementById('t1').disabled=!this.checked;" />
      <input type="number" max="5" min="1" id="t1" name="t[1]" value="" disabled="disabled" /> <br /><br />

      <input type="checkbox" name="cb[2]" value="" onclick="document.getElementById('t2').disabled=!this.checked;"/>
      <input type="number" max="5" min="1"id="t2" name="t[2]" value="" disabled="disabled" /><br /><br />

      <input type="checkbox" name="cb[3]" value="" onclick="document.getElementById('t3').disabled=!this.checked;"/>
      <input type="number" max="5" min="1"id="t3" name="t[3]" value="" disabled="disabled" /><br /><br />

      <input type="checkbox" name="cb[4]" value="" onclick="document.getElementById('t4').disabled=!this.checked;"/>
      <input type="number" max="5" min="1"id="t4" name="t[4]" value="" disabled="disabled" /><br /><br />

      <input name="submit" type="submit" value="Submit" />
  </form>
  • 写回答

1条回答 默认 最新

  • dtyrxmoj20617 2013-11-16 20:05
    关注

    Can you try this,

          if ( isset($_POST['submit']) && array_key_exists("t", $_POST)  && is_array($_POST["t"]) && array_key_exists("cb", $_POST) && is_array($_POST["cb"])) {
            $usedTexts = array_intersect_key($_POST["t"], $_POST["cb"]);
    
                    foreach($usedTexts as $subjectId=>$subjectExp){
    
                        $query=  "insert into mysubjects (subjectId, subjectExp) values('".$subjectId."','".$subjectExp."')";
                        //execute your query, before that make sure you have established the database connection
                    }               
    
          }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀