Jump to content

Error: cannot select DB


jrws

Recommended Posts

I don't really know what code to post, so please ask what sections you would like to see.

Basically this is an install page. However for some reason none of the variables carry onto the next page. The below code is what I did for error checking:

case 1:
        {
		if(isset($name)){
            echo 'Testing connection now:<br>';
            $q1 = mysql_connect($host, $user, $pass) or die('Error: ' . mysql_error() .
                '<br>Query: ' . $q1 . '<br> <a href="/news_system/install.php">Go back</a>');
            $q2 = mysql_select_db($name) or die('Error: ' . mysql_error() . '<br>Query: ' .
                $q2 . '<br> <a href="/news_system/install.php">Go back</a>');
                }else{
				echo 'WTF?!';
			}
            break;
        }

You know what it returned? It returned 'WTF?!'

Meaning for some reason the variable hasn't been set. If anyone wants me to I can display all the code, since its only like 200 or so lines, all on one page...

But would anyone have any idea as to why a variable would not be set?

I made sure that all the variable names were correct and that I did the $_POST correct, so I am now lost.

 

Link to comment
Share on other sites

try post['']

<?php
case 1:
        {
         if(isset($_POST['name'])){
            echo 'Testing connection now:<br>';
            $q1 = mysql_connect($host, $user, $pass) or die('Error: ' . mysql_error() .
                '<br>Query: ' . $q1 . '<br> <a href="/news_system/install.php">Go back</a>');
            $q2 = mysql_select_db($name) or die('Error: ' . mysql_error() . '<br>Query: ' .
                $q2 . '<br> <a href="/news_system/install.php">Go back</a>');
                }else{
               echo 'WTF?!';
            }
            break;
        }
?>

Link to comment
Share on other sites

To dezit;

Yes I do. I will post the full code so you can have a look if you like.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title>Install New News System</title>
		<style type="text/css">

*{
    margin: 0;
    padding: 0;
    }

    body{

    background: #1f1f1f;
    font-family: tahoma;
    font-size: 10px;
    color: #D6D6D6;
    text-align: center;

    }
      
    a{

    letter-spacing: 1px;
    text-decoration: none;
    color: #BBBBBB;
    }

    a:hover{
    text-decoration: underline;
    color: #D6D6D6;
    }
    ul{
    margin:0; padding:0; list-style:none;
    }
    #logo{
    float: right;
    height:396px ;
    width: 200px;
    margin-right: 25px;
    padding: 15px;
    border: solid 2px #575757;
    background: #3C3C3C;
    text-align:left ;
    } 
    .holder{
    width: 500px;
    margin: 0 auto;
    text-align: left;
    border: solid 2px #575757;
    padding: 3px;
    background: #3C3C3C;
    margin-top: 5px;
    max-height:36em;
}
    
#header{
    height:60px ;
    width: 500px;
    margin-bottom: 150px auto;
    padding-bottom: 2px;
    border-bottom: solid 2px #575757;
    background: #3C3C3C;
    text-align:center ;
    }
    #footer{
    height:60px ;
    width: 500px;
    margin-top: 150px auto;
    padding-top: 2px;
    border-top: solid 2px #575757;
    background: #3C3C3C;
    text-align:center ;
    }
    .error{
	color:#C93838;
	text-align:center;
}

		</style>
	</head>

<body><div class="holder">
<?php
//Install Page//
//This writes to the configuration file and updates or creates the tables nessessary to function.
//Inserts administrator into the fix as well
//Creates the index file
//Self deleting. (finished version);
$x = $_GET['x'];
switch ($x)
{
    default:
        {
            if (!isset($_POST['submit']))
            {
?>

	<p>Welcome to the installer. This file was created to make your life easier. So please don't abuse it </p><p>Below is a form, it requires your database information. If you don't have a password for you database, perhaps you should get one...</p>
<form method="POST" action="<?php echo $PHP_SELF; ?>">
Database Name:<input type = "text" name="db_name" value="news_system" ><br>
Database Host:<input type = "text" name="db_host" value="localhost"><br>
Database password:<input type = "password" name="db_pass" ><br>
Database user:<input type = "text" name="db_user" value="root"><br>
<input type = "submit" name = "submit" value ="Submit">
</form>
<?php
            } else
            {
                $name = $_POST['db_name'];
                $host = $_POST['db_host'];
                $pass = $_POST['db_pass'];
                $user = $_POST['db_user'];
                if (empty($name) || empty($host) || empty($user))
                {
?><div class="error">
	WARNING: You have not filled in one or more fields, please <a href="/news_system/install.php">go back</a></div>
<?
                } else
                {
                    echo '<a href="/news_system/install.php?x=1">Proceed to next step...</a>';
                    //echo '<meta http-equiv="refresh" content="1;url="/news_system/install.php?x=1">';
                }
            }
            break;
        }
    case 1:
        {
		if(isset($name)){
            echo 'Testing connection now:<br>';
            $q1 = mysql_connect($host, $user, $pass) or die('Error: ' . mysql_error() .
                '<br>Query: ' . $q1 . '<br> <a href="/news_system/install.php">Go back</a>');
            $q2 = mysql_select_db($name) or die('Error: ' . mysql_error() . '<br>Query: ' .
                $q2 . '<br> <a href="/news_system/install.php">Go back</a>');
                }else{
				echo 'WTF?!';
			}
            break;
        }
}
//}
?>
</div>
</body>
</html>

Link to comment
Share on other sites

Variables do not pass between pages, only $_SESSION variables do, as long as your in the same session for each page.

 

The variable isnt set, because when you go to page.php?x=1, you are skipping the part that sets the variables from the form, in fact - since your clicking that link your not posting any form data anyway.

 

If you want to keep track of data over multiple pages, you need to look into sessions.

Link to comment
Share on other sites

To get you started;

 

Every page that makes use of the session variables needs to initiate the session with session_start();

 

sessions use cookies, so when you call session_start() it sets a cookie, cookies must be sent before any output, therefor session_start(); is best put at the very top of your script.

----

 

To access any session data, you would use the $_SESSION global; eg: $_SESSION['name'];

To assign data to a session variable, use $_SESSION['db_name'] = $_POST['db_user'];

Link to comment
Share on other sites

The same page is maybe the wrong way to look at it. The same run of the script is a better way to look at it. You have set $name, and you are using $name, but the page has to be refreshed in between the setting of $name and the using of $name. So you can count this as running the script twice. It will not carry over once the page is refreshed. It will only carry through each single run of the script.

 

As a side note, you have a switch() statement set up, but you put the default at the start. You should move the default to the end, after all the other possibilities, or the default will not work properly.

 

As a second side note, you should always use <?php tags instead of <? tags.

Link to comment
Share on other sites

Alright thanks mate, will take it into consideration.

BTW here is the revised code with sessions, now I keep getting the error of the data being empty even when not, however if I remove that line, everything works again.

<? session_start(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title>Install New News System</title>
		<style type="text/css">

*{
    margin: 0;
    padding: 0;
    }

    body{

    background: #1f1f1f;
    font-family: tahoma;
    font-size: 10px;
    color: #D6D6D6;
    text-align: center;

    }
      
    a{

    letter-spacing: 1px;
    text-decoration: none;
    color: #BBBBBB;
    }

    a:hover{
    text-decoration: underline;
    color: #D6D6D6;
    }
    ul{
    margin:0; padding:0; list-style:none;
    }
    #logo{
    float: right;
    height:396px ;
    width: 200px;
    margin-right: 25px;
    padding: 15px;
    border: solid 2px #575757;
    background: #3C3C3C;
    text-align:left ;
    } 
    .holder{
    width: 500px;
    margin: 0 auto;
    text-align: left;
    border: solid 2px #575757;
    padding: 3px;
    background: #3C3C3C;
    margin-top: 5px;
    max-height:36em;
}
    
#header{
    height:60px ;
    width: 500px;
    margin-bottom: 150px auto;
    padding-bottom: 2px;
    border-bottom: solid 2px #575757;
    background: #3C3C3C;
    text-align:center ;
    }
    #footer{
    height:60px ;
    width: 500px;
    margin-top: 150px auto;
    padding-top: 2px;
    border-top: solid 2px #575757;
    background: #3C3C3C;
    text-align:center ;
    }
    .error{
	color:#C93838;
	text-align:center;
}

		</style>
	</head>

