Jump to content

connot find database


cvandingelen

Recommended Posts

Hi everyone, I hope you guys can help me. I'm a beginning php user and made a website. Everything worked fine on my local server, but when I put everything online I get an error. What's happening? When I display the page to register everything displays well, but when I insert a testuser and click to insert it, but then I get an error from my connect file that the database cannot be found. I checked username, password, host and database and everything is ok. Can anyone check this out?

 

code register.php:

 

<?php
session_start();
include("template.php"); //template.php includen
//Set up the defaults
$defaults['name'] = isset($_POST["name"])?htmlentities($_POST["name"]):'';
$defaults['password'] = isset($_POST["password"])?htmlentities($_POST["password"]):'';
$defaults['passwordRep'] = isset($_POST["passwordRep"])?htmlentities($_POST["passwordRep"]):'';
$defaults['passwordRepOK'] = isset($_POST["passwordRepOK"])?htmlentities($_POST["passwordRepOK"]):'';
$defaults['email'] = isset($_POST["email"])?htmlentities($_POST["email"]):'';
$defaults['secQuestion'] = isset($_POST["secQuestion"])?htmlentities($_POST["secQuestion"]):'';
$defaults['secAnswer'] = isset($_POST["secAnswer"])?htmlentities($_POST["secAnswer"]):'';
//is it a GET?
if($_SERVER['REQUEST_METHOD'] == 'GET')
{
display_form(array());
}
else
{
//it's a POST
//validate the form
$errors = validate_form();
if(count($errors))
{
	display_form($errors);
}
else
{
	//insert in the database 
	//send an e-mail and display a message to the user
	//that the form is submitted

	//first we insert in the database
	//include the login information
	require('db_login.php');
	//open the connection
	require('db_connect.php');
	//set the insert query
	$query = "INSERT INTO users (PKID, Username, Email, Comment, " .
			"Password, PasswordQuestion,PasswordAnswer) " .
			"VALUES (NULL, '".mysql_real_escape_string($_POST['name'])."','".
			mysql_real_escape_string($_POST['email'])."','gebruiker', '".
			sha1($_POST['password'])."','".
			mysql_real_escape_string($_POST['secQuestion'])."','".
			mysql_real_escape_string($_POST['secAnswer'])."')";
	//execute the query
	require('db_execute.php');
	//check for the pkid
	$pkid = 0;
	$query = "SELECT LAST_INSERT_ID()";
	//execute the query
	require('db_execute.php');
	//the result of the query needs to be in a variable pkid
	$pkid = $result->fetchRow();

	//now we're gonna insert into the userinfo database
	//actievatiecode
   		global $activatiecode;
   		$activatiecode = sha1(md5(microtime()*rand(1,10)));
	//set the insert query
	$query = "INSERT INTO userinfo (userInfoID, PKID, IsApproved, LastActivityDate, LastLoginDate, LastPasswordChangedDate, CreationDate, IsOnLine, IsLockedOut, LastLockedOutDate, FailedPasswordAttemptCount, FailedPasswordAttemptWindowStart, FailedPasswordAnswerAttemptCount, FailedPasswordAnswerAttemptWindowStart) " .
			"VALUES (NULL, ".$pkid[0].", '".$activatiecode."', ".date("Ymd").", NULL, ".date("Ymd").", ".date("Ymd").", 0, 0, NULL, 0, NULL, 0, NULL)";
	//execute the query
	require('db_execute.php');
	//set the query to update the LastActivityDate
	$query = "UPDATE userinfo SET LastActivityDate = ".date("Ymd")." WHERE PKID=(SELECT PKID FROM users WHERE Username= '".$_SESSION['username']."')"; 

	//execute the query
	require('db_execute.php');


	//the only thing left now is to send the user an activation e-mail
	//End of line 
	define('EOL', "\r\n"); 
   
	//this is the mail body
	$mail   = 'Bedankt voor je registratie, hierbij jou gebruikersgegevens: '.EOL.EOL;
	$mail  .= 'Gebruikersnaam: '.$_POST['name'].EOL;
	$mail  .= 'E-mail: '.$_POST['email'].EOL;
	$mail  .= 'Veiligheidsvraag: '.$_POST['secQuestion'].EOL;
	$mail  .= 'Veiligheidsantwoord: '.$_POST['secAnswer'].EOL.EOL;
	$mail  .= 'Gebruik onderstaande link om jezelf in te loggen op www.ruilmij.be '.EOL.EOL;
	$mail  .= 'http://localhost/activatie.php?gebruiker='.$pkid[0].'&sleutel='.$activatiecode.EOL;
	$mail  .= 'Tot ziens op RuilMij.be!';
   
	$to = $_POST['email'];
	$subject = "Uw RuilAlles.be account activeren.";
	$message = $mail;

	define('DISPLAY_XPM4_ERRORS', true); // display XPM4 errors
	require_once 'MAIL.php'; // path to 'MAIL.php' file from XPM4 package

	$m = new MAIL; // initialize MAIL class
	$m->From('[email protected]'); // set from address
	$m->AddTo($to); // add to address
	$m->Subject($subject); // set subject
	$m->Text($message); // set text message

	// send mail local and print result
	if($m->Send())
	{
		header("Location: registerOK.php");	
	}
	else
	{
		echo "Error";
	}

		//nothing needs to be displayed anymore, so we close the connection
		require('db_close.php');
	}
}

