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ó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á</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.
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 likeNote 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).