简体   繁体   中英

Checking other checkboxes when clicking on a checkbox and saving them to MySQL

I'm working with MySQL and when you click on the checkboxes, they save their data/value (tinyint(1)) to the database table, so it'll be shown as checkbox either checked or not checked.

Simply said now I want to for examples:

  • When clicking on Grade 3 that also Grade 2, Grade 1 and Obtained will be checked and saved.
  • When clicking on Grade 2, only Grade 2 and Grade 1 will be checked
  • This also means that if for example all are checked (Obtained, Grade 1, Grade 2 and Grade 3) and you would uncheck Grade 2, that both Grade 3 and Grade 4 will unchecked too

I tried some scripts before but if I clicked for example Grade 3, both Grade 1 and Grade 2 would be checked but when I refreshed the page only gradetwo would have changed. So that it doesn't matter if I close the browser, change the browser, the checkboxes are still checked the way I left them. I sadly don't know much about coding to fix this.

To give a little more details: I'm trying to make something like a Equipment Tracker? for a game. There are 100+ weapons, armors etc inside that game and I put all of them inside my MySQL database, all of them have a unique ID, the name of the equipment and 4 checkboxes which will indicate if I have obtained this and then upgraded them up to maximum Grade 3.

For example weapons look like this in the table: https://imgur.com/tIv5wSe

Name - [1]Obtained - [2]Grade 1 - [3]Grade 2 - [4]Grade 3

Weapon 01 - [1] - [2] - [3] - [4]
~
Weapon 04 - [X] - [X] - [X] - [X]
~
Weapon 07 - [X] - [X] - [3] - [4]

If I were to sell a Grade 3 Weapon (Weapon 04 for example), it means I will lose it and would have to obtain it again, so by clicking op [1], it'll also remove [2][3][4].

Hopefully this makes sense and that someone can help me with this.

Down below are some pieces of the code I'm using currently to hopefully give you some idea.

Part of Index.php

echo "<div id=msg_display></div>";

$q="
SELECT id,name
     , if( obtained=true,'checked','') as obtained
     , if( gradeone=true,'checked','') as gradeone
     , if( gradetwo=true,'checked','') as gradetwo
     , if( gradethree=true,'checked','') as gradethree
  FROM weapons
";


echo "<table class='table table-striped'> ";
$th="<tr class='info'> <th>Name</th><th>obtained</th><th>gradeone</th><th>gradetwo</th><th>gradethree</th></tr>";
echo $th; 
$i=1;
if ($result_set = mysqli_query($connection,$q)) {
while($row = $result_set->fetch_array(MYSQLI_ASSOC)){
echo "<tr><td>$row[name]</td>
<input type=checkbox data-column_name='obtained' data-id='$row[id]' $row[obtained]>
<input type=checkbox data-column_name='gradeone' data-id='$row[id]' $row[gradeone]>
<input type=checkbox data-column_name='gradetwo' data-id='$row[id]' $row[gradetwo]>
<input type=checkbox data-column_name='gradethree' data-id='$row[id]' $row[gradethree]>
</tr>";

";
$i=$i+1;
if(fmod($i,10)==0){//echo $th;
}
}
 $result_set->close();
}

Script inside Index.php

<script>
$(document).ready(function() {
////////////////////    
$('input[type="checkbox"]').change(function(){
var column_name=$(32).data('column_name');
var id=$(this).data('id');
$.post( "data-check.php", {"column_name":$(this).data('column_name'),"id":$(this).data('id')},function(return_data,status){
$("#msg_display").html(return_data);
$("#msg_display").show();
setTimeout(function() { $("#msg_display").fadeOut('slow'); }, 5000);
});
});
//////////////////////////
});
</script>

data-check.php

<?php
$column_name=$_POST['column_name'];
$id=$_POST['id'];
if(!ctype_alpha($column_name)){
echo " Data error ";
exit;
}
include "config-mysqli.php"; // database connection details stored here
$q=" 
UPDATE weapons 
   SET $column_name = !$column_name 
 WHERE id=?
 ";
$stmt = $connection->prepare($q);
if($stmt){
$stmt->bind_param('i',  $id);
$stmt->execute();   
$msg="Data Updated for : $column_name ";    
}else {
$msg="No Data Updated for : $column_name ";     
}
echo "$msg";
?>

Consider the following which, if nothing else, will at least demonstrate that I am terrible at writing code...

DROP TABLE IF EXISTS inventory;

CREATE TABLE inventory
(user_id INT NOT NULL
,weapon_id INT NOT NULL
,level_code TINYINT NOT NULL
,PRIMARY KEY(user_id,weapon_id)
);

INSERT INTO inventory VALUES (1,101,1),(1,102,2),(1,103,3);
INSERT INTO inventory VALUES (2,104,3);

DROP TABLE IF EXISTS weapons;

CREATE TABLE weapons
(weapon_id SERIAL PRIMARY KEY
,weapon_name VARCHAR(12) UNIQUE
);

INSERT INTO weapons VALUES
(101,'nudger'),
(102,'poker'),
(103,'blaster'),
(104,'pulverizer');

By inspection, we can see that user 1 owns all weapons except the pulverizer.

<?php

require('path/to/pdo/connection/statme.nts');

$query = "
SELECT w.weapon_id
     , w.weapon_name
     , COALESCE(i.level_code,0) level
  FROM weapons w
  LEFT
  JOIN inventory i
    ON i.weapon_id = w.weapon_id
   AND i.user_id = 1
 ORDER
    BY weapon_id;
";
$stmt  = $pdo->prepare($query);
$stmt->execute();
$data  = $stmt->fetchAll();

?>

<table>
   <tr>
    <th>weapon name</th>
    <th>Grade I</th>
    <th>Grade II</th>
    <th>Grade III</th>
    <th>Grade IV</th>
   </tr>

<?php

$flag1 =0;
$flag2 =0;
$flag3 =0;
$flag4 =0;

foreach( $data as $v ) {
if($v['level']>=1) {$flag1=1;} else {$flag1=0;}
if($v['level']>=2) {$flag2=1;} else {$flag2=0;}
if($v['level']>=3) {$flag3=1;} else {$flag3=0;}
if($v['level']>=4) {$flag4=1;} else {$flag4=0;}

  echo "<tr>
         <td>".$v['weapon_name']."</td>
         <td>$flag1</td>
         <td>$flag2</td>
         <td>$flag3</td>
         <td>$flag4</td>
        <tr>";
}
?>

</table>

Outputs:

weapon name Grade I Grade II Grade III Grade IV
nudger         1       0        0         0
poker          1       1        0         0
blaster        1       1        1         0
pulverizer     0       0        0         0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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