PHP: add multiple checkbox values โ€‹โ€‹to a single MySQL column

I want to write a simple PHP function to insert values โ€‹โ€‹of 10 and 20 flags. Now the problem is this: should I insert all the values โ€‹โ€‹in one column of the MySQL table or should I go with a separate table?

My main goal is to insert the values โ€‹โ€‹of several checkboxes into MySQL and then update them. If I checked 7 checkboxes, and after some time I want to update from 7 to 5, how will it remove the values โ€‹โ€‹from the table column?

Please help me with some simple PHP example and what type of MySQL fields I should add, because I want to insert a flag value, which should be in the digital and checkbox labels in another field.

Here is the HTML i

<form method="post" action=""> Games You Like: <br/> <input type="checkbox" name="games[]" value="1"><label>Football</label><br> <input type="checkbox" name="games[]" value="2"><label>Basket Ball</label><br> <input type="checkbox" name="games[]" value="3"><label>Pool</label><br> <input type="checkbox" name="games[]" value="4"><label>Rugby</label><br> <input type="checkbox" name="games[]" value="5"><label>Tennis</label><br> <input type="checkbox" name="games[]" value="6"><label>Cricket</label><br> <input type="checkbox" name="games[]" value="7"><label>Table Tennis</label><br> <input type="checkbox" name="games[]" value="8"><label>Hockey</label><br> <input type="submit" name="submit" value="submit"> </form> 
+5
source share
5 answers

Try this whole example,

Table structure

 CREATE TABLE IF NOT EXISTS `games` ( `id` int(12) NOT NULL AUTO_INCREMENT, `game_name` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1; 
 <?php include_once("yourconfig.php"); //include your db config file extract($_POST); $check_exist_qry="select * from games"; $run_qry=mysql_query($check_exist_qry); $total_found=mysql_num_rows($run_qry); if($total_found >0) { $my_value=mysql_fetch_assoc($run_qry); $my_stored_game=explode(',',$my_value['game_name']); } if(isset($submit)) { $all_game_value = implode(",",$_POST['games']); if($total_found >0) { //update $upd_qry="UPDATE games SET game_name='".$all_game_value."'"; mysql_query($upd_qry); } else { //insert $ins_qry="INSERT INTO games(game_name) VALUES('".$all_game_value."')"; mysql_query($ins_qry); } } ?> <form method="post" action=""> Games You Like: <br/> <input type="checkbox" name="games[]" value="1" <?php if(in_array(1,$my_stored_game)){echo "checked";}?>><label>Football</label><br> <input type="checkbox" name="games[]" value="2" <?php if(in_array(2,$my_stored_game)){echo "checked";}?>><label>Basket Ball</label><br> <input type="checkbox" name="games[]" value="3" <?php if(in_array(3,$my_stored_game)){echo "checked";}?>><label>Pool</label><br> <input type="checkbox" name="games[]" value="4" <?php if(in_array(4,$my_stored_game)){echo "checked";}?>><label>Rugby</label><br> <input type="checkbox" name="games[]" value="5" <?php if(in_array(5,$my_stored_game)){echo "checked";}?>><label>Tennis</label><br> <input type="checkbox" name="games[]" value="6" <?php if(in_array(6,$my_stored_game)){echo "checked";}?>><label>Cricket</label><br> <input type="checkbox" name="games[]" value="7" <?php if(in_array(7,$my_stored_game)){echo "checked";}?>><label>Table Tennis</label><br> <input type="checkbox" name="games[]" value="8" <?php if(in_array(8,$my_stored_game)){echo "checked";}?>><label>Hockey</label><br> <input type="submit" name="submit" value="submit"> </form> 

This is just a basic example and the request that I added in this example, you can learn from this basic example, and I think it is very useful for you ... if useful, than give the correct answer for this solution

Db Table Stored Output

Form output

+12
source

use the implode function to convert the returned array to a string, and then insert it into the database as usual

 if(isset($_POST['submit'])){ $result = implode(",",$_POST['games']); 

}

hope this helps you. Regards Imran Qasim

+1
source

I put the name of the game in brackets and changed their values โ€‹โ€‹to true:

 <form method="post" action=""> Games You Like: <br/> <input type="checkbox" name="games[Football]" value="true"><label>Football</label><br> <input type="checkbox" name="games[Basket]" value="true"><label>Basket Ball</label><br> <input type="checkbox" name="games[Pool]" value="true"><label>Pool</label><br> <input type="checkbox" name="games[Rugby]" value="true"><label>Rugby</label><br> <input type="checkbox" name="games[Tennis]" value="true"><label>Tennis</label><br> <input type="checkbox" name="games[Cricket]" value="true"><label>Cricket</label><br> <input type="checkbox" name="games[Table]" value="true"><label>Table Tennis</label><br> <input type="checkbox" name="games[Hockey]" value="true"><label>Hockey</label><br> <input type="submit" name="submit" value="submit"> </form> 

After submission, this will result in the following array:

 $_POST["games"] = Array ( [Tennis] => true [Cricket] => true [Table] => true ) // all other values false 

You can easily save and process this array if you save it as a JSON string in your database. To do this, you need one varchar or text column.

 json_encode($_POST["games"]) // {"Tennis":"true","Cricket":"true","Table":"true"} 
0
source

Please try this

 <?php if(isset($_POST['submit'])) { //in here you get games array $mygames = $_POST['games']; } ?> <form method="post" action=""> Games You Like: <br/> <input type="checkbox" name="games[]" value="1"><label>Football</label><br> <input type="checkbox" name="games[]" value="2"><label>Basket Ball</label><br> <input type="checkbox" name="games[]" value="3"><label>Pool</label><br> <input type="checkbox" name="games[]" value="4"><label>Rugby</label><br> <input type="checkbox" name="games[]" value="5"><label>Tennis</label><br> <input type="checkbox" name="games[]" value="6"><label>Cricket</label><br> <input type="checkbox" name="games[]" value="7"><label>Table Tennis</label><br> <input type="checkbox" name="games[]" value="8"><label>Hockey</label><br> <input type="submit" name="submit" value="submit"> </form> 
0
source

Firstly, very directly, I prefer to make such names from different flags

 BasketBall;Cricket;Tennis 

You can do it with

 implode(";",$array); 

and then write that in the mySQL table ...

To get these values โ€‹โ€‹just use

 $array = explode(";",$urVariable); 

For different values โ€‹โ€‹use

 numberOne = $array[1]; // returns Cricket 

to change any use of the value. Just get the values โ€‹โ€‹and rewrite to ";" the form..

I like it because itโ€™s easy to implement and does a great job!

0
source

Source: https://habr.com/ru/post/1209596/


All Articles