复选框值放入mysql查询 [英] Checkbox values into mysql query

查看:73
本文介绍了复选框值放入mysql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,它允许我从复选框中检索数据,尚不包含任何mysql,因此您可以帮助我进行修改吗?

i have this code which permits me to retrieve data from a checkbox, there isn't any mysql included yet, so can you help me modify this?

表单如下:

<form action="checkbox.php" method="post">
<input type="checkbox" name="checkbox[]" value="hostess_name">
<input type="checkbox" name="checkbox[]" value="hostess_familyname_en">
<input type="checkbox" name="checkbox[]" value="hostess_id">
<input type="checkbox" name="checkbox[]" value="hostess_firstname_en">
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>

我插入的值应该是数据库字段,然后我有了这个checkbox.php文件,该文件可以读取所选的值.

The values i inserted are supposed to be database fields, then i have this checkbox.php file which reads the values selected.

    <?php
mysql_connect("localhost", "root", "") or die(mysql_error());
echo "Connected to MySQL<br />";
?>
<?

/* and in your checkbox.php you do this: */

if(isset($_POST['Submit']))
{
echo "<pre>"; print_r($_POST);
}
$checked = mysql_real_escape_string(implode(',', $_POST['checkbox'])); 
echo $checked;
?>

如何为复选框值分配数据库字段? 我正在尝试做的是存储值并使用implode将其导出到查询中. 救救我.

How can assign to checkbox values database fields? What i'm trying to do is to store the values and export them into a query using implode.. Help me..

推荐答案

您的POST变量($ _POST ['checkbox'])实际上已经是一个数组.首先,要弄清楚您的实际用途,请执行以下操作:

Your POST variable ($_POST['checkbox']) is actually already an array. First, to figure out what you are actually working with, do this:

echo '<pre>';
print_r ($_POST['checkbox']);
echo '</pre>';

然后查看您的脚本并查看输出.您可能会看到带有一些键和值的数组.使用它,您可以决定如何进行.

Then view your script and have a look at the output. Chances are you'll see an array with some keys and values. Using that you can decide how to proceed.

如果是我,我会执行以下操作来完成您的任务:

If it were me I would do something like the following to accomplish your task:

$sql = "SELECT `table_id_column`, `another_column` ";
foreach ($_POST['checkbox'] as $key => $value) {
  $sql .= ", `$value`";
}
$sql .= " FROM `hostess` ORDER BY `another_colmn` ASC";

请记住,以这种方式修改SQL语句是非常不好的做法.在将其投入生产环境之前,您需要对此进行一些安全性.

Please keep in mind that allowing an SQL statement to be modified in this manner is very bad practice. You'll want to introduce some security into this before putting it on a production environment.

卢克

这篇关于复选框值放入mysql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