...

View Full Version : Update mysql database when dynamic checkboxes were altered in PHP



rajesh007king
07-24-2012, 05:34 PM
hi i have a table with 3 fields. Airlines service, Id and Blacklist(0 or 1). when i fetch the data from the table i ll get the name of the airline service and there blacklist status as checked if the value is 1 and unchecked if the value is 0. after fetching the results if i make any changes and submit the changes i need to update my database accordingly. i am using mvc architecture in my website. can any one help me....

thanks in advance.


Blacklist.Phtml:


<?php
//require('template/adminheader.phtml')?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<h1>Blacklist</h1>
<ul></ul>
<table class="table" border="2" cellspacing="5" cellpadding="5">
<thead>
<tr>
<th>Checked</th>
</tr>
</thead>
<td><?php foreach ($view->peopleList as $person):?></td>
<tr>
<td><input id="$person->getBlacklist()"value="<?php $person->getID(); ?>" name="Airlines[]" type="checkbox" <? if ($person->getBlacklist()==="1") echo "checked=checked"; ?> class="span2">
<?php echo $person->getService_Name();?> </input> </td>
</tr>
<?php endforeach; ?>
</table>
</ul>
</table>
<button name="submit" type="submit" class="btn btn-large">S</button>
</form>



Blacklist.php


<?php
$view->pageTitle = 'Database';
require_once('Models/class.logintable.php');
require_once('Models/class.rulesdef.php');
$PNR=$_GET['PNR'];
$database1 = new rulesdef();
$view->peopleList = $database1->Blacklist();
//$view1->peopleList = $database1->ischecked();
if(isset($_POST['submit']))
{
if($view->err=='')
{
$view->laptops=array();
$dbupdate=new logintable();
$dbupdate->updateitemsdetails1($_POST);
//$view->laptops=$dbupdate->getDetails();
}
}
require_once('Views/Blacklist.phtml');



logintable.php

public function updateitemsdetails1($data)
{
foreach ($_POST['Airlines'] as $value)
{
print_r($_POST['Airlines[]']);

//print_r($ids);
// if(isset ($_POST['Airlines']))
// {
//
// $n=$_POST['id'];
// $va='1';
// $sql="UPDATE Rules SET Blacklist='$va' WHERE Service_Name='British Airlines'";
// $results=$this->dbh->prepare($sql);
// $results->execute(array());
// }
// else
// {
// $va='0';
// $sql="UPDATE Rules SET Blacklist='$va' WHERE Service_Name='Brussils'";
// $results=$this->dbh->prepare($sql);
// $results->execute(array());
//
// }
// elseif(!isset ($_POST['Airlines']))
// {
//
// //alert("king");
// $va='1';
// $sn=$_POST['Airlines'];
//
// $sql="UPDATE Rules SET Blacklist='0' WHERE Service_Name='Brussils";
// $results=$this->dbh->prepare($sql);
// $results->execute(array());
//
// }
}
}



rulesdef.php

<?Php
require_once 'Models/class.rules.php';
require_once 'Models/class.database.php';
class rulesdef
{
protected $dbh;
protected $db;
public function __construct() {
$this->db=Database::getInstance();
$this->dbh=$this->db->getDbh();
}

public function Blacklist()
{
$sql="select * from Rules order by ID ";
$results= $this->dbh->prepare($sql);
$results->execute();
$personArray=array();

while($row=$results->fetch())
{
$personArray[]=new Rules($row);

}
return $personArray;
print_r($_POST['Airlines[]']);
}
}

Keleth
07-24-2012, 05:39 PM
Welcome to CF; please read the stickies on the forums and put code in appropriate tags ([php] or [code]).

That being said, you've thrown us a bunch of code, but told us nothing of whats wrong with it. We know what you're expecting, but whats happening/what are you trying to fix?

rajesh007king
07-24-2012, 05:41 PM
actully i need to update the altered checkbox values to my database.

any sort of help will be appreciated. i am in rush to finish my project.

Keleth
07-24-2012, 06:53 PM
Well, your control structure is completely out of whack and doesn't take any of your airlines into account.



if(isset ($_POST['Airlines'])) {
// stuff
} else {
// stuff
} if(!isset ($_POST['Airlines'])) {
// stuff
}

That makes no sense, let alone making sense for the queries you have written within.

You're saying if $_POST['Airlines'] is set, do something with British Airlines. Otherwise do something with Brussils. And if its $_POST['Airlines'] is not set and its not not set, then check if its not set and do something else. Do you see how its nonsensical? else is a catch all for the above if statements. You can't go further then an else with a conditional tree. Plus, you have all that in a loop cycling through $_POST['Airlines']. If $_POST['Airlines'] is not set and is not an array, it won't even get to your conditional.

Next, your HTML won't validate... you have end quotes next to letters with no space between it ( id="$person->getBlacklist()"value="<?php $person->getID(); ?>"). It means you may or may not get the responses you expect.

Finally, you're sending a bunch of IDs to the processing code. As it seems they're the airline IDs, just toggle them directly?


$sql="UPDATE Rules SET Blacklist=1 XOR Blacklist WHERE id='$value'";

Replace id with whatever the id column is called.

Did you write this code yourself?

rajesh007king
07-25-2012, 03:35 AM
i am unable to understand how to get the id of the specific airways i am blacklisting. so to test whether my code is working i gave the condition to change the value of british airways if there is any effect taking place in the checkbox. and once i realised the function is working and i need to make it generic for every checkbox, i commented those lines .

above all thank you very much for your quick reply. i am new to php but very enthusiastic to solve issues my self. after giving all my efforts for two days and reading almost every forum available i posted my question. i am sorry to say but i wrote all the code that i posted.

Keleth
07-25-2012, 04:20 PM
That's nothing to be ashamed of, the point is to learn. The only reason I asked if you wrote the code is that it seems like you don't understand what your own code is doing, and that worries me a bit.

You're already sending the ids to the processing code, so I'm not getting where the trouble is. You say you don't know how to get the specific id's you're blacklisting, but you're already doing it. So either you copied from a template, followed a guide you didn't fully grasp, or don't really understand what you're doing. None of them are bad inherently bad things, as long as you learn from what you're doing.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum