samedi 19 décembre 2020

How to put selected check box into SQL table?

I copy paste a code from the internet and modify a little bit according to my need and it's a messy code.

Below is the CheckBox.php

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://code.jquery.com/jquery.js"></script>
<style>
* {
  box-sizing: border-box;
}

/* Create two equal columns that floats next to each other */
.column {
  float: left;
  width: 50%;
  padding: 10px;
}

/* Clear floats after the columns */
.row:after {
  content: "";
  display: table;
  clear: both;
}

.footer {
   position: fixed;
   left: 0;
   bottom: 0;
   width: 100%;
   background-color: red;
   color: white;
   text-align: center;
}
</style>
</head>
<body>
<?php
$name = addslashes($name);
    
$servername = "localhost";
$username = "x";
$password = "y";
$dbname = "z";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);}
    
$sql = "SELECT NAMA, NOTA, BlnNota, SUM 
        FROM DarmaPangan 
        WHERE NAMA = 'ABENG SUPPLIER'
        GROUP BY BlnNota
        ORDER BY BlnNota ASC";
$result = $conn->query($sql);
?>


<form method="post" action="NotaTrf.php">
<div class="row">
  <div class="column" style="background-color:#aaa;"><h2>NILAI</h2></div>
  <div class="column" style="background-color:#bbb;"><h2>NOTA</h2></div>
 </div>
 <div class="row">
 <div class="column" style="background-color:#aaa;">
 
 <?php
 while($row = $result->fetch_assoc()){
    $nota=$row['NOTA'];
    $nilai=$row['SUM'];
    $kiri= '<input class="calc" type="checkbox" id=' .
            '"' . $nota . '" value=' .
            '"' . $nilai . '" name="trfnilai[]"/>' .
            $nilai . '<br>' ;
    echo $kiri ;
 }
 
echo '</div><div class="column" style="background-color:#bbb;">' ;
 
 $sql2 = "SELECT NAMA, NOTA, BlnNota, SUM 
        FROM DarmaPangan 
        WHERE NAMA = 'ABENG SUPPLIER'
        GROUP BY BlnNota
        ORDER BY BlnNota ASC";
$result2 = $conn->query($sql2);
    
 while($row2 = $result2->fetch_assoc()){
    $nota2=$row2['NOTA'];
    $nilai2=$row2['SUM'];
    $kanan= '<input class="t" type="checkbox" id=' .
            '"' . $nota2 . '" value=' .
            '"' . $nota2 . '" name="trfnota[]" />' .
            $nota2 . '<br>' ;
    echo $kanan ;
}       

echo '</div></div>' ;
?>

<input type="submit" name="submit" value="Submit">
</form>

<div class="footer">
  Total Score:<input type="text" name="sum" />
</div>

<script>
function calcscore(){
    var score = 0;
    $(".calc:checked").each(function(){
        score+=parseInt($(this).val(),10);
    });
    $("input[name=sum]").val(score)
}
$().ready(function(){
    $(".calc").change(function(){
        calcscore()
    });
});


$( ":checkbox" ).change(function() {
var id1 = "#" + this.id + ".calc"
var id2 = "#" + this.id + ".t"
  //alert(id2);
    if ($(id1).is(':checked')) {
    $(id2).prop('checked', true)}
    else{$(id2).prop('checked', false)}
});
     

</script>

</body>
</html>

After I run the CheckBox.php, the result is like I expected
enter image description here

And the page source for the rendered form is like this

<form method="post" action="NotaTrf.php">
<div class="row">
  <div class="column" style="background-color:#aaa;"><h2>NILAI</h2></div>
  <div class="column" style="background-color:#bbb;"><h2>NOTA</h2></div>
 </div>
<div class="row">
    <div class="column" style="background-color:#aaa;">
        <input class="calc" type="checkbox" id="PB-200317040" value="-908500" name="trfnilai[]"/>-908500<br>
        <input class="calc" type="checkbox" id="PB-200407005" value="-130000" name="trfnilai[]"/>-130000<br>
        <input class="calc" type="checkbox" id="PB-200504008" value="-1200000" name="trfnilai[]"/>-1200000<br>
        <input class="calc" type="checkbox" id="PB-200623010" value="-1200000" name="trfnilai[]"/>-1200000<br>
        <input class="calc" type="checkbox" id="PB-200814016" value="-2400000" name="trfnilai[]"/>-2400000<br>
    </div>
    <div class="column" style="background-color:#bbb;">
        <input class="t" type="checkbox" id="PB-200317040" value="PB-200317040" name="trfnota[]" />PB-200317040<br>
        <input class="t" type="checkbox" id="PB-200407005" value="PB-200407005" name="trfnota[]" />PB-200407005<br>
        <input class="t" type="checkbox" id="PB-200504008" value="PB-200504008" name="trfnota[]" />PB-200504008<br>
        <input class="t" type="checkbox" id="PB-200623010" value="PB-200623010" name="trfnota[]" />PB-200623010<br>
        <input class="t" type="checkbox" id="PB-200814016" value="PB-200814016" name="trfnota[]" />PB-200814016<br>
    </div>
</div>
<input type="submit" name="submit" value="Submit">
</form>


My SQL table header is NAMA - DATE - SUM, but I'm stuck on how to put the checked checkbox(es) of column NILAI into this table column SUM and the checked checkbox(es) of column NOTA into this table column NAMA within the respective row.

What I've tried so far, I copied a code from this link to a NotaTrf.php like below

<?php
$con=mysqli_connect("localhost","x","y","z");
  if (mysqli_connect_errno()){
  echo "Failed to connect to MySQL: " . mysqli_connect_error();}

$checkBox = implode(',', $_POST['trfnota']);
echo $checkBox . "<br>";

if(isset($_POST['submit']))
{       
    $query="INSERT INTO transferan (NAMA) VALUES ('" . $checkBox . "')";     
    mysqli_query($con,$query) or die (mysqli_error() );
    echo "Complete";
}

mysqli_close($con);
?>

The echo result from $checkBox = implode(',', $_POST['trfnota']);echo $checkBox"; is like this
: PB-200317040,PB-200814016

But when I browse the table in phpadmin in my website cpanel, in the table column NAMA the result is in one row like this :
PB-200317040,PB-200814016

which I thought the result will be in two rows (under column NAMA) like this :
PB-200317040
PB-200814016

Anyway, I think I can't use the code like that in the NotaTrf.php, because I think that code is meant to fill one column in the table, while my case is I need to fill two columns in the table. So the table result is look like this

NAMA ----------- SUM
PB-200317040 -908500
PB-200814016 -2400000

But I don't know how to do it and I don't know if it's even possible to do it.

Any help would be greatly appreciated.
Thank you in advanced.
Sorry for the long post because I want to be as clear as possible.




Aucun commentaire:

Enregistrer un commentaire