0

i want get all values from checkbox and insert it on one column like 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

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

2 Answers 2

1

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)

<?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>
Sign up to request clarification or add additional context in comments.

15 Comments

kevinabelita,thnx brother , but it's not work with me , values on column = 0
@o6qr wow :) no error whatsoever? my tip is, try to build the query statement first (test the echoed query of $statement in phpmyadmin, so that you'll see errors
:) sorry , i try it agine , show me this error "Column count doesn't match value count at row 1"
@o6qr i updated the answer maybe you forgot $db->query_write($statement)
no i don't forget it , i used it already [$db->query_write("INSERT INTO ]
|
1

i found what's problem ,

the problem not in code , this will be adding values array on mysql like 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 .

Excuse me and thank for your efforts @user1978142

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.