Ejardy
Ejardy

Reputation: 101

Saving the selected rows by checkbox to database in php

I have a query to display all the records of the ebooks table from the database.

I displayed it in a table with a checkbox from the first column.

I'm trying to save the rows from the selected checkbox in the table width the Name of School but I don't have any idea.

I'm just a beginner in php, any help is appreciated. Here is my codes:

HTML codes:

$result1 = mysql_query("SELECT * FROM school GROUP BY SCHOOL_NAME");

while($row1=mysql_fetch_array($result1)) {
?>
    <option><?php echo $row1['SCHOOL_NAME'];?></option>
<?php } ?>
</select>

<table class="table table-striped table-hover table-bordered datatable">
    <thead style="background: #a83535; color: black;">
    <tr>
        <th style="width: 50px;"></th>
        <th style="color: #3b3b3b;">Ebook Title</th>
        <th style="color: #3b3b3b;">Category</th>
        <th style="color: #3b3b3b;">File Name</th>
    </tr>
    </thead>
    <tbody>
    <?php
    $result = mysql_query("SELECT * from ebooks ORDER BY EBOOK_TITLE");
    $count = mysql_num_rows($result);
    while($row=mysql_fetch_array($result)) {
    ?>
        <tr>
            <td>
                <center><input type="checkbox" name="check[]" ></center>
            </td>
            <td><input type="" name="ebookname[]" value="<?php echo $row['EBOOK_TITLE'];?>"></label></td>
            <td><input type="" name="ebookcategory[]" value="<?php echo $row['EBOOK_CATEGORY'];?>"></label></td>
            <td><input type="label" name="ebookfilename[]" value="<?php echo $row['EBOOK_FILENAME'];?>"></label></td>
        </tr>
    <?php } //end of while loop ?>
    </tbody>
</table>

<button type="submit" class="btn btn-hg btn-primary" name="AddEbooks">Submit</button>

I want to save the rows selected by the checkbox to database:

Here is the screenshot:

Upvotes: 0

Views: 5661

Answers (1)

princessjackie
princessjackie

Reputation: 565

First, you will need to assign an ID number to the checkboxes so that you can differentiate each record. Using the ID_NUMBER would be ideal here. In your current script where you are displaying the checkbox, your line that reads this:

<center><input type="checkbox" name="check[]" ></center>

Should include the ID_NUMBER like this in the name. Additionally, a value should be assigned to the checkbox so we can verify later.

<center><input type="checkbox" name="check[<?php echo $row['ID_NUMBER'];?>]" value="1"></center> 

Now you can go ahead and set up something that gets triggered when your form is submitted. You can essentially loop through each record and check it against the "check[ID_NUMBER]" checkbox to see if the value is 1. If the value is 1 then the record had been checked to save and you can then carry on saving the record.

How you want to handle the form is up to you - you could do AJAX to avoid reloading the entire page or you could do a simple form action="page_name_here.php" and handle the submission there. If you go about doing a simple form post without AJAX, it could be something like this -

Current page form tag:

<form name="" action="page_name_here.php" method="POST">

If you want to use the school_ebook var you need to assign the select options for the school_ebook a value:

<option value="<?php echo $row1['SCHOOL_NAME']'?>">

page_name_here.php:

<?php
    /* Get the school name */
    $school_ebook=$_POST["school_ebook"];
    /* Query your books DB to get the records */
    $result=mysql_query("SELECT * from ebooks ORDER BY EBOOK_TITLE");
    $count=mysql_num_rows($result);
    while($row=mysql_fetch_array($result)) {
        $bookID=$row['ID_NUMBER'];
        /* Get the Book ID and now we have to fetch from $_POST the value from the form */
        if (array_key_exists($bookID, $_POST["check"])) {
            $ischecked=$_POST["check"][$bookID];
            /* See if this has a value of 1.  If it does, it means it has been checked */
            if ($ischecked==1) {
                /* It is checked, so now in this area you can finish the code to retrieve the data from the row and save it however you like */
            }
        }
    }
?>

Once completed with your inserts, etc., it should accomplish what you need.

Upvotes: 2

Related Questions