Jump to content


Photo

can't select database? any suggestions?


  • Please log in to reply
2 replies to this topic

#1 tjhilder

tjhilder
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationKent, UK

Posted 05 November 2005 - 02:55 PM

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?


#2 azuka

azuka
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts
  • LocationNigeria

Posted 05 November 2005 - 03:26 PM

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

Given enough eyeballs all bugs are shallow. My Website

#3 tjhilder

tjhilder
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationKent, UK

Posted 05 November 2005 - 04:22 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users