i have a problem retrieving data from mysql array, what i want to do is, when user open the same form next time for existing ID, he should see which option is checked, and which not, so, if he submit the form without changing anything (on checkbox), it'll be the same again.
Here's the code:
$media_array = $_POST['nesto1'];
foreach ($media_array as $one_media) {
$source .= $one_media.", ";
}
$nesto1 = substr($source, 0, -2);
<label><input type="checkbox" name="nesto1[]" value="NewData" /> NewData </label>
<label><input type="checkbox" name="nesto1[]" value="NewData2" /> NewData2 </label>
<label><input type="checkbox" name="nesto1[]" value="NewData3" /> NewData3 </label>
Here's how i retrieve the data:
$result = mysql_query("SELECT * FROM `article` WHERE id=".intval($_GET["id"])." ORDER BY id ASC");
$row = mysql_fetch_array($result);
How should i make checkbox option to be checked on next open, for example, NeData and NewData3, if i have values in my mysql row as: NewData, NewData3.
I have tried with in_array, but i didn't success and here's what i have tried:
<label><input type="checkbox" name="nesto1[]" value="NewData"<?php echo in_array("NewData",$nesto1)?" checked="checked"":""; ?> />NewData</label>
<label><input type="checkbox" name="nesto1[]" value="NewData2"<?php echo in_array("NewData2",$nesto1)?" checked="checked"":""; ?> />NewData2</label>
<label><input type="checkbox" name="nesto1[]" value="NewData3"<?php echo in_array("NewData3",$nesto1)?" checked="checked"":""; ?> />NewData3</label>
Thanks in advance.
New code (i have changed values, array and syntax error is fixed now):
if(isset($_POST["name"]) && isset($_POST["change"]) && isset($_POST["nesto2"]) && is_numeric($_POST["change"])){ $id = intval($_GET["id"]);
$name = mysql_real_escape_string($_POST["name"]);
$nesto1 = mysql_real_escape_string($_POST["nesto1"]);
$nesto2 = mysql_real_escape_string($_POST["nesto2"]);
$change = mysql_real_escape_string($_POST["change"]);
$type= mysql_real_escape_string($_POST["category"]);
$media_array = array();
foreach ($media_array as $one_media) {
$source .= $one_media.", ";
}
$nesto1 = substr($source, 0, -2);
$result = mysql_query("SELECT * FROM `article` WHERE id=".intval($_GET["id"])." ORDER BY id ASC");
$row = mysql_fetch_array($result);
form:
<label><input type="checkbox" name="nesto1[]" value="Kutija"<?php echo in_array("Kutija",$nesto1)?" checked=\"checked\"":""; ?> />Kutija</label>
<label><input type="checkbox" name="nesto1[]" value="Punjač"<?php echo in_array("Punjač",$nesto1)?" checked=\"checked\"":""; ?> />Punjač</label>
<label><input type="checkbox" name="nesto1[]" value="Baterija"<?php echo in_array("Baterija",$nesto1)?" checked=\"checked\"":""; ?> />Baterija</label>
Error: Warning: in_array() expects parameter 2 to be array, null given in /home/smarthos/public_html/articles/article/edit.php on line 142