...

View Full Version : Aaah! I'm trying to design a CMS and can't get it to connect to database!



Noah
06-19-2005, 04:47 PM
I've been working on getting a content management system up for my site. But I can't get the **** to connect to my database! I've tried everything! Here's what I have so far:

SystemComponent.php (store settings)

<?php
class SystemComponent {

var $settings;

function getSettings() {

// System variables
$settings['siteDir'] = '/home/nymfans/public_html/bab/cmsadmin';

// Database variables
$settings['dbhost'] = 'localhost';
$settings['dbusername'] = 'nymfans_nw';
$settings['dbpassword'] = 'homicide';
$settings['dbname'] = 'nymfans_nwcms';

return $settings;

}

}
?>

DbConnector.php (connect to database)

<?php
////////////////////////////////////////////////////////////////////////////////////////
// Class: DbConnector
// Purpose: Connect to a database, MySQL version
///////////////////////////////////////////////////////////////////////////////////////
require_once 'SystemComponent.php';

class DbConnector extends SystemComponent {

var $theQuery;
var $link;

//*** Function: DbConnector, Purpose: Connect to the database ***
function DbConnector(){

// Load settings from parent class
$settings = SystemComponent::getSettings();

// Get the main settings from the array we just loaded
$host = $settings['dbhost'];
$db = $settings['dbname'];
$user = $settings['dbusername'];
$pass = $settings['dbpassword'];

// Connect to the database
$this->link = mysql_connect($host, $user, $pass);
mysql_select_db($db);
register_shutdown_function(array(&$this, 'close'));

}

//*** Function: query, Purpose: Execute a database query ***
function query($query) {

$this->theQuery = $query;
return mysql_query($query, $this->link);

}

//*** Function: fetchArray, Purpose: Get array of query results ***
function fetchArray($result) {

return mysql_fetch_array($result);

}

//*** Function: close, Purpose: Close the connection ***
function close() {

mysql_close($this->link);

}


}
?>
newArticle.php (post to DB)

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<?php
// Get the PHP file containing the DbConnector class
require_once('../includes/DbConnector.php');

// Check whether a form has been submitted. If so, carry on
if ($HTTP_POST_VARS){

// Create an instance of DbConnector
$connector = new DbConnector();

// IMPORTANT!! ADD FORM VALIDATION CODE HERE - SEE THE NEXT ARTICLE

// Create an SQL query (MySQL version)
$insertQuery = "INSERT INTO cmsarticles (title,tagline,section,thearticle) VALUES (".
"'".$HTTP_POST_VARS['title']."', ".
"'".$HTTP_POST_VARS['tagline']."', ".
$HTTP_POST_VARS['section'].", ".
"'".$HTTP_POST_VARS['thearticle']."')";

// Save the form data into the database
if ($result = $connector->query($insertQuery)){

// It worked, give confirmation
echo '<center><b>Article added to the database</b></center><br>';

}else{

// It hasn't worked so stop. Better error handling code would be good here!
exit('<center>Sorry, there was an error saving to the database</center>');

}

}
?>

<body>
<form name="form1" method="post" action="newArticle.php">
<p>&nbsp;Title:
<input name="title" type="text" id="title">
</p>
<p>&nbsp;Tagline:
<input name="tagline" type="text" id="tagline">
</p>
<p>&nbsp;Section:
<input name="section" type="text" id="section">
</p>
<p>&nbsp;Article:
<textarea name="thearticle" cols="50" rows="6" id="thearticle"></textarea>
</p>
<p align="center">
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>

Help!

mrruben5
06-20-2005, 02:20 PM
Why do you have two classes for a mysql connection?

Kurashu
06-20-2005, 07:49 PM
Furthermore, why didn't you censor the connection data?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum