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);
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>
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'])."
";
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