0

I am working on a checkbox table, the submitted data should be saved in the mysql database. There are two columns in the table, auto-increment Id and another one for saving the values of the checkbox.

Currently it is only saving one value at a time even when I select multiple, only the latest one gets saved. Here's the code:

report.php

<?php


$active = "report";
require_once 'pages/header.php';
require_once './functions/schema-functions.php';

$course = Schema::getCourse();
$objective = Schema::getObjective();
?>




<form id="addReport" action ='./functions/report-functions.php' method="post">

<table id="table1" class="table">
    <?php
    echo '<tr><th>Objectives</th>';
    for ($i = 0; $i < count($course); $i++) {
        echo '<th id = "rotate1">'. $course[$i]->commonName . '</th>';            
    }
    echo '</tr>';

    for ($y = 0; $y < count($objective); $y++) {
        echo '<tr><th class=row-header>'.$objective[$y]->objective.'</th>';

    for ($x = 0; $x < count($course); $x++) {

        echo "<td><input name='check[]' type=checkbox value=c".$course[$x]->courseId."-o".$objective[$y]->objectiveId." id=checked></td>";

        }
        echo '</tr>';
    }
    ?>

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

report-functions.php

<?php


 require_once 'db-connect.php';

if(isset($_POST['submit'])){
{


$conn = DatabaseConnection::getConnection();
    $conn->beginTransaction();
    if(isset($_POST['check'])){
   foreach($_POST['check'] as $value){
    $sql = $conn->prepare("INSERT INTO Report (ColRow) VALUES 
 ('$value')");
  }
    if ($sql->execute(array( ':checked' => $checked))) {
        $conn->commit();
        return true;
    } else {
        $conn->rollback();
        return false;
    }

   }
   }
   }
    ?>

I want to save multiple selected checkboxes in different rows of the database and after submit, I want to display the table again with the boxes being checked and the user should be able to make the checks and submit the data again.

5
  • 1
    execute(array( ':checked' => $checked)) You don't use the :checked bound parameter and you don't defined the $checked variable. Honestly, I wouldn't expect any rows to be inserted. Also, you start the transaction outside of the loop, but commit inside the loop. That kind of defeats the purpose of having the transaction. Commented Apr 8, 2019 at 14:46
  • It is still saving single value, when I select multiple the last one gets saved Commented Apr 8, 2019 at 15:01
  • "still" after making what changes exactly? Commented Apr 8, 2019 at 15:11
  • I meant the above code is saving a single record, even with the errors which you mentioned Commented Apr 8, 2019 at 15:14
  • Can you print exactly how the code should be? to save multiple values in a single submit? Commented Apr 8, 2019 at 15:17

1 Answer 1

1

For saving the records, you might try like this:

<?php

    if( isset( $_POST['submit'], $_POST['check'] ) ){
        try{

            require_once 'db-connect.php';
            $conn = DatabaseConnection::getConnection();


            $sql='insert into `report`  ( `colrow` ) values ( :value )';
            $stmt = $conn->prepare( $sql );



            if( $stmt ){

                $conn->beginTransaction();

                foreach( $_POST['check'] as $index => $value ) {
                    $result = $stmt->execute( [ ':value' => $value ] );
                    if( !$result ) {
                        throw new Exception( sprintf( 'Failed to execute query %d for %s', $index, $value ) );
                    }
                }

                $conn->commit();
                exit();
            }
        }catch( Exception $e ){
            $conn->rollback();
            exit( $e->getMessage() );
        }
    }
?>
Sign up to request clarification or add additional context in comments.

9 Comments

Can you help with the other part too?
what "other part"?
Being able to edit and delete the entries... now when the user submits the table, the page should load the same table with the checked boxes and the ability to make changes and re-submit it
You might be better to ask a new question with the appropriate code. The "other part" seems like there are several moving pieces, all fairly simple granted but too broad to update the above and for it to still make sense in relation to the initial question.
Hey @RamRaider can you please help with this question - stackoverflow.com/questions/55579816/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.