如何从mysql数组中检索数据并在表单中选中复选框

时间:2021-09-19 13:27:18

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.

我在从mysql数组中检索数据时遇到问题,我想要做的是,当用户下次为现有ID打开相同的表单时,他应该看到哪个选项被选中,哪个没有,所以,如果他提交表单而不更改任何东西(在复选框上),它会再次相同。

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.

我应该如何使复选框选项在下次打开时被检查,例如,NeData和NewData3,如果我的mysql行中的值为:NewData,NewData3。

I have tried with in_array, but i didn't success and here's what i have tried:

我试过in_array,但我没有成功,这就是我尝试过的:

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

if(isset($ _ POST [“name”])&& isset($ _ POST [“change”])&& isset($ _ POST [“nesto2”])&& is_numeric($ _ POST [“change”])){$ id = INTVAL($ _ GET [ “身份证”]);

$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

错误:警告:in_array()期望参数2为数组,在第142行的/home/smarthos/public_html/articles/article/edit.php中给出null

3 个解决方案

#1


0  

Syntax error:

in_array("NewData",$nesto1)?" checked="checked"":""
                            ^--       ^--     ^^---

You cannot use the same type of quotes inside a string as you quoted the entire string with. Try

在引用整个字符串时,不能在字符串中使用相同类型的引号。尝试

in_array("NewData",$nesto1)?' checked="checked"':""
                            ^--                ^--

or

in_array("NewData",$nesto1)?" checked=\"checked\"":""
                                      ^--      ^--

instead.

#2


0  

In your foreach look, check the array value for data, if data exists render 'checked'.

在foreach外观中,检查数据的数组值,如果数据存在渲染“已检查”。

foreach ( as $key=>$value) {
    echo '<label><input type="checkbox" name="nesto1[]" value="NewData"        

    //if data item is checked, render option as checked
    if ($nesto1 == true) {
        //this could also be done via CSS on the option element.
        echo'checked';
    }
    echo $value. 'NewData</label>';
}

That should get you on the right track.

那会让你走上正轨。

#3


0  

In your first code block, $nesto1 is not an array, so you can't use in_array. Try:

在您的第一个代码块中,$ nesto1不是数组,因此您不能使用in_array。尝试:

$nesto1 = array();
foreach ($media_array as $one_media) {
    $nesto1[] = $one_media;
}

Also, as @marc-b points, there is a syntax error.

另外,正如@ marc-b指出的那样,存在语法错误。

#1


0  

Syntax error:

in_array("NewData",$nesto1)?" checked="checked"":""
                            ^--       ^--     ^^---

You cannot use the same type of quotes inside a string as you quoted the entire string with. Try

在引用整个字符串时,不能在字符串中使用相同类型的引号。尝试

in_array("NewData",$nesto1)?' checked="checked"':""
                            ^--                ^--

or

in_array("NewData",$nesto1)?" checked=\"checked\"":""
                                      ^--      ^--

instead.

#2


0  

In your foreach look, check the array value for data, if data exists render 'checked'.

在foreach外观中,检查数据的数组值,如果数据存在渲染“已检查”。

foreach ( as $key=>$value) {
    echo '<label><input type="checkbox" name="nesto1[]" value="NewData"        

    //if data item is checked, render option as checked
    if ($nesto1 == true) {
        //this could also be done via CSS on the option element.
        echo'checked';
    }
    echo $value. 'NewData</label>';
}

That should get you on the right track.

那会让你走上正轨。

#3


0  

In your first code block, $nesto1 is not an array, so you can't use in_array. Try:

在您的第一个代码块中,$ nesto1不是数组,因此您不能使用in_array。尝试:

$nesto1 = array();
foreach ($media_array as $one_media) {
    $nesto1[] = $one_media;
}

Also, as @marc-b points, there is a syntax error.

另外,正如@ marc-b指出的那样,存在语法错误。