Jump to content

can't select database? any suggestions?


tjhilder

Recommended Posts

I've been fiddling around with some code but it doesn't seem to work right, I been trying several things but to no success, anyone able to help me on this one?

 

<?php

$username = $_POST['username'];
$password = $_POST['password'];
$fullname = $_POST['full_name'];
$email = $_POST['email'];
$url = $_SERVER['PHP_SELF'];

ini_set('display_errors',1);
error_reporting(E_ALL & ~E_NOTICE);

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

DEFINE (DB_USER, "username");
DEFINE (DB_PASSWORD, "password");
DEFINE (DB_HOST, "localhost");
DEFINE (DB_NAME, "database");

if ($dbc = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD))
{
if (@mysql_select_db(DB_NAME))
{
 die ('<p>Could not select the database because: <b>' . mysql_error() . '</b></p>');
}

} else {

die ('<p>Could not connect to MySQL because: <b>' . mysql_error() . '</b></p>');
}

$query = "INSERT INTO members (member_userid, member_username, member_password, member_fullname, member_email, member_rank) VALUES (
 0,
        '$username',
        '$password',
 '$fullname',
 '$email',
 '3'
 )";

if (@mysql_query($query)) {
print '<p>The blog entry has been added.</p>';
} else {
print "<p>Could not add the entry because: <b>" . mysql_error() . "</b>. The query was $query.</p>";
}
}

mysql_close();

?>

<form action="<? echo $url ?>" method="post">
<input type="text" name="full_name" cols="60" /><br />
<input type="text" name="username" cols="60" /><br />
<input type="text" name="password" cols="60" /><br />
<input type="text" name="email" cols="60" /><br />
<input type="submit" name="submit" value="Add Member!" /><br />

 

everytime i enter information it brings up the database error but nothing appears with mysql_error(), this is what I get:

Could not select the database because:

 

if I knew what the error was maybe I'd know what I'm meant to be doing but it doesn't seem to want to show up.

 

any suggestions?

 

Link to comment
Share on other sites

The problem seems to come from the line where you wrote

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]

if ($dbc = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD))

{

if (@mysql_select_db(DB_NAME))

{

die (\'<p>Could not select the database because: <b>\' . mysql_error() . \'</b></p>\');

}

 

} else {

 

die (\'<p>Could not connect to MySQL because: <b>\' . mysql_error() . \'</b></p>\');

}

 

[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

 

What actually occurs is like this -

If DB_NAMe is selected you throw an error.

 

So change that line to

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]

if (!@mysql_select_db(DB_NAME))

{

die (\'<p>Could not select the database because: <b>\' . mysql_error() . \'</b></p>\');

}

 

[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

 

 

Your code should work just fine

 

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.