<body><div class="holder">
<?php
//Install Page//
//This writes to the configuration file and updates or creates the tables nessessary to function.
//Inserts administrator into the fix as well
//Creates the index file
//Self deleting. (finished version);
$x = $_GET['x'];
switch ($x)
{
    default:
        {
            if (!isset($_POST['submit']))
            {
?>

	<p>Welcome to the installer. This file was created to make your life easier. So please don't abuse it </p><p>Below is a form, it requires your database information. If you don't have a password for you database, perhaps you should get one...</p>
<form method="POST" action="<?php echo $PHP_SELF; ?>">
Database Name:<input type = "text" name="db_name" value="news_system" ><br>
Database Host:<input type = "text" name="db_host" value="localhost"><br>
Database password:<input type = "password" name="db_pass" ><br>
Database user:<input type = "text" name="db_user" value="root"><br>
<input type = "submit" name = "submit" value ="Submit">
</form>
<?php
            } else
            {
                $_SESSION['name'] = $_POST['db_name'];
                $_SESSION['host'] = $_POST['db_host'];
                $_SESSION['pass'] = $_POST['db_pass'];
                $_SESSION['user'] = $_POST['db_user'];
                //Error check for the post:
                $name = $_SESSION['name'];
                $host = $_POST['host'];
                $pass = $_POST['pass'];
                $user = $_POST['user'];
                if (empty($name) || empty($host) || empty($user))
                {
?><div class="error">
	WARNING: You have not filled in one or more fields, please <a href="/news_system/install.php">go back</a></div>
<?
                } else
                {
                    echo '<a href="/news_system/install.php?x=1">Proceed to next step...</a>';
                    //echo '<meta http-equiv="refresh" content="1;url="/news_system/install.php?x=1">';
                }
            }
            break;
        }
    case 1:
        {
                echo 'Testing connection now:<br>';
                $q1 = mysql_connect($_SESSION['host'], $_SESSION['user'], $_SESSION['pass']) or die('Error: ' . mysql_error() .
                    '<br>Query: ' . $q1 . '<br> <a href="/news_system/install.php">Go back</a>');
                $q2 = mysql_select_db($_SESSION['name']) or die('Error: ' . mysql_error() . '<br>Query: ' .
                    $q2 . '<br> <a href="/news_system/install.php">Go back</a>');
                 
            
            break;
        }
}
//}

?>
</div>
</body>
</html>

Link to comment
Share on other sites

you really need to take a step back and figure out how php is working with your code.

 

you have a switch statement.

 

If x = 1, then it will show that form, so here is where you need to set the session variables.

 

then if x=2, it should display the information posted into the session variables. (do not need to "set" anthing here)

==============

 

Hope this helps.

Link to comment
Share on other sites

You've still got your default before your first switch option.

 

Switch statements should look like this:

 

switch($var)
{
  case 1:
    // do some stuff;
    break;
  case 2:
    // do some other stuff;
    break;
  default:
    // do the default stuff
    break;
}

 

What happens is that the code will run through each of the cases, and if it doesn't find one that works, it will execute the default. If you have the default first, I'm not exactly sure what will happen (as I've never done this), but I suspect that it is going to do whatever is in the default no matter what, since there are no cases ahead of it.

Link to comment
Share on other sites

only a fort

<html>
<head>
<title>Install Application.</title>
</head>
<body>
<a href="<?php $_SERVER['PHP_SELF'];?>?cmd=application">Install new application</a>
</body>
</html>


<?php if($_GET['cmd']=="application"){ ?>

<p>Welcome to the installer.
  This file was created to make your life easier.
   So please don't abuse it </p><p>Below is a form, it requires your database information.
    If you don't have a password for you database, perhaps you should get one...</p>

<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>?cmd=install">
Database Name:<input type = "text" name="db_name" value="news_system" ><br>
Database Host:<input type = "text" name="db_host" value="localhost"><br>
Database password:<input type = "password" name="db_pass" ><br>
Database user:<input type = "text" name="db_user" value="root"><br>
<input type = "submit" name = "submit" value ="Submit">
</form>

<?php 

}

if($_POST['submit']){

$name = $_POST['db_name'];
          $host = $_POST['db_host'];
             $pass = $_POST['db_pass'];
                $user = $_POST['db_user'];
                
                if (empty($name) || empty($host) || empty($user)){

           echo"Please provide all the form info !.";
           
                }
                

                if (($name) && ($host) && ($user)){
                	
                $q1 = mysql_connect($host, $user, $pass) or die('Error: ' . mysql_error());
               
                $q2 = mysql_select_db($name) or die('Error: ' . mysql_error());
                
                
                //include the database info.
                
                //include("database_info.php");
              }
}
?>

</div>
</body>
</html>

Link to comment
Share on other sites

A word of caution around the order used for the case/default controls.  I notice that a lot of people do not break; the default section and the following could lead to incorrect results when run.

 

$a = "lowercase";

 

switch ( $a ) {

  default:

    $a = strtoupper( $a );

    print $a . "<br />";

 

  case ( 'LOWERCASE' ):

    print $a . "<br />";

    break;

}

 

Result:

LOWERCASE

LOWERCASE

 

Placing a break; in the default control will result in:

LOWERCASE

 

.. as expected.  Also, placing the default section at the bottom (as in an else control) will also display the correct result.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.