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> Title:
<input name="title" type="text" id="title">
</p>
<p> Tagline:
<input name="tagline" type="text" id="tagline">
</p>
<p> Section:
<input name="section" type="text" id="section">
</p>
<p> 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> Title:
<input name="title" type="text" id="title">
</p>
<p> Tagline:
<input name="tagline" type="text" id="tagline">
</p>
<p> Section:
<input name="section" type="text" id="section">
</p>
<p> 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.