Newbie Question

5 views
Skip to first unread message

dirtyone30

unread,
Mar 6, 2006, 5:26:22 PM3/6/06
to Php Object Generator
I am having great difficulty using the POG code in my project.
Basicaly i cannot get data inserted using the save() method. Can
someone please tell me where i am going wrong.

Here's my code for the insert...

<?php
include
$_SERVER['DOCUMENT_ROOT'].'/clients/leith_2/includes/custom/pre.php';

if (isset($_POST['action']) && $_POST['action'] == 'commit') {
$AgonyAunt = new AgonyAunt();

// we are doing extra validation before saving.
if (isset($_POST['question']))
{
$question->question = $_POST['question'];
}
if (isset($_POST['qemail']))
{
$email->qemail = $_POST['qemail'];
}
if ($AgonyAunt->Save())
{
echo 'question saved successfully';
}
}else{
site_header('AgonyAunt');

echo '
<form action="'.$_SERVER['$PHP_SELF'].'" method="post">
<fieldset>
<legend>Ask a Question</legend>
<div class="required">
<label for="question">Input a question: </label>
<input type="text" name="question" maxlength="255"/>
</div>
<div class="required">
<label for="qemail">Input your email address: </label>
<input type="text" name="qemail" maxlength="255"/>
</div>
<div class="submitButton">
<input type="hidden" name="action" value="commit" />
<input type="submit" name="save" value="Go" />
</div>
</fieldset>
</form>';
}
site_footer();
?>


And i have used the following POG generated class file:

<?php
/*
This SQL query will create the table to store your object.

CREATE TABLE `agonyaunt` (
`agonyauntid` int(11) auto_increment,
`question` VARCHAR(255),
`answer` VARCHAR(255),
`qdate` VARCHAR(255),
`adate` VARCHAR(255),
`approved` VARCHAR(255),
`qemail` VARCHAR(255), PRIMARY KEY (`agonyauntid`));
*/

/**
* <b>AgonyAunt</b> class with integrated CRUD methods.
* @author Php Object Generator
* @version 1.6 rc2
* @copyright Free for personal & commercial use. (Offered under the BSD
license)
* @link
http://www.phpobjectgenerator.com/?language=php4&wrapper=pog&objectName=AgonyAunt&attributeList=array+%28%0A++0+%3D%3E+%27question%27%2C%0A++1+%3D%3E+%27answer%27%2C%0A++2+%3D%3E+%27qDate%27%2C%0A++3+%3D%3E+%27aDate%27%2C%0A++4+%3D%3E+%27approved%27%2C%0A++5+%3D%3E+%27qemail%27%2C%0A%29&typeList=array+%28%0A++0+%3D%3E+%27VARCHAR%28255%29%27%2C%0A++1+%3D%3E+%27VARCHAR%28255%29%27%2C%0A++2+%3D%3E+%27VARCHAR%28255%29%27%2C%0A++3+%3D%3E+%27VARCHAR%28255%29%27%2C%0A++4+%3D%3E+%27VARCHAR%28255%29%27%2C%0A++5+%3D%3E+%27VARCHAR%28255%29%27%2C%0A%29
*/
class AgonyAunt
{
var $agonyauntId;

/**
* @var VARCHAR(255)
*/
var $question;

/**
* @var VARCHAR(255)
*/
var $answer;

/**
* @var VARCHAR(255)
*/
var $qDate;

/**
* @var VARCHAR(255)
*/
var $aDate;

/**
* @var VARCHAR(255)
*/
var $approved;

/**
* @var VARCHAR(255)
*/
var $qemail;

var $pog_attribute_type = array(
"agonyauntid" => array("NUMERIC", "INT"),
"question" => array("TEXT", "VARCHAR", "255"),
"answer" => array("TEXT", "VARCHAR", "255"),
"qDate" => array("TEXT", "VARCHAR", "255"),
"aDate" => array("TEXT", "VARCHAR", "255"),
"approved" => array("TEXT", "VARCHAR", "255"),
"qemail" => array("TEXT", "VARCHAR", "255"),
);
var $pog_query;

function AgonyAunt($question='', $answer='', $qDate='', $aDate='',
$approved='', $qemail='')
{
$this->question = $question;
$this->answer = $answer;
$this->qDate = $qDate;
$this->aDate = $aDate;
$this->approved = $approved;
$this->qemail = $qemail;
}


/**
* Gets object from database
* @param integer $agonyauntId
* @return object $AgonyAunt
*/
function Get($agonyauntId)
{
$Database = new DatabaseConnection();
$this->pog_query = "select * from `agonyaunt` where
`agonyauntid`='".intval($agonyauntId)."' LIMIT 1";
$Database->Query($this->pog_query);
$this->agonyauntId = $Database->Result(0, "agonyauntid");
$this->question = $Database->Unescape($Database->Result(0,
"question"));
$this->answer = $Database->Unescape($Database->Result(0, "answer"));
$this->qDate = $Database->Unescape($Database->Result(0, "qdate"));
$this->aDate = $Database->Unescape($Database->Result(0, "adate"));
$this->approved = $Database->Unescape($Database->Result(0,
"approved"));
$this->qemail = $Database->Unescape($Database->Result(0, "qemail"));
return $this;
}


/**
* Returns a sorted array of objects that match given conditions
* @param multidimensional array {("field", "comparator", "value"),
("field", "comparator", "value"), ...}
* @param string $sortBy
* @param boolean $ascending
* @param int limit
* @return array $agonyauntList
*/
function GetList($fcv_array, $sortBy='', $ascending=true, $limit='')
{
$sqlLimit = ($limit != '' && $sortBy == ''?"LIMIT $limit":'');
if (sizeof($fcv_array) > 0)
{
$agonyauntList = Array();
$Database = new DatabaseConnection();
$this->pog_query = "select agonyauntid from `agonyaunt` where ";
for ($i=0, $c=sizeof($fcv_array)-1; $i<$c; $i++)
{
$this->pog_query .= "`".strtolower($fcv_array[$i][0])."`
".$fcv_array[$i][1]." '".$Database->Escape($fcv_array[$i][2])."' AND";
}
$this->pog_query .= "`".strtolower($fcv_array[$i][0])."`
".$fcv_array[$i][1]." '".$Database->Escape($fcv_array[$i][2])."' order
by agonyauntid asc $sqlLimit";
$Database->Query($this->pog_query);
for ($i=0; $i < $Database->Rows(); $i++)
{
$agonyaunt = new AgonyAunt();
$agonyaunt->Get($Database->Result($i, "agonyauntid"));
$agonyauntList[] = $agonyaunt;
}
if ($sortBy != '')
{
$f = '';
$agonyaunt = new AgonyAunt();
if (isset($agonyaunt->pog_attribute_type[strtolower($sortBy)]) &&
$agonyaunt->pog_attribute_type[strtolower($sortBy)][0] == "NUMERIC")
{
$f = 'return $agonyaunt1->'.$sortBy.' >
$agonyaunt2->'.$sortBy.';';
}
else if
(isset($agonyaunt->pog_attribute_type[strtolower($sortBy)]))
{
$f = 'return strcmp(strtolower($agonyaunt1->'.$sortBy.'),
strtolower($agonyaunt2->'.$sortBy.'));';
}
usort($agonyauntList, create_function('$agonyaunt1, $agonyaunt2',
$f));
if (!$ascending)
{
$agonyauntList = array_reverse($agonyauntList);
}
if ($limit != '')
{
$limitParts = explode(',', $limit);
if (sizeof($limitParts) > 1)
{
return array_slice($agonyauntList, $limitParts[0],
$limitParts[1]);
}
else
{
return array_slice($agonyauntList, 0, $limit);
}
}
}
return $agonyauntList;
}
return null;
}


/**
* Saves the object to the database
* @return integer $agonyauntId
*/
function Save()
{
$Database = new DatabaseConnection();
$this->pog_query = "select agonyauntid from `agonyaunt` where
`agonyauntid`='".$this->agonyauntId."' LIMIT 1";
$Database->Query($this->pog_query);
if ($Database->Rows() > 0)
{
$this->pog_query = "update `agonyaunt` set
`question`='".$Database->Escape($this->question)."',
`answer`='".$Database->Escape($this->answer)."',
`qdate`='".$Database->Escape($this->qDate)."',
`adate`='".$Database->Escape($this->aDate)."',
`approved`='".$Database->Escape($this->approved)."',
`qemail`='".$Database->Escape($this->qemail)."' where
`agonyauntid`='".$this->agonyauntId."'";
}
else
{
$this->pog_query = "insert into `agonyaunt` (`question`, `answer`,
`qdate`, `adate`, `approved`, `qemail` ) values (
'".$Database->Escape($this->question)."',
'".$Database->Escape($this->answer)."',
'".$Database->Escape($this->qDate)."',
'".$Database->Escape($this->aDate)."',
'".$Database->Escape($this->approved)."',
'".$Database->Escape($this->qemail)."' )";
}
$Database->InsertOrUpdate($this->pog_query);
echo $this->pog_query;
return;
if ($this->agonyauntId == "")
{
$this->agonyauntId = $Database->GetCurrentId();
}
return $this->agonyauntId;
}


/**
* Clones the object and saves it to the database
* @return integer $agonyauntId
*/
function SaveNew()
{
$this->agonyauntId = '';
return $this->Save();
}


/**
* Deletes the object from the database
* @return boolean
*/
function Delete()
{
$Database = new DatabaseConnection();
$this->pog_query = "delete from `agonyaunt` where
`agonyauntid`='".$this->agonyauntId."'";
return $Database->Query($this->pog_query);
}
}
?>

When i echo out the pog_query none of my form data is there?
I know this is a very basic concept but it has me stumped...
Any help much appreciated.

Fraser

Aaron Vegh

unread,
Mar 6, 2006, 5:34:16 PM3/6/06
to Php-Object...@googlegroups.com
Fraser,
I don't see where you've included the class file in your code? i.e.

include('class.agonyaunt.php');

Cheers,
Aaron

Joel

unread,
Mar 6, 2006, 5:34:52 PM3/6/06
to Php Object Generator
Hi there,

replace


if (isset($_POST['question']))
{
$question->question = $_POST['question'];
}
if (isset($_POST['qemail']))
{
$email->qemail = $_POST['qemail'];
}
if ($AgonyAunt->Save())
{
echo 'question saved successfully';
}


with

if (isset($_POST['question']))
{
$AgonyAunt->question = $_POST['question'];
}
if (isset($_POST['qemail']))
{
$AgonyAunt->qemail = $_POST['qemail'];
}
if ($AgonyAunt->Save())
{
echo 'AgonyAunt saved successfully';
}


you were not assigning the post values to the agonyaunt object, which
is why it wasn't saving.

let me know if this helps

Joel

dirtyone30

unread,
Mar 7, 2006, 4:04:02 AM3/7/06
to Php Object Generator
DOH !


I need more coffee.

Thank you ever so much.

Fraser

By the way pog is a great tool, i am using it to build a new site for
one of scotlands leading advertising agencies.

Some of the functionality from phase 1 can be seen at
www.greetingsfromleith.co.uk...

Thanks again guys...

Reply all
Reply to author
Forward
0 new messages