Jump to content

Issues Building a CMS, cannot get the form to post to the db


liquidity-zero

Recommended Posts

Im having trouble getting the form to post to the database, i am not sure if i am using correct syntax either, i know i should buy some updated book i.e. php6, i would really appreciated help from someone on debugging this code :( any suggestions? many thanks in advance guys.

 

 

newArticle.php

<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 ($_POST){

 

// 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 (".

"'".$_POST['title']."', ".

"'".$_POST['tagline']."', ".

$_POST['section'].", ".

"'".$_POST['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>

 

 

 

 

// System variables

$settings['siteDir'] = 'localhost/test';

 

// Database variables

$settings['dbhost'] = 'localhost';

$settings['dbusername'] = 'root';

$settings['dbpassword'] = '';

$settings['dbname'] = 'mydb';

 

return $settings;

 

}

 

}

?>

 

SystemComponent.php

<?php

class SystemComponent {

 

var $settings;

 

function getSettings() {

 

// System variables

$settings['siteDir'] = 'localhost/test';

 

// Database variables

$settings['dbhost'] = 'localhost';

$settings['dbusername'] = 'root';

$settings['dbpassword'] = '';

$settings['dbname'] = 'mydb';

 

return $settings;

 

}

 

}

?>

 

DbConnector.php

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

 

}

 

 

}

?>

 

 

 

 

I haven't looked at this for long but the syntax is definitely wrong and in very basic ways.  PHP6 is not the issue but a look at the basics would be good.

 

For example, You seem to be setting system variables (except you are not as they are in HTML not PHP) after you have finished the querying.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.