-2

Okay so I am running into an issue with my code since no matter what I change it to, it stays on the index page?

Here is my code

<?php
// Maintenance mode

$system = (mysql_query("SELECT * FROM system"));

if($system['maintenance'] == 1)
{
header('Location: /maintenance');
exit;
}
?>

I want it so if maintenance = 1 it would send you to the maintenance.php page but it doesn't can anyone help me with this? I tried,

if(!$system['maintenance'] == 1)

which works but I have this code in my maintenance.php page

<?php 
$system = (mysql_query("SELECT * FROM `system`"));

if(!$system['maintenance'] == 0)
{
header('Location: /index');
exit;
}
?>

What I thought would send me back to the index page once that in the database it shows it is a 0 or that's what I want it to do. Can anyone please explain why my code isn't working?

I am a beginner php, mysql, and C++ coder / programmer so please try to explain it the easiest way possible.

I fixed it thanks to @Script47. New code and works

<?php
$system = mysql_query("SELECT * FROM system");
$results = mysql_fetch_array($system);

if($results['maintenance'] == 1)
{
    header('Location: /maintenance');
    exit;
}
?>

The reason I do not need an extension for maintenance is because I have it set in .htaccess to ignore them, but for everyone to know it's a php file.

GrimmRP
  • 43
  • 8

2 Answers2

1

You need to actually fetch your results from the database. Look in to the link I provided. I will update my post with code.

    <?php
    $system = mysql_query("SELECT * FROM system");
    $results = mysql_fetch_row($system);

    if($results[yourColumnKey] == 1)
    {
        header('Location: /maintenance.php');
        exit;
    }
    ?>

http://php.net/manual/en/function.mysql-fetch-row.php

Edit 1

Your code is prone to SQL injection, you are still using MySQL even though it has been deprecated, you should use either MySQLi or PDO with prepared statements.

Edit 2

You don't seem to be specifying a file extension on your header();.

header('Location: /maintenance');
                            --^

What is maintenance? A PHP file? A HTML file? You need to specify an extension too.

Script47
  • 14,230
  • 4
  • 45
  • 66
  • I am having a hard time understanding that, how would I implant that into my page the first code is all I have in that *.php file so would I do something like `mysql_fetch_assoc($system['maintenance'] = 1`? – GrimmRP Aug 11 '15 at 23:29
  • Copy my code in to your file. Also in this part `$results[yourColumnKey]` change *yourColumnKey* to number at which maintenance show up. – Script47 Aug 11 '15 at 23:30
  • @GrimmRP check **Edit 1**, also if this answer helped. Please press the tick. – Script47 Aug 11 '15 at 23:33
  • Okay I put in the code you gave me and in the database I have it set at one shouldn't it be taking me to the maintenance page? – GrimmRP Aug 11 '15 at 23:36
  • Grimm, can't you have Script47 combine that with your [dupe](http://stackoverflow.com/questions/31932991/how-to-put-a-site-into-maintenance) question from last nite ? Dagon wrote up something nice too. Oh, **nice job**, Script, by the way :> – Drew Aug 11 '15 at 23:39
  • @GrimmRP what is *maintenance*? Is it a `PHP` file? An `HTML` file? You don't specify a file extension. – Script47 Aug 11 '15 at 23:40
  • @Script47 It's a `php` file yes, I have no idea why it isn't working. – GrimmRP Aug 11 '15 at 23:42
  • @Drew thanks man. :) GrimmRP, please check the code again. Copy and paste it in to your file then try. – Script47 Aug 11 '15 at 23:44
  • @Script47 Okay I did this, `` but still no luck, it just keeps me at the index page I will add the top of my index page to show you I have it added. `` – GrimmRP Aug 11 '15 at 23:48
  • @GrimmRP which page are you running in your browser? The one I'm telling you to update? – Script47 Aug 11 '15 at 23:49
  • @Script47 I am running, Index.php which I have the code `` which should bring the codes you are giving me to the index page. Shouldn't that grab the maintmod.php which I added your code to and check if maintenance is a 1 or 0 in the database? – GrimmRP Aug 11 '15 at 23:51
  • @GrimmRP does *maintenance* actually equal 1? – Script47 Aug 11 '15 at 23:53
  • @Script47 Yes in my database it's set as 1 or equal 1 I have it set as a `enum('0','1')` Is there away to show you my site and code etc? I have no idea why it isn't working.. – GrimmRP Aug 11 '15 at 23:54
0

As you are a self-described beginner, it's probably best to learn how to do this using mysqli instead of using old deprecated code.

Below does essentially the same thing, but will last past the current version of php. It's a good habit to get into. (note if you are going to use any variables in your query, you will want to look up Prepared Statements

$conn = new mysqli($host, $username, $password, $dbname);

$system = $conn->query("SELECT maintenance FROM `system`");
while ($row = $system->fetch_assoc()) {
    if($row['maintenance'] == 1) {
         header('Location: /maintenance');
         exit();
    }
    else {
        header('Location: /index');
        exit();
    }

}
nomistic
  • 2,902
  • 4
  • 20
  • 36
  • The problem is, I have no idea how to update to mysqli since I am using xampp to host my folders and files. – GrimmRP Aug 12 '15 at 00:01
  • This will work fine in xampp. mysqli is included. – nomistic Aug 12 '15 at 00:02
  • Okay I will try this code, also I already have my database fetched through a config file. – GrimmRP Aug 12 '15 at 00:02
  • note as this uses object oriented style, the connection needs to work this way as well. – nomistic Aug 12 '15 at 00:03
  • So I would have to type my database details into that also? Just getting this error now `Parse error: syntax error, unexpected '[' on line 4` – GrimmRP Aug 12 '15 at 00:04
  • yes. if you are using `mysql_` connection, that will need to be changed as well. all `mysql_` functions are deprecated. It's pretty easy to use `mysqli_`. (that's why I provided the connection detail)There are only some subtle changes to the syntax – nomistic Aug 12 '15 at 00:06
  • oh and I made a typo. fixed (ack, I always seem to do that ;)) – nomistic Aug 12 '15 at 00:10
  • Thanks for trying to help, I got it working since @Script47 had it as row it wasn't getting it since it needed to be an array. – GrimmRP Aug 12 '15 at 00:13
  • yes, that will technically work. However, as mentioned, it's a bad habit to get into for multiple reasons. `mysql_` extensions are *deprecated*. This means that they will soon *stop working*. Also, they cannot handle prepared statements or object oriented approaches. This means that you will have code that is a) not scalable, and b) more importantly vulnerable to sql injection. I just wanted to give some pointers. – nomistic Aug 12 '15 at 00:20
  • I will try looking into mysqli, I am using an old version of xampp since it was the easiest for me when I started coding back in 2011. This easy stuff which above people would look at and be like that is so damn easy but it is hard for me but boolans etc are easy for me. Thanks for helping (: – GrimmRP Aug 12 '15 at 00:24
  • note @Script47 also mentioned this in his explanation. I just wanted to help with demo. also note: mysqli has been available since well before 2011. It was introduced in php version 5.0.0. It definitely was in xampp in 2011 (I know, I use it). Any way, good luck, and have fun :) – nomistic Aug 12 '15 at 00:28
  • so I can use mysqli in xampp version or control panel version 1.7.3? If so I will try to learn it. – GrimmRP Aug 12 '15 at 00:33
  • you can check with phpinfo(). if you have php 5.3 or above you definitely have it. Also you may want to look into PDO, which is the current standard – nomistic Aug 12 '15 at 00:33