- Joined
- Jun 27, 2009
- Messages
- 1,571
- Reaction score
- 170
function checkMaintenance()
{
global $config, $dbh;
if (isset($_POST['checkMaintenance']))
{
switch ($_POST['siteOption']) {
case 1:
$siteMode = "Site Open";
break;
case 2:
$siteMode = "Site Closed";
break;
}
$checkMaintenance = $dbh->prepare("INSERT INTO cms_system (site_closed) VALUES(:siteOption)");
$checkMaintenance->bindParam(':siteMode', $siteMode);
$checkMaintenance->execute();
Admin::succeed("Maintenance Mode Enabled");
}
}
<form action="" method='POST' name='theAdminForm' id='theAdminForm'>
<div class='tableborder'>
<div class='tableheaderalt'>Turn your site on/off</div>
<?php admin::checkMaintenance(); ?>
<table width='100%' cellspacing='0' cellpadding='5' align='center' border='0'>
<tr>
<td class='tablerow1' width='40%' valign='middle'><b>Close Site</b><div class='graytext'>If enabled, your site will be closed and show a maintenance page to regular users. Administrators can still login through Housekeeping.</div></td>
<td class='tablerow2' width='60%' valign='middle'>
<select name="checkMaintenance" class='dropdown'>
<option name="siteOption" value='1'>Site Open</option>
<option name="siteOption" value='2'>Site Closed</option>
</select>
</td>
</tr>
<tr>
<tr><td align='center' class='tablesubheader' colspan='2' ><input type='submit' value='Apply' class='realbutton' accesskey='s'></td></tr>
</form>
CREATE TABLE `cms_system` (
`systemVar` varchar(50) NOT NULL,
`value` text DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `cms_system`
--
INSERT INTO `cms_system` (`systemVar`, `value`) VALUES
('ip', '127.0.0.1'),
('language', 'en'),
('localhost', '0'),
('reload_url', 'http://127.0.0.1/holov3/client.php'),
('site_closed', '0');
Your placeholder in the "INSERT INTO" query is ":siteOption" but you're binding a variable to a placeholder named ":siteMode"
function checkMaintenance() {
global $config, $dbh;
if (isset($_POST['checkMaintenance'])) {
$siteOption = $_POST['siteOption'];
$checkMaintenance = $dbh->prepare("INSERT INTO cms_system (site_closed) VALUES(:siteOption)");
$checkMaintenance->bindParam(':siteOption', ($siteOption == 1)?(0):(1));
$checkMaintenance->execute();
Admin::succeed("Maintenance Mode Enabled");
}
}
You're setting $siteMode to a string when it should be a true/false integer. 0 or 1, but since you're checking if the option is 1/2, I assume 1 = maintenance, 2 = normal.
PHP:function checkMaintenance() { global $config, $dbh; if (isset($_POST['checkMaintenance'])) { $siteOption = $_POST['siteOption']; $checkMaintenance = $dbh->prepare("INSERT INTO cms_system (site_closed) VALUES(:siteOption)"); $checkMaintenance->bindParam(':siteOption', ($siteOption == 1)?(0):(1)); $checkMaintenance->execute(); Admin::succeed("Maintenance Mode Enabled"); } }
Fatal error: Uncaught Error: Cannot pass parameter 2 by reference in C:\xampp\htdocs\system\app\classes\class.admin.php:365 Stack trace: #0 C:\xampp\htdocs\adminpan\sitesettings.php(24): Admin::checkMaintenance() #1 C:\xampp\htdocs\system\app\classes\class.html.php(227): include('C:\\xampp\\htdocs...') #2 C:\xampp\htdocs\adminpan\index.php(10): html::pageHK() #3 {main} thrown in C:\xampp\htdocs\system\app\classes\class.admin.php on line 365
Why are you inserting and not updating? I'm guessing there's already a table row with all the settings. You should be updating that row.
Also how does the CMS check if the site is in maintenance? Is it a boolean check (true/false), an integer check (1/2/...) or a varchar check ('open'/'closed' or 'true'/'false' or something)?
UPDATE cms_system SET site_closed = :siteOption
':siteOption', $siteOption?'open':'closed'
Try this code in the prepare statement:
Code:UPDATE cms_system SET site_closed = :siteOption
Also change your bindParam to:
Code:':siteOption', $siteOption?'open':'closed'
Fatal error: Uncaught Error: Cannot pass parameter 2 by reference in C:\xampp\htdocs\system\app\classes\class.admin.php:371 Stack trace: #0 C:\xampp\htdocs\adminpan\sitesettings.php(24): Admin::checkMaintenance() #1 C:\xampp\htdocs\system\app\classes\class.html.php(227): include('C:\\xampp\\htdocs...') #2 C:\xampp\htdocs\adminpan\index.php(10): html::pageHK() #3 {main} thrown in C:\xampp\htdocs\system\app\classes\class.admin.php on line 371
$checkMaintenance->bindParam(':siteOption', $siteOption?'open':'closed');
So Im getting this error.
PHP:Fatal error: Uncaught Error: Cannot pass parameter 2 by reference in C:\xampp\htdocs\system\app\classes\class.admin.php:371 Stack trace: #0 C:\xampp\htdocs\adminpan\sitesettings.php(24): Admin::checkMaintenance() #1 C:\xampp\htdocs\system\app\classes\class.html.php(227): include('C:\\xampp\\htdocs...') #2 C:\xampp\htdocs\adminpan\index.php(10): html::pageHK() #3 {main} thrown in C:\xampp\htdocs\system\app\classes\class.admin.php on line 371
On line 371 is this code
PHP:$checkMaintenance->bindParam(':siteOption', $siteOption?'open':'closed');
Unlike PDOStatement::bindValue(), the variable is bound as a reference and will only be evaluated at the time that PDOStatement::execute() is called.
Right,
Either use bindValue instead of bindParam.
Or keep using bindParam, but define the 2nd value ($siteOption?'open':'closed') as a parameter before binding it there.
Which one do you recommend? bindValue or bindParam?
If I decide to keep bindParam, how would define the 2nd value before binding?
Im learning all this of this for educational purpose, all assistance will be credited.
UPDATE:
I changed bindParam to bindValue. its still not updating the database.
I'm just curious, why INSERT instead of UPDATE?
function checkMaintenance()
{
global $config, $dbh;
if (isset($_POST['checkMaintenance']))
{
switch ($_POST['checkMaintenance']) {
case 'open':
default:
$siteMode = "open";
break;
case 'closed':
$siteMode = "closed";
break;
}
// Check if there's already a row
$checkRow = $dbh->prepare("SELECT * FROM cms_site");
$checkRow->execute();
// If the row already exists, update
if ($checkRow->fetch()) {
$checkMaintenance = $dbh->prepare("UPDATE cms_site SET site_closed = :siteOption");
$checkMaintenance->bindParam(':siteOption', $siteMode);
$checkMaintenance->execute();
}
// Else, insert
else {
$checkMaintenance = $dbh->prepare("INSERT INTO cms_site VALUES (:siteOption, '')");
$checkMaintenance->bindParam(':siteOption', $siteMode);
$checkMaintenance->execute();
}
if ($siteMode === 'open') {
Admin::succeed("Maintenance Mode Disabled");
}
else {
Admin::succeed("Maintenance Mode Enabled");
}
}
}
I give full credit to Joorren for this.
PHP:function checkMaintenance() { global $config, $dbh; if (isset($_POST['checkMaintenance'])) { switch ($_POST['checkMaintenance']) { case 'open': default: $siteMode = "open"; break; case 'closed': $siteMode = "closed"; break; } // Check if there's already a row $checkRow = $dbh->prepare("SELECT * FROM cms_site"); $checkRow->execute(); // If the row already exists, update if ($checkRow->fetch()) { $checkMaintenance = $dbh->prepare("UPDATE cms_site SET site_closed = :siteOption"); $checkMaintenance->bindParam(':siteOption', $siteMode); $checkMaintenance->execute(); } // Else, insert else { $checkMaintenance = $dbh->prepare("INSERT INTO cms_site VALUES (:siteOption, '')"); $checkMaintenance->bindParam(':siteOption', $siteMode); $checkMaintenance->execute(); } if ($siteMode === 'open') { Admin::succeed("Maintenance Mode Disabled"); } else { Admin::succeed("Maintenance Mode Enabled"); } } }
Is it working?