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 Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-16-2009, 04:30 AM
anoxia anoxia is offline
Registered User
 
Join Date: Mar 2009
Posts: 3
Question PHP-Based CMS, Part 3 (error saving to DB)

Solved:
(...) section must be a numeric value, and this article is PHP4, while I run PHP5 on my server, so I had to change to _POST and _GET.
It works now


Original post:

Hi,

I've followed the article on creating a PHP based CMS, but I'm stuck at part 3, page 1 (might be a problem earlier on?).

When I try and save an article, I get the following error:
Quote:
Sorry, there was an error saving to the database
I got the following:

cmsadmin/newArticle.php:
Code:
<html>
<head>
<title>newArticle.php</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<html>
<head>
<title>newArticle.php</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>

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>
includes/SystemComponent.php:
Code:
<?php
class SystemComponent {

var $settings;

function getSettings() {

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

// Database variables
$settings['dbhost'] = 'MY HOST';
$settings['dbusername'] = 'MY USR;
$settings['dbpassword'] = 'MY PSW';
$settings['dbname'] = 'MY DB NAME';

return $settings;

}
}
?>
includes/DbConnector.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['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);

}


}
?>

Everything is located at the root of my webserver.

I need help figuring out what the problem might be, as I am clueless, and possible a solution to the problem.


Thanks in advance.

Last edited by anoxia; 03-16-2009 at 05:56 AM. Reason: Solved.
Reply With Quote
  #2 (permalink)  
Old 03-16-2009, 05:07 AM
anoxia anoxia is offline
Registered User
 
Join Date: Mar 2009
Posts: 3
Hi,

I am sorry for the inconvenience, but I read in a different topic (regarding part 2), that section must be a numeric value, and this article is PHP4, while I run PHP5 on my server, so I had to change to _POST and _GET.

It works, for now.

Please lock or delete this topic, unless it's useful for people searching the forum.
Reply With Quote
  #3 (permalink)  
Old 01-05-2010, 09:41 PM
luvvips luvvips is offline
Registered User
 
Join Date: Jan 2010
Posts: 1
Add numeric value to section

Hey,
I had the same problem, do one thing. Add numeric value to section (which is type of int). You'll solve the problem immediate.

http://www.sharmavipul.com
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 11:05 AM.





Acceptable Use Policy


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