function display_form($errors)
{
//Set up the defaults
$defaults['name'] = isset($_POST["name"])?htmlentities($_POST["name"]):'';
$defaults['password'] = isset($_POST["password"])?htmlentities($_POST["password"]):'';
$defaults['passwordRep'] = isset($_POST["passwordRep"])?htmlentities($_POST["passwordRep"]):'';
$defaults['passwordRepOK'] = isset($_POST["passwordRepOK"])?htmlentities($_POST["passwordRepOK"]):'';
$defaults['email'] = isset($_POST["email"])?htmlentities($_POST["email"]):'';
$defaults['secQuestion'] = isset($_POST["secQuestion"])?htmlentities($_POST["secQuestion"]):'';
$defaults['secAnswer'] = isset($_POST["secAnswer"])?htmlentities($_POST["secAnswer"]):'';
}

function print_error($key, $errors)
{
if(isset($errors[$key]))
{
	print "<dd class='error'>{$errors[$key]}</dd>";
}
}

function validate_form()
{
//start with no errors;
$errors = array();

//name is required
if(! (isset($_POST['name']) && (strlen($_POST['name']) > 0)))
{
	$errors['name'] = 'Gelieve een gebruikersnaam op te geven.';
}
//password is required
if(! (isset($_POST['password']) && (strlen($_POST['password']) > 0)))
{
	$errors['password'] = 'Gelieve een geldig paswoord op te geven.';
}

//repeat password is required
if(! (isset($_POST['passwordRep']) && (strlen($_POST['passwordRep']) > 0)))
{
	$errors['passwordRep'] = 'Gelieve uw paswoord te herhalen.';
}

//check if password and passwordRep are the same
if(isset($_POST['password']) && isset($_POST['passwordRep']) && !(($_POST['password']) == ($_POST['passwordRep'])))
{
	$errors['passwordRepOK'] = 'Uw paswoord en de herhaling van uw paswoord komen niet overeen.';
}

//email is required
if(! (isset($_POST['email']) && (strlen($_POST['email']) > 0)))
{
	$errors['email'] = 'Geef een geldig e-mail adres op.';
}

//the securityquestion and the securityanswer are required
if(! (isset($_POST['secQuestion']) && (strlen($_POST['secQuestion']) > 0)))
{
	$errors['secQuestion'] = 'Gelieve een veiligheidsvraag op te geven.';
}

if(! (isset($_POST['secAnswer']) && (strlen($_POST['secAnswer']) > 0)))
{
	$errors['secAnswer'] = 'Gelieve een veiligheidsantwoord op te geven.';
}

//we need to check if the e-mail adress provided is valid
if((valid_email($_POST['email'])) == false)
{
	$errors['email'] = 'Gelieve een geldig e-mail adres op te geven.';
}

//now check if the username and the e-mail do not exist in the database
//first include the login information
require_once('db_login.php');
//then connect to the database
require_once('db_connect.php');
//set the query
$query = "SELECT * FROM users WHERE Username = '".$_POST['name']."'";
//execute the query
require_once('db_execute.php');
//if the result > 0, display an error
$row = $result->fetchRow(DB_FETCHMODE_ASSOC);
if(count($row)>0)
{
	$errors['name'] = 'De opgegeven gebruikersnaam bestaat reeds.';
}

//do the same for e-mail
//the login & connect are already included so now we set a new query
$query = "SELECT * FROM users WHERE Email = '".$_POST['email']."'";

//execute the query
//since we already did a require_once for db_execute, I'm gonna do the code
//of this file manually
global $connection;

$result = $connection->query($query);

if(DB::isError($result))
{
	die("Kan query niet uitvoeren: <br />".DB::errorMessage($result));
}

//if the result > 0, display an error
$row = $result->fetchRow(DB_FETCHMODE_ASSOC);
if(count($row)>0)
{
	$errors['email'] = 'Het opgegeven e-mail adres is reeds in gebruik.';
}

//finally close the connection to the database
require_once('db_close.php');

return $errors;
}

