简体   繁体   English

尝试显示来自MySQL查询的数组结果

[英]Trying to display results from mysql query of an array

I have a form that has checkboxes on it. 我有一个带有复选框的表单。 Each check box is assigned a value from 1 to 10. These values correspond to an ID in a table for service names. 每个复选框都分配一个1到10的值。这些值对应于服务名称表中的ID。 Here is the form checkbox markup: 这是表单复选框标记:

<input type="checkbox" name="serv[]" id="janitorial" value=1><label for="janitorial" class="radCheck">Full Service Janitorial</label>
<input type="checkbox" name="serv[]" id="strip" value=6><label for="strip" class="radCheck">Stripping & Waxing</label></br>
<input type="checkbox" name="serv[]" id="buff" value=2><label for="buff" class="radCheck">Floor Buffing/Burnishing</label>
<input type="checkbox" name="serv[]" id="carp" value=7><label for="carp" class="radCheck">Carpet & Upholstery Care</label><br>
<input type="checkbox" name="serv[]" id="tile" value=3><label for="tile" class="radCheck">Tile Cleaning & Restoration</label>
<input type="checkbox" name="serv[]" id="windows" value=8><label for="windows"class="radCheck">Window & Blind Care</label></br>
<input type="checkbox" name="serv[]" id="park" value=4><label for="park" class="radCheck">Parking Lot Care</label>
<input type="checkbox" name="serv[]" id="porter" value=9><label for="porter" class="radCheck">Day Porter</label></br>
<input type="checkbox" name="serv[]" id="snow" value=5><label for="snow" class="radCheck">Snow Removal & Landscape</label>
<input type="checkbox" name="serv[]" id="rest" value=10><label for="rest" class="radCheck">Restroom Services & Supplies</label>

This is posted with all the other form data and sent in an email as well as inputted into the database. 它与所有其他表单数据一起过帐并通过电子邮件发送,并输入到数据库中。 I am try to retrieve the names of each service selected from the database but cannot figure out how to write the results of my query. 我试图检索从数据库中选择的每个服务的名称,但无法弄清楚如何编写查询结果。 Here is the code for the services query: 这是服务查询的代码:

$conn=connect();

            $names = implode(',', $serv);
            $query = "SELECT SERVICE_NAME FROM SERVICES WHERE SERVICE_ID ({$names})";
            $result = mysql_query($query);                

            echo "$query";

This outputs: 输出:

SELECT SERVICE_NAME FROM SERVICES WHERE SERVICE_ID (1,7,3)

When I submitted the form I had boxes 1, 7, and 3 checked. 当我提交表格时,我选中了1、7和3框。 This all seems right to me. 这一切对我来说似乎都是正确的。 But when I try to get the results through fetch_array or fetch_row I get errors. 但是,当我尝试通过fetch_array或fetch_row获得结果时,会出现错误。 I've tried countless loops and as many suggestions as I could find on here and other sites. 我尝试了无数循环,并在这里和其他站点上找到了尽可能多的建议。 How can I write the results of the query? 如何编写查询结果? Please go easy on me, I am very new to PHP and am sure I'm missing something simple. 请放轻松,我对PHP还是很陌生,请确保我缺少一些简单的东西。

That's incorrect: 那是不正确的:

SELECT SERVICE_NAME FROM SERVICES WHERE SERVICE_ID IN (1,7,3)
                                                  ^^^^---missing

And please note that you're wide open to an sql injection attack . 并且请注意,您对sql注入攻击持开放态度 Enjoy having your server pwn3d. 享受服务器pwn3d的乐趣。 You also seem to be depending on register_globals being enabled (unless you're doing stuff in code not shown here). 您似乎还取决于是否启用register_globals(除非您正在使用此处未显示的代码进行操作)。 Definitely step away from this code, update your PHP version, and LEARN how to write secure code. 绝对远离此代码,更新您的PHP版本,并了解如何编写安全代码。

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

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