database - changing background color with php


Keywords:php 


Question: 

I have problem with the changing background color of php page. Firstly, I want to take the color name from DATABASE to make the current color constant. I mean, if user change the background color to BLUE, for example, it will be constant and perminant. So, I store the colors in database BUT the problem is that I can't UPDATE the database according to the COLOR that user chooses.

THIS IS THE CURRENT COLOR FROM DATABASE (THIS PART WORKS WELL, I JUST ADDED THIS PART FOR EVERYTHING TO BE UNDERSTOOD EASILY);

<body name="body" id="body" bgcolor="

<?php 
         mysql_connect("localhost","DB","password") or die("ERROR!!");
         mysql_select_db("DB") or die("COULDN'T FIND IT!!") or die("COULDN'T FIND DB"); 


        $sql = mysql_query("SELECT color FROM background");

        $color = 'color';   

        while($rows = mysql_fetch_assoc($sql))
        echo $rows[color];

?>

">

AND THIS IS THE PART THAT I UPDATE THE DATABASE ;

<?php

        $con = mysql_connect("localhost","DB","password") or die("ERROR!!");
        mysql_select_db("DB") or die("COULDN'T FIND IT!!") or die("COULDN'T FIND DB"); 


            if (isset( $_SESSION['CurrentUser'])){

            echo '<form method="post"><center><input type="submit" name="background" value="Change background">';
            echo '<input type="submit" name="color" value="Change font color">';
            echo '<input type="submit" name="size" value="Change font size"></center></form><br/>'; 

                if (isset( $_POST['background'])){ 

                echo '<span style="color:black"><center>Choose background color</center></span><br/>';
                echo '<center><a href=""><img src="" width="20" height="50"></a></center>'; 
                echo '<center><a href=""><img src="" width="20" height="50"></a></center>'; 
                echo '<center><a href=""><img src="" width="20" height="50"></a></center>';
                echo '<center><a href=""><img src="" width="20" height="50"></a></center><br/><br/>';

                $Color = $_POST['colour'];  // THIS LINE IS THE PROBLEM

                if ($Color == 'blue'){
                $sql = "UPDATE background SET color='blue' WHERE id=1";
                mysql_query($sql,$con); }
                if ($Color == 'black'){
                $sql = "UPDATE background SET color='black' WHERE id=1";
                mysql_query($sql,$con); }
                if ($Color == 'silver'){
                $sql = "UPDATE background SET color='silver' WHERE id=1";
                mysql_query($sql,$con); }
                if ($Color == 'red'){
                $sql = "UPDATE background SET color='red' WHERE id=1";
                mysql_query($sql,$con); }
                }

    }
    ?>

    </form>


    </td></tr></table>

THING IS THAT EVERYTHING WORKS WELL!! I MEAN I CAN UPDATE THE DATABASE IF I WANT SO THERE IS NO PROBLEM WITH THE MYSQL CONNECTION OR SOMETHING ABOUT IT. PROBLEM IS I CAN'T GET VALUE OF "COLOR" PARAMETER TO UPDATE THE DATABASE ACCORDING TO IT.

THANKS


3 Answers: 

You're forgetting a dollar sign before color when you echo your results:

$color = 'color';
while($rows = mysql_fetch_assoc($sql))
    echo $rows[color];

should be:

$color = 'color';
while($rows = mysql_fetch_assoc($sql))
    echo $rows[$color];
 

Then try

        $Color = $_GET['colour']; 
 
How about this:

//....

    echo '<form method="post"><center><input type="submit" name="background" value="Change background">';
    echo '<input type="submit" name="color" value="Change font color">';
    echo '<input type="submit" name="size" value="Change font size"></center></form><br/>'; 

    if (isset( $_POST['background'])){ 

        echo '<span style="color:black"><center>Choose background color</center></span><br/>';
        echo '<center><a href=""><img src="" width="20" height="50"></a></center>'; 
        echo '<center><a href=""><img src="" width="20" height="50"></a></center>'; 
        echo '<center><a href=""><img src="" width="20" height="50"></a></center>';
        echo '<center><a href=""><img src="" width="20" height="50"></a></center><br/><br/>';

   }

   if( !empty( $_GET['colour'] )) {
        $Color = $_GET['colour'];

        if ($Color == 'blue'){
                $sql = "UPDATE background SET color='blue' WHERE id=1";
                mysql_query($sql,$con); }
        if ($Color == 'black'){
                $sql = "UPDATE background SET color='black' WHERE id=1";
                mysql_query($sql,$con); }
        if ($Color == 'silver'){
                $sql = "UPDATE background SET color='silver' WHERE id=1";
                mysql_query($sql,$con); }
        if ($Color == 'red'){
                $sql = "UPDATE background SET color='red' WHERE id=1";
                mysql_query($sql,$con); }
        }    
   }
    ?>

    </form>