function valid_email($email) { 
  // First, we check that there's one @ symbol, and that the lengths are right 
  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) 
  { 
    // Email invalid because wrong number of characters in one section, or wrong number of @ symbols. 
    return false; 
  } 
  // Split it into sections to make life easier 
  $email_array = explode("@", $email); 
  $local_array = explode(".", $email_array[0]); 
  for ($i = 0; $i < sizeof($local_array); $i++) 
  { 
     if (!ereg("^(([A-Za-z0-9!#$%&#038;'*+/=?^_`{|}~-][A-Za-z0-9!#$%&#038;'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) { 
      return false; 
  	  } 
  }   
  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name 
    $domain_array = explode(".", $email_array[1]); 
    if (sizeof($domain_array) < 2) 
{ 
        return false; // Not enough parts to domain 
    } 
    for ($i = 0; $i < sizeof($domain_array); $i++)
{ 
      if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) 
  { 
        return false; 
      } 
    } 
  } 
  return true; 
} 

$template = new template; //nieuwe template aanmaken
$template->assign_vars(array("SITE_NAME" => "Welkom bij Ruil! - Registreer", "PAGINA_INHOUD" => ""));
echo $template->parse_file("master.tpl");//output parsen
?>

<form action='<?php echo htmlentities($_SERVER["PHP_SELF"]); ?>' method="post">
<dl>
<?php
//here we will call the print_error method witch will do all our checks
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
	print_error('name',$errors);
	print_error('password',$errors);
	print_error('passwordRep',$errors);
	print_error('passwordRepOK',$errors);
	print_error('email',$errors);
	print_error('secQuestion',$errors);
	print_error('secAnswer',$errors);
}
//finally we display the form.	
?>
<form>
  <table width="100%" border="0">
      <tr>
      	<!-- advertisement 1 -->
        <td><div align="center">Hier komt de eerste ad.</div></td>
        <td><div align="left"><table width="100%" border="0">
      <tr>
      	<!-- username -->
        <td width="28%"><div align="right">Gebruikersnaam: </div></td>
        <td width="16%"><input type='text' name='name' value='<?php echo $defaults['name']; ?>' /></td>
        <td width="56%"> </td>
      </tr>
      <tr>
      	<!-- password -->
        <td><div align="right">Paswoord: </div></td>
        <td><input type='password' name='password' value='' /></td>
        <td> </td>
      </tr>
      <tr>
      	<!-- repeat password -->
        <td><div align="right">Paswoord herhalen: </div></td>
        <td><input type='password' name='passwordRep' value='' /></td>
        <td><input type='hidden' name='passwordRepOK' value='' /></td>
      </tr>
      <tr>
      	<!-- email -->
        <td><div align="right">E-mail: </div></td>
        <td><input type='text' name='email' value='<?php echo $defaults['email']; ?>' /></td>
        <td> </td>
      </tr>
      <tr>
      	<!-- security question -->
        <td><div align="right">Veiligheidsvraag: </div></td>
        <td><input type="text" name="secQuestion" value="<?php echo $defaults['secQuestion']?>" /></td>
        <td> </td>
      </tr>
      <tr>
      	<!-- security answer -->
        <td><div align="right">Veiligheidsantwoord: </div></td>
        <td><input type="text" name="secAnswer" value="<?php echo $defaults['secAnswer']?>" /></td>
        <td> </td>
      </tr>
      <tr>
      	<!-- submit button -->
        <td> </td>
        <td> </td>
        <td><input type="submit" value="Versturen" /></td>
      </tr>
  </table></div></td>
  		<!-- advertisement 2 -->
        <td><div align="center">Hier komt de tweede ad.</div></td>
      </tr>
    </table>
</form>

 

code connect.php

 

<?php
//include the login information
require_once('db_login.php');
require_once('DB.php');

//connect
global $connection;	
$connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
//without pear its: 
//$connection = mysql_connect($db_host, $db_username, $db_password);
if(DB::isError($connection))
{
	die("Kan geen verbinding maken met de database: <br />".DB::errorMessage($connection));
}
/*without pear its: 
 * 
 if (!$connection)
{
	die ("Kan geen verbinding maken met de database: <br />".mysql_error());
}*/

/*without pear, use this to:
 * //Select the database
$db_select=mysql_select_db($db_database);
if (!$db_select)
{
	die ("Kan de database niet selecteren: <br />".mysql_error());
}*/
?>

 

thx already. If you need more information or code from the other files, just ask.

Link to comment
https://forums.phpfreaks.com/topic/109617-connot-find-database/
Share on other sites

Archived

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

×
×
  • 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.