doubiankang2845 2018-08-14 18:22
浏览 37

用php将multiselect添加到数据库中

I'm trying to get a input from a multiselect into a database. below is a snippet of the select field in the form I have. I did find the suggestion to [] so I added that. was a big step forward

<select name="project_dicipline[]" data-placeholder="Select uw dicipline(s)" class="chosen-select" multiple style="width:350px;">
     <option value=""></option> 
     <option value="Elektra_LS">Elektra LS</option> 
     <option value="Elektra_MS">Elektra MS</option> 
     <option value="Elektra_OV">Elektra OV</option>
</select>

Below is what I got for reading the info out of the _POST it works fine if I just echo the foreach() line (got it from another post on stackoverflow), but I want to add it in a database, and I'm not sure how to get that done. I could really use some help for that.

$project_dicipline = array();
foreach ($_POST['project_dicipline'] as $project_dicipline)
   $project_dicipline = mysqli_real_escape_string($conn, $project_dicipline);
   echo $project_dicipline;

--edit-- below is the connection to db. That part i know works, because the rest of the form gets validated and added to the form. for that reason i have cut out a lot of the fields out of the query. (all variables have been escaped before the query is run)

    $conn = mysqli_connect($servername, $usernamedb, $passwordDB, $database);
    if (!$conn) 
    {
        die("Connection failed: " . mysqli_connect_error());
    }


    $query = "INSERT INTO projects (project_status, project_name, project_client, project_diciplines)

     VALUES('New', '$project_naam', '$project_plaats', '$project_client_company', '$project_dicipline')";
     mysqli_query($conn, $query);
  • 写回答

1条回答 默认 最新

  • dqu3974 2018-08-15 11:20
    关注

    You need to insert it using the foreach loop if they are to be individual records in the database

    $conn = mysqli_connect($servername, $usernamedb, $passwordDB, $database);
    if (!$conn) 
    {
        die("Connection failed: " . mysqli_connect_error());
    }
    
    if ($stmt = mysqli_prepare($conn, "INSERT INTO projects (project_status, project_name, project_client, project_diciplines) VALUES('New', ?, ?, ?, ?)")) {
    
        $project_dicipline = array();
        foreach ($_POST['project_dicipline'] as $project_dicipline){
             $project_dicipline = mysqli_real_escape_string($conn, $project_dicipline);
    
             mysqli_stmt_bind_param($stmt, $project_naam, $project_plaats,$project_client_company,$project_dicipline);
    
             /* execute query */
             mysqli_stmt_execute($stmt);
        }
    
    }
    

    You can read more about mysqli on php.net http://php.net/manual/en/book.mysqli.php and post/loops as well http://php.net/manual/en/control-structures.foreach.php and http://php.net/manual/en/reserved.variables.post.php

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)