【问题标题】:Trying to insert into a database as well as post items from a multi select drop-down menu尝试插入数据库以及从多选下拉菜单中发布项目
【发布时间】:2019-09-03 17:46:51
【问题描述】:

我需要能够将多选下拉列表中的选定项目保存到 MySQL 数据库中并显示所有选定项目。当我点击提交时,只保存最后一项。

代码最初用于从下拉菜单中选择单个项目。我已经修改它以选择多个项目(以便其他人编辑)。我尝试了各种解决方案,包括 if is_array、for 循环和 foreach 循环,但没有任何运气。谁能指出我正确的方向。

HTML 代码

<select name="topic_id[]" multiple="multiple" id="select">
    <?php
     $topic_set = find_all_topics();
    while($topic = mysqli_fetch_assoc($topic_set)) {
        foreach($topic_set as $topic) {
            echo "<option value=\"" . $topic['id'] . "\"";
            if($page['topic_id'] == $topic['id']) {
                echo " selected";
            }
            echo ">" . $topic['menu_name'] . "</option>";
        }
    }
    mysqli_free_result();
    ?>
</select>

PHP

function insert_page($page) {
        global $db;
        $errors = validate_page($page);
        if(!empty($errors)) {
            return $errors;
        }
        shift_page_positions(0, $page['position'], $page['topic_id']);
        $post_t_ids = array();
        foreach($_POST['topic_id'] as $post_t_id) {
            $post_t_ids[] = (int) $post_t_id;
        }
        $post_t_id_joined = join('), (', $post_t_ids);
        $sql = "INSERT INTO pages ";
        $sql .= "(topic_id, content) ";
        $sql .= "VALUES (";
        $sql .= "'" . db_escape($db, $post_t_id_joined) . "',";
        $sql .= "'" . db_escape($db, $page['content']) . "'";
        $sql .= ")";
        $result = mysqli_query($db, $sql);
        if($result) {
            return true;
        } else {
            echo mysqli_error($db);
            db_disconnect($db);
            exit;
        }
    }
    if(is_post_request()) {
        $page = [];
        $page['topic_id'] = $_POST['topic_id'] ?? '';
        $page['content'] = $_POST['content'] ?? '';
        $result = insert_page($page);
        if(!isset($_POST['topic_id'])) {
            $_POST['topic_id'] = [];
        }
        if($result === true) {
            $new_id = mysqli_insert_id($db);
        } else {
            $errors = $result;
        }
    }

结果应该是从下拉列表中选择多个项目,单击提交后,所有选定的项目应保存到数据库并显示到不同的页面。不会弹出错误消息,但只会保存和显示最后选择的项目。

【问题讨论】:

  • 如何将数据发送到服务器?发布代码。
  • 这是表单的代码
    。这是页面顶部的相关PHP if(is_post_request()){$page = []; $page['topic_id'] = $_POST['topic_id'] ?? '';$result = insert_page($page);if(!isset($_POST['topic_id'])){$_POST['topic_id'] = [];} if($result === true){$ new_id = mysqli_insert_id($db); $_SESSION['message'] = '页面创建成功。'; redirect_to(url_for('/​​content/content_pages/show.php?id=' . $new_id));}else{ $errors = $result;} 希望对您有所帮助。
  • 请把它作为代码块放在你的问题中

标签: php mysql drop-down-menu


【解决方案1】:

您做出sql 声明的部分是错误的。它会给你带来类似的东西:

INSERT INTO pages (topic_id, content) VALUES ('1), (2), (3','content')

您需要修改您的代码以获得正确的 sql 语句。当然,我建议你使用准备好的语句:

$params['content'] = $page['content'];
$stmt = $mysqli->prepare('INSERT INTO pages (topic_id, content) VALUES (:id, :content)');

foreach($_POST['topic_id'] as $id) {
    $params['id'] = $id;
    $stmt->execute($params);    
}

如果你仍然想使用你的方法,你应该这样做:

$content = $page['content'];
$makeValues = function($id) use ($content) {
    return "($id, '$content')";
};
$post_t_id_joined = implode(', ', array_map($makeValues, $post_t_ids));
$sql = "INSERT INTO pages ";
$sql .= "(topic_id, content) ";
$sql .= "VALUES $post_t_id_joined";

【讨论】:

  • 我将不得不花时间研究准备好的陈述。我不太精通PHP,所以这是我需要学习的东西。我确实尝试了这两个建议,每个都抛出了不同的错误。我确定我的一些代码不在正确的位置。我希望能够将所有代码发布到某个地方,以便有人可以查看它并查明问题出在哪里,因为无法选择多个项目。感谢您尝试提供帮助,我将远离它并在其他事情上工作一点。 .
  • @cfelker 您可以使用任何 php 沙箱来发布您的代码。此外,您可以使用实际代码更新您的问题,所以我会尽力帮助您。祝你好运:)
  • 我把必要的代码放在这里 git@github.com:clintdf/php_cms.git。感谢您提供的任何帮助。
猜你喜欢
  • 2020-01-11
  • 2020-03-27
  • 2020-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-09
  • 1970-01-01
  • 2019-07-31
相关资源
最近更新 更多