Intranet Journal

Go Back   IT Management Forum > Intranet Journal

Intranet Journal The new discussion forum for Intranet Journal readers. Leave comments and questions for IJ authors. Suggest story ideas and provide feedback.

Reply
 
LinkBack Thread Tools Rate Thread Display Modes
  #1 (permalink)  
Old 01-20-2010, 11:20 AM
batumulia batumulia is offline
Registered User
 
Join Date: Jan 2010
Posts: 2
Article won't save to database

Hi there,

In part 3 you will learn that you have to create a database and a form which you will use to save the article with. Whenever I try to write an article and save it (just some basic test writing) it won't give me a message if it failed to save or if it was a succes. It just returns to a blank form.

Here is my code so far:

includes/DbConnector.php
PHP Code:
<?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['localhost'];
$db = $settings['starcatcher'];
$user = $settings['root'];
$pass = $settings['***'];

// 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);

}


}
?>
includes/SystemComponent.php

PHP Code:
<?php
class SystemComponent {

var
$settings;

function
getSettings() {

// System variables
$settings['siteDir'] = 'localhost';

// Database variables
$settings['localhost'] = 'localhost';
$settings['root'] = 'root';
$settings['***'] = '***';
$settings['starcatcher'] = 'starcatcher';


return
$settings;

}

}
?>
cmsadmin/newArticle.php

PHP Code:
<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_GET_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_GET_VARS['title']."', ".
"'".$HTTP_GET_VARS['tagline']."', ".
$HTTP_GET_VARS['section'].", ".
"'".$HTTP_GET_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="get" 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>
Does someoby know what's wrong? I've been trying to crack it for 2 nights now.
Reply With Quote
  #2 (permalink)  
Old 01-29-2010, 11:41 AM
batumulia batumulia is offline
Registered User
 
Join Date: Jan 2010
Posts: 2
Almost 1,200 views and still no answer.

Is there someone who can help me out?

-Batumulia
Reply With Quote
  #3 (permalink)  
Old 02-25-2010, 07:54 AM
wixnot wixnot is offline
Registered User
 
Join Date: Feb 2010
Posts: 2
Couple of Issues

Hi, I'm no expert first off. I have been working with thes scripts and found a couple of issues with them.

First issue that I came across was in your article.php

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

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


$HTTP_GET_VARS should be $_GET

Also on the third line you need to add the "'" before the $HTTP_GET_VARS

I Changed those settings and got the Script working. I will check your scripts again and see if there is anything else and post more if there is
Reply With Quote
  #4 (permalink)  
Old 02-25-2010, 08:02 AM
wixnot wixnot is offline
Registered User
 
Join Date: Feb 2010
Posts: 2
couple more changes

I have noticed that you have entered in your login information for the database a couple of times, the first Cut and Paste below is from your DbConnector.php file

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

You need to leave the dbhost, dbname, dbusername and dbpassword the way I have it writen above, this script calls this information from SystemComponents.php and automatically enters it in.

Which brings me to my next part and that is the information you entered into SystemComponents.php. The first part $settings['dbhost'], $settings['dbusername'] etc need to be writen like that, you don't change the dbhost etc to your information, you only change what is after the =, where I have ****, this is where you enter your information

// Database variables
$settings['dbhost'] = 'localhost';
$settings['dbusername'] = '****';
$settings['dbpassword'] = '****';
$settings['dbname'] = '****';

Hopefully this works for you, I will check back to see if this helped
Reply With Quote
Reply

Bookmarks

Tags
blank form , cms , database , intranet

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 09:25 AM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.0