使用PHP将复选框值插入MySQL数据库 [英] Inserting checkbox values into MySQL Database with PHP

查看:202
本文介绍了使用PHP将复选框值插入MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人。



我知道这是一个很受好评的主题,但我已经无处不在,无法解决。



我在我的网页上有一系列复选框,需要将这些值插入数据库。



对于我阅读的内容,以这种方式:

 < tr> 
< td class =titulofila> Sal& oacute; n Completo con:< / td>
< td>
< table width =100%>< tr>< td>
< label>
< input name =equipSalon []type =checkboxid =equipSalon []value =mesa/>
mesas< / label>
< / td>< td>
< label>
< input name =equipSalon []type =checkboxid =equipSalon []value =sillas/>
sillas< / label>
< / td>< td>
< label>
< input name =equipSalon []type =checkboxid =equipSalon []value =sofa/>
sof& aacute;< / label>
< / td>< / tr>< / table>
< / td>
< / tr>

然后,在提交按钮所用的PHP脚本中:

  $ equipSalonF = mysql_real_escape_string(implode(',',$ _POST ['equipSalon'])); 
mysql_query(INSERT INTO markers(ciudad,
zona,address,name,
telefono,email,piso,
tipo,erasmus,nhabitaciones,
plazas,equipHabita ,nbanos,
salon,cocina,electrodomesticos,
garaje,internet,calefaccion,
sexo,precio,superficie,otros,
fecha,lat,lng)
VALUES ('{$ _POST ['ciudad']}','{$ _ POST ['zona']}',
'{$ _POST ['address']}','{$ _ POST ['name'] }','{$ _ POST ['telefono']}',
'{$ _POST ['email']}','{$ _ POST ['piso']}','{$ _ POST ['tipo ']}',
'{$ _POST ['erasmus']}','{$ _ POST ['nhabitaciones']] {$ equipHabitaF}',
'{$ _POST ['nbanos']}','{$ equipSalonF}',
'{$ equipCocinaF}','{$ equipElectroF}','{$ _POST ['garaje']}',
'{$ _POST ['internet']}','{$ _ POST ['calefaccion']}',
'{$ _POST ['sexo'] }','{$ _ POST ['precio']}',
'{$ _POST ['superficie']}','{$ _ POST ['otrosF']}',
'{$ _POST ['fecha']}','{$ _ POST ['lat']},
'{$ _POST ['lng']},$ link);

我有超过此复选框,这不工作,只是说你有一个错误您的SQL语法;检查与您的MySQL服务器版本对应的手册,以获得在第21行附近使用的正确语法



如果任何人想要查看完整的index.html和php文件,您可以在这里: http://paste.ideaslabs.com/show/ 8qEHDnsZdr ,表单从第147行开始。这里的PHP文件: http:// paste。

解决方案



复选框只有在勾选时才会发布。因此,如果复选框未勾选,则不会出现在 $ _ POST 中。此外,你通常不应该给复选框的任何值。



在数据库中,我通常用tinyint表示复选框,存储1表示检查,0表示未选中。

  //如果你的复选框名称是foo,这将把
//转换为可以存储在数据库中的值
$ foo = isset($ _ POST ['foo'])? 1:0;

另请注意,html要求ids是唯一的。所以你不能有多个元素具有相同的id。并且您应该清理您的输入,以防止sql注入。对数据库中的用户输入使用 mysql_real_escape_string()



更新



主要的问题是查询在最后一行缺少一个''。
查询应类似于

  $ query =INSERT INTO markers(ciudad,
zona,address ,name,
telefono,email,piso,
tipo,erasmus,nhabitaciones,
plazas,equipHabita,nbanos,
salon,cocina,electrodomesticos,
garaje,internet ,calefaccion,
sexo,precio,superficie,otros,
fecha,lat,lng)
VALUES('{$ _POST ['ciudad']}','{$ _ POST ['zona ']}',
'{$ _POST ['address']}','{$ _ POST ['name']}','{$ _ POST ['telefono']}',
' {$ _POST ['email']}','{$ _ POST ['piso']}','{$ _ POST ['tipo']}',
'{$ _POST ['erasmus']}' ,'{$ _ POST ['nhabitaciones']}',
'{$ _POST ['plazas']}','{$ equipHabitaF}',
'{$ _POST ['nbanos']} ','{$ equipSalonF}',
'{$ equipCocinaF}','{$ equipElectroF}','{$ _ POST ['garaje']}',
'{$ _POST ''}','{$ _ POST ['calefaccion']}',
'{$ _POST ['sexo']}','{$ _ POST ['precio']}',
' {$ _POST ['superficie']}','{$ _ POST ['otrosF']}',
'{$ _POST ['fecha']}','{$ _ POST ['lat']} ,
'{$ _POST ['lng']}');
mysql_query($ query,$ link);

注意关闭'查询的最后一行。还要注意,在变量中创建这样的查询,允许您输出创建的查询,以便您可以看到发送到MySQL是什么,您还可以在不同的环境中测试您的查询(即在phpmyadmin或任何其他数据库管理工具)。


everyone.

I know this is a far treated subject, but I've looked everywhere and cannot get it solved.

I have a series of checkboxes in my webpage and need to insert the values into a database.

For what I've read, it's supposed to be done this way:

<tr>
        <td class="titulofila">Sal&oacute;n Completo con: </td>
        <td>
        <table width="100%"><tr><td>
            <label>
            <input name="equipSalon[]" type="checkbox" id="equipSalon[]" value="mesa"  />
            mesas </label>
        </td><td>
            <label>
            <input name="equipSalon[]" type="checkbox" id="equipSalon[]" value="sillas"  />
            sillas </label>
        </td><td>
            <label>
            <input name="equipSalon[]" type="checkbox" id="equipSalon[]" value="sofa"  />
            sof&aacute;</label>
        </td></tr></table>
        </td>
    </tr>

And then, in the PHP script that the submit button takes to:

 $equipSalonF=mysql_real_escape_string(implode(',', $_POST['equipSalon']));
mysql_query("INSERT INTO markers (ciudad,
                            zona,address,name,
                            telefono,email,piso,
                            tipo,erasmus,nhabitaciones,
                            plazas,equipHabita,nbanos,
                            salon,cocina,electrodomesticos,
                            garaje,internet,calefaccion,
                            sexo,precio,superficie,otros,
                            fecha,lat,lng)
        VALUES ('{$_POST['ciudad']}','{$_POST['zona']}',
                '{$_POST['address']}','{$_POST['name']}','{$_POST['telefono']}',
                '{$_POST['email']}','{$_POST['piso']}','{$_POST['tipo']}',
                '{$_POST['erasmus']}','{$_POST['nhabitaciones']}',
                '{$_POST['plazas']}','{$equipHabitaF}',
                '{$_POST['nbanos']}','{$equipSalonF}',
                '{$equipCocinaF}','{$equipElectroF}','{$_POST['garaje']}',
                '{$_POST['internet']}','{$_POST['calefaccion']}',
                '{$_POST['sexo']}','{$_POST['precio']}',
                '{$_POST['superficie']}','{$_POST['otrosF']}',
                '{$_POST['fecha']}','{$_POST['lat']}',
                '{$_POST['lng']}'",$link);

I have more than this checkboxes and this doesn't work, just says "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 21"

If anyone wants to look at the full index.html and php files, you can have them here: http://paste.ideaslabs.com/show/8qEHDnsZdr the form starts at line 147. And the PHP File in here: http://paste.ideaslabs.com/show/IJFLSHM7Ka

Thanks in advance.

解决方案

Checkboxes are only posted when they are ticked. So if the checkbox is not ticked, is will not appear in $_POST. Also you should generally not give any values to checkboxes. Instead use names to distinguish between them.

In the database I usually represent checkboxes with tinyints and store 1 for checked and 0 for unchecked.

// If your checkbox name is foo, this will convert it
// into a value that can be stored in the database
$foo = isset($_POST['foo']) ? 1 : 0;

Also note that html requires ids to be unique. So you can't have multiple elements with the same id. And you should sanitize your inputs to prevent sql injection. Use mysql_real_escape_string() on user inputs that go in the database.

Update

The main issue is that the query is missing a ')' at the last line. The query should look like

$query = "INSERT INTO markers (ciudad,
                        zona,address,name,
                        telefono,email,piso,
                        tipo,erasmus,nhabitaciones,
                        plazas,equipHabita,nbanos,
                        salon,cocina,electrodomesticos,
                        garaje,internet,calefaccion,
                        sexo,precio,superficie,otros,
                        fecha,lat,lng)
    VALUES ('{$_POST['ciudad']}','{$_POST['zona']}',
            '{$_POST['address']}','{$_POST['name']}','{$_POST['telefono']}',
            '{$_POST['email']}','{$_POST['piso']}','{$_POST['tipo']}',
            '{$_POST['erasmus']}','{$_POST['nhabitaciones']}',
            '{$_POST['plazas']}','{$equipHabitaF}',
            '{$_POST['nbanos']}','{$equipSalonF}',
            '{$equipCocinaF}','{$equipElectroF}','{$_POST['garaje']}',
            '{$_POST['internet']}','{$_POST['calefaccion']}',
            '{$_POST['sexo']}','{$_POST['precio']}',
            '{$_POST['superficie']}','{$_POST['otrosF']}',
            '{$_POST['fecha']}','{$_POST['lat']}',
            '{$_POST['lng']}')";
mysql_query($query, $link);

Note the closing ')' on the last line of the query. Also note that creating the query like this, in a variable, allows you to output the created query so you can see what exactly is sent to MySQL and you also can test your query in a different environment (ie in phpmyadmin or any other database administration tool).

这篇关于使用PHP将复选框值插入MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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