Jump to content

Archived

This topic is now archived and is closed to further replies.

tjhilder

can't select database? any suggestions?

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?

 

Share this post


Link to post
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

 

Share this post


Link to post
Share on other sites

thanks very much, sometimes seems to take someone elses point of view to figure out the error and correct it.

Share this post


Link to post
Share on other sites

×

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.