Solved

Hide or Show Images using checkbox array in mysql database

Posted on 2014-01-18
11
313 Views
Last Modified: 2016-05-17
Hi, I have a group of images that I want to show (make visible) or hide using checkbox array in php/mysql (see screen1).

The database:

visible bit(1) =  show images
visible bit(0) =  hide images

PHP code:
  if($row['visable']==1){
                echo'<td align="center"><input type="checkbox" name="imageCheck[]" id="imageCheck[]" checked='.$chk.'  value="'.$row['id'].'"><br />'.$chk.'</td>';
            }else{
                echo'<td align="center">                   
                    <input type="checkbox" name="unimageCheck[]" id="unimageCheck[]" value="'.$row['id'].'"><br />'.$chk.'</td>';
            }

Open in new window

using a submit button:

          <input name="check" type="submit" id="check" value="Update"></td>



Code for updating database:

if (isset($_POST['check'])){ 
               
 // hide images
        $imageCheck = isset($_POST['imageCheck']);
        $checkbox = $_POST['imageCheck']; //from name="checkbox[]"
        $countCheck = count($_POST['imageCheck']);
        for($i=0;$i<$countCheck;$i++) {
        $cid  = $checkbox[$i];    

// Create Query: if checkbox is unchecked make image visible=0
            $sql="UPDATE images SET visible = 0 WHERE images.id = $cid " ;
            $row = @mysqli_query ($mysqli, $sql);
            echo '<meta http-equiv="refresh" content="3;url=../admin/admin_edit.php?id='.$id.'">';
        }

//show test results
        echo 'hide';
        echo '<pre>';
        print_r($_POST['imageCheck']);
        echo '</pre>';    
  
 
//show images
        $unimageCheck = isset($_POST['unimageCheck']);
        $uncheckbox = $_POST['unimageCheck']; //from name="checkbox[]"
        $countCheck = count($_POST['unimageCheck']);
        for($i=0;$i<$countCheck;$i++) {
        $uncid  = $uncheckbox[$i];

// Create Query: if checkbox is checked make image visible=1
            $sql2="UPDATE images SET visible = 1 WHERE images.id = $uncid " ;
            $row = @mysqli_query ($mysqli, $sql2);
            echo '<meta http-equiv="refresh" content="3;url=../admin/admin_edit.php?id='.$id.'">';
        }

//show test results
        echo 'show';
        echo '<pre>';
        print_r($_POST['unimageCheck']);
        echo '</pre>';    
}

?>

Open in new window

I can get the code to work independently but cant get them to work together. I’m really out of the depth here and been working on this for the past 2 months. I have tried many different 'if' statements but still no joy. Need help.
screen1.JPG
screen2.JPG
0
Comment
Question by:mrwells
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
11 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39791140
Is it
visable if($row['visable']==1){
or
visible  $sql="UPDATE images SET visible = 0 WHERE images.id = $cid " ;
0
 

Author Comment

by:mrwells
ID: 39791158
sorry type error in the question. in all casses it is 'visible'
0
 
LVL 58

Expert Comment

by:Gary
ID: 39791165
Confused about what you want.
Do you want the images to be hidden on the screenshots you gave if the Visible checkbox is not checked.
Else explain more clearly what/where you are going wrong.
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39791171
Please, avoid to use @ before statements: that sign suppresses any error message and while debugging it's the most bad thing you can do. Place instead at the top of your scripts

error_reporting(E_ALL)

so you'll be notified about any small problem in your code.

Secondly, you should use mysqli_error() function:

    $sql2="UPDATE images SET visible = 1 WHERE images.id = $uncid " ;
    if (!mysqli_query ($mysqli, $sql2))
    {
        printf("Error message: %s\n", $mysqli->error);
    }

Open in new window

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39791179
You don't need to write

$row = mysqli_query....

since you're updating the table and not selecting records.

You should also post here the whole code and the error messages you get once you have fixed the script as I said above (error_reporting and suppressing @ sign)
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39791183
We also need to see the code where you select check values from the database and build your form with checkboxes check or unchecked accordingly to the query results.
0
 

Author Comment

by:mrwells
ID: 39791190
I'd like the user to check the ckeckbox if they want the image 'visible' or 'hidden' on the website;

if the images checkbox is checked then make the image 'visible' (visible=1):

<input type="checkbox" name="imageCheck[]" id="imageCheck[]" checked='.$chk.' value="'.$row['id'].'"><br />'.$chk.'</td>

$sql2="UPDATE images SET visible = 1 WHERE images.id = $uncid " ;
$row = @mysqli_query ($mysqli, $sql2);

if the images checkbox is unchecked then 'hide' the image (visable=0):

<input type="checkbox" name="unimageCheck[]" id="unimageCheck[]" value="'.$row['id'].'"><br />'.$chk.'</td>

 $sql="UPDATE images SET visible = 0 WHERE images.id = $cid " ;
 $row = @mysqli_query ($mysqli, $sql);
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39791211
First off a checkbox doesn't send a value if it is unchecked - so you cannot check this with the posted values.  The only checkboxes you will get posted are the ones that are checked.

Depending on if this page shows all the images, then you could initially set them all to invisible and then to set the ones that should be visible

foreach($_POST['imageCheck'] as $id){
     $sql2="UPDATE images SET visible = 1 WHERE images.id = $id" ;
     mysqli_query ($mysqli, $sql2);
}
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39791419
@Gary: One thing you can do to identify unchecked checkboxes is have a hidden input control of the same name in the HTML form before the checkbox.  The way PHP constructs the $_REQUEST-type arrays from the raw request data is always from top-to-bottom, so it will set the hidden value first, then if the checkbox is fired, it will overwrite the hidden value with the checkbox value.  This article shows how to do it.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_5450-Common-Sense-Examples-Using-Checkboxes-with-HTML-JavaScript-and-PHP.html
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question