简体   繁体   English

使用PHP将复选框值插入MySQL数据库

[英]Inserting checkbox values into MySQL Database with PHP

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: 然后,在提交按钮所用的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']}',
                '{$_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" 我有更多这个复选框,这不起作用,只是说“你的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第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 如果有人想查看完整的index.html和php文件,你可以在这里找到它们: http//paste.ideaslabs.com/show/8qEHDnsZdr表单从第147行开始。这里的PHP文件: 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 . 因此,如果未勾选复选框,则不会出现在$_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. 在数据库中,我通常表示带有tinyints的复选框,存储1表示已选中,0表示未选中。

// 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. 另请注意,html要求ID是唯一的。 So you can't have multiple elements with the same id. 所以你不能有多个具有相同id的元素。 And you should sanitize your inputs to prevent sql injection. 你应该清理你的输入,以防止SQL注入。 Use mysql_real_escape_string() on user inputs that go in the database. 对数据库中的用户输入使用mysql_real_escape_string()

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). 另请注意,在变量中创建这样的查询,允许您输出创建的查询,以便您可以查看究竟发送到MySQL的内容,还可以在不同的环境中测试查询(即在phpmyadmin或任何其他数据库管理中)工具)。

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

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