简体   繁体   English

将多个复选框值插入单个列 - SQL数据库

[英]inserting multiple checkbox values into a single column - SQL database

i want get all values from checkbox and insert it on one column like 1,2,3 我想从复选框中获取所有值并将其插入一列如1,2,3

that my code , i don't know what's wrong 我的代码,我不知道什么是错的

<form method="post" style="display:inline">
<label><input type="checkbox" name="data[]" value="1">1</label>
<label><input type="checkbox" name="data[]" value="2">2</label>
<label><input type="checkbox" name="data[]" value="3">3</label>
</form>

        <?php
if ($_POST['do'] =="insertawards")
{
    $media_array = $_POST['data'];
    $values = array();
    foreach($media_array as $value)
    {
        $values[] = $value;
    }
    $values = implode(',', $values);
        $db->query_write("INSERT INTO " . TABLE_PREFIX . " awards (awards_forumid,awards_userid,awards_name,awards_link) 
        VALUES('".$values."','0','".$_POST['awards_name']."','".$_POST['awards_link']."') ");
}
        ?>

when i used implode , i get error and inserting 0 on datbase 当我使用implode时,我得到错误并在数据库上插入0

$media_array = $_POST['data'];
foreach ($media_array as $one_media)
{
$source .= $one_media;
}
$source = implode(',', $source);

I think the submit button is missing, anyway, if you need to implode the values properly. 无论如何,如果您需要正确地破坏值,我认为提交按钮丢失了。 Consider this example: (i assume 1, 2, 3 are inserted each not the whole string is inserted like 1,2,3) 考虑这个例子:(我假设插入1,2,3,而不是像1,2,3那样插入整个字符串)

<?php

define('TABLE_PREFIX', 'vb_');

if(isset($_POST['submit'])) {
    // quick and dirty, just roll up your sanitation
    $media_array = $_POST['data'];
    $awards_name = '"'.$_POST['awards_name'].'"';
    $awards_link = '"'.$_POST['awards_link'].'"';
    $checkbox_values = '"'.implode(',', $media_array).'"';
    $statement = "INSERT INTO ".TABLE_PREFIX."awards (awards_forumid,awards_userid,awards_name,awards_link) ".
        "VALUES ($checkbox_values, '0', $awards_name, $awards_link)";
    echo $statement;
    // INSERT INTO vb_awards (awards_forumid,awards_userid,awards_name,awards_link) VALUES ("1,2,3", '0', "best_website", "http://www.google.com/")
}

?>

<form method="post" style="display:inline">
    <input type="hidden" name="awards_name" value="best_website" />
    <input type="hidden" name="awards_link" value="http://www.google.com/" />
    <label><input type="checkbox" name="data[]" value="1">1</label>
    <label><input type="checkbox" name="data[]" value="2">2</label>
    <label><input type="checkbox" name="data[]" value="3">3</label>
    <input type="submit" name="submit" value="submit" />
</form>

i found what's problem , 我发现了什么问题,

the problem not in code , this will be adding values array on mysql like 1,2,3,4 问题不在代码中,这将在mysql上添加值数组,如1,2,3,4

$values = implode(',',$_POST['data'])."\n"; 

the real problem on Type of mysql field , i make it int(10) While must be mediumtext Even accept coma . 关于mysql字段类型的真正问题,我使它成为int(10)虽然必须是mediumtext甚至接受昏迷。

Excuse me and thank for your efforts @user1978142 对不起,谢谢你的努力@ user1978142

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM