Member Avatar for msolomon.ashish

Hello
iwant a script in php i describe my problem below

i checked all the checkbox and updated after updating i am getting values from enable field but when i checked randomly i am not getting updating i am getting different values to be checked

Here's the code

/* For connecting database connect.php */ /* database connection */ <?php $con=mysqli_connect("localhost","root","","multiple"); if(mysqli_connect_errno()) { echo "failed to connect".mysqli_connect_error(); } ?> */ <?php error_reporting(E_ALL ^ E_NOTICE); include_once("connect.php"); if(isset($_POST["submit"])) { $usersCount = count($_POST["id"]); for($i=0;$i<=$usersCount;$i++) { $query1=mysqli_query($con,"UPDATE multiple set enable='".$_POST["id1"][$i]."' where id='".$_POST["id"][$i]."'"); if($query1) { echo "updated"; } } } ?> <html> <head> </head> <body> <form action="index.php" method="post"> <?php include_once("connect.php"); $retrieve=mysqli_query($con,"select * from multiple ORDER BY id ASC "); while($row=mysqli_fetch_array($retrieve)) { $id=$row["id"]; $enable=$row["enable"]; ?> <input type="checkbox" name="id1[]" id="id1[]" value="enable" <?php if($enable=="enable") { echo 'checked' ; } ?> /> <input type="hidden" name="id[]" id="id[]" value="<?php echo $row["id"]; ?>" /> <?php echo $row["name"]; ?> <?php } ?> </br></br> <input type="submit" name="submit" id="submit" value="submit" /> </form> </body> </html> /* sql dump file */ -- phpMyAdmin SQL Dump -- version 4.1.12 -- http://www.phpmyadmin.net -- -- Host: 127.0.0.1 -- Generation Time: Nov 08, 2014 at 05:23 PM -- Server version: 5.6.16 -- PHP Version: 5.5.11 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; -- -- Database: `multiple` -- -- -------------------------------------------------------- -- -- Table structure for table `multiple` -- CREATE TABLE IF NOT EXISTS `multiple` ( `id` int(11) NOT NULL AUTO_INCREMENT, `firstname` varchar(255) DEFAULT NULL, `lastname` varchar(255) DEFAULT NULL, `email` varchar(255) DEFAULT NULL, `phone` varchar(255) DEFAULT NULL, `enable` varchar(255) DEFAULT NULL, PRIMARY KEY (id) ) ; -- -- Dumping data for table `multiple` -- INSERT INTO `multiple` (`id`, `firstname`, `lastname`, `email`, `phone`, `enable`) VALUES (3, 'ashish2', 'ashish2', 'ashish2', 'ashish2', 'enable'), (4, 'ashish3', 'ashish2', 'ashish3', 'ashish2', 'enable'), (5, 'anusha', 'anusha', 'anusha', 'anusha', ''), (6, 'madiri', 'madiri', 'madiri', 'madiri', ''); /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; 
Member Avatar for diafol

A problem with this setup is that only checked checkboxes are sent as post data, the unchecked ones are ignored so do not appear in the $_POST superglobal.

You can use the id value of the row in the checkbox itself - don't use a hidden input.

<input type="checkbox" name="id1[<?php echo $row["id"]; ?>]" id="id_<?php echo $row["id"]; ?>" value="enable" <?php if($enable=="enable") { echo 'checked' ; } ?> /> 

Or

<input type="checkbox" name="id1[]" id="id_<?php echo $row["id"]; ?>" value="<?php echo $row["id"]" <?php if($enable=="enable") { echo 'checked' ; } ?> /> 

The latter is probably easiest as you can then get all the ids (checked) from this:

$checkedArray = (array) $_POST['id1']; //will give an array of id values that are checked if(!empty($checkArray)) { $str = implode(",",$checkedArray); //run update query -see below } 

BUT that will not tell you which ones were changed from 1 to 0. You can however cover this in your SQL. I strongly suggest that you do not store the word 'enable' in your DB but change it to tinyint type and have value 1 and 0 (1 equiv to checked and 0 unchecked). So this:

<?php if($enable=="enable") { echo 'checked' ; } ?> 

Becomes:

<?php if($enable==1) { echo 'checked' ; } ?> 

The update query could then look like something like this:

UPDATE multiple SET `enable` = IF(id IN ($str),1,0) 

NOT TESTED

Member Avatar for msolomon.ashish

Hello It's not working for my form

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.