gzhang
4-9-09, 02:56 AM
Hi, I'm a graphic designer attempting to pick up PHP and mySQL for the first time, and I have to say that I've had close to no success, especially with the latter. I've gone as far as to practically copy and paste the code for a CMS from a tutorial, and still no luck.
Basically, I'll input my information into the HTML input form and it theoretically should execute the PHP script to insert it into the database, but instead of actually inserting the values into the database I simply get redirected to a blank page. No confirmation, no error messages, nothing.
Here is my code:
Component Script
<?php
class SystemComponent {
var $settings;
function getSettings(){
//System Variables
$settings['siteDir']='REMOVED';
//Database Variables
$settings['dbhost']='REMOVED';
$settings['dbusername']='REMOVED';
$settings['dbpassword']='REMOVED';
$settings['dbname']='REMOVED';
return $settings;
}
}
?>
Database Connector Script
<?php
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);
}
}
?>
Insertion Script
<?php
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>');
}
}
?>
Test Page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Page</title>
</head>
<body>
<table width="80%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#999999">
<tr>
<td bordercolor="#FFFFFF"><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>
</td>
</tr>
</table>
</body>
</html>
If anyone could help that would be great. Thanks a lot!
Basically, I'll input my information into the HTML input form and it theoretically should execute the PHP script to insert it into the database, but instead of actually inserting the values into the database I simply get redirected to a blank page. No confirmation, no error messages, nothing.
Here is my code:
Component Script
<?php
class SystemComponent {
var $settings;
function getSettings(){
//System Variables
$settings['siteDir']='REMOVED';
//Database Variables
$settings['dbhost']='REMOVED';
$settings['dbusername']='REMOVED';
$settings['dbpassword']='REMOVED';
$settings['dbname']='REMOVED';
return $settings;
}
}
?>
Database Connector Script
<?php
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);
}
}
?>
Insertion Script
<?php
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>');
}
}
?>
Test Page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Page</title>
</head>
<body>
<table width="80%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#999999">
<tr>
<td bordercolor="#FFFFFF"><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>
</td>
</tr>
</table>
</body>
</html>
If anyone could help that would be great. Thanks a lot!