How to insert Checkbox value to database

Hi, I want to know how to insert checkbox value to database. I want the database identify their value on the checkbox. The value is name=“tm01” on the checkbox. When it declares the value on the checkbox, it adds + 1 as update table integer value field on the database.Thank you, I need your help asap.

database name:poll
table name:candidate
field name for add +1 value: votecount
field name:can_id(same as name=“tm01” on checkbox

What have you tried?

2 Likes

It seems like each candidate from table candidate gets +1 even I checked for 4 candidates only.

if (isset($_REQUEST['tm01'])){
        // removes backslashes
	$tm01 = stripslashes($_REQUEST['tm01']);
        //escapes special characters in a string
	$tm01 = mysqli_real_escape_string($con,$tm01); 
	$tm02 = stripslashes($_REQUEST['tm02']);
	$tm02 = mysqli_real_escape_string($con,$tm02);
	$tm03 = stripslashes($_REQUEST['tm03']);
	$tm03 = mysqli_real_escape_string($con,$tm03);
	$tm04 = stripslashes($_REQUEST['tm04']);
	$tm04 = mysqli_real_escape_string($con,$tm04);
	$tm05 = stripslashes($_REQUEST['tm05']);
	$tm05 = mysqli_real_escape_string($con,$tm05);
	$tm06 = stripslashes($_REQUEST['tm06']);  
	$tm06 = mysqli_real_escape_string($con,$tm06);
	

$query ='UPDATE candidate SET votecount = votecount + 1 WHERE cand_id =
 "tm01" OR cand_id = "tm02" OR cand_id = "tm03" OR cand_id = "tm04" OR 
 cand_id = "tm05" OR cand_id = "tm06" ';
 $result = mysqli_query($con,$query);
  if(($result)==1){
  echo"<center><p>Congratulations. You have made your vote.</p></center>";
  exit();
  }
  } ?>

No.

create table candidate(votecount int, cand_id text);
insert into candidate(votecount, cand_id) values(1, "tm01");
insert into candidate(votecount, cand_id) values(2, "tm02");
insert into candidate(votecount, cand_id) values(3, "tm03");
insert into candidate(votecount, cand_id) values(4, "tm04");
insert into candidate(votecount, cand_id) values(5, "tm05");
insert into candidate(votecount, cand_id) values(6, "tm06");
insert into candidate(votecount, cand_id) values(7, "tm07");
insert into candidate(votecount, cand_id) values(8, "tm08");

select * from candidate;

UPDATE candidate SET votecount = votecount + 1 WHERE cand_id =
 "tm01" OR cand_id = "tm02" OR cand_id = "tm03" OR cand_id = "tm04" OR 
 cand_id = "tm05" OR cand_id = "tm06";

select * from candidate;

votecount	cand_id
1	tm01
2	tm02
3	tm03
4	tm04
5	tm05
6	tm06
7	tm07
8	tm08

votecount	cand_id
2	tm01
3	tm02
4	tm03
5	tm04
6	tm05
7	tm06
7	tm07
8	tm08

https://kripken.github.io/sql.js/GUI/

whatever problem you have does not rely on the code you posted.

1 Like

Why do you extract all those checkbox entries into variables, but then not use them in the query?

1 Like

can you show me how? Thank you.

Thank you, I’ll try use your answer.

Well, add the values of those variables in, instead of hard-coding the ids. You could also look at using the IN clause in your query, once you have it working.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.