上下文:
我正在使用php langage来开发Dolibarr software上的新模块,我需要你的帮助,因为我从2天起就没有克服我的问题。
流程:
我创建了一个非常简单的php表单,只有2个字段:lastname
(法语中为“nom”)和firstname
(法语中为“prénom”)。我填写表单,然后我提交它,我应该将我的对象保存到我的MySQL数据库中。
问题:
我收到一个空白页面,没有任何对象保存到我的数据库中。
文件:
根据新表格的第一个:
#llx_moduletest_myobject.sql
CREATE TABLE llx_moduletest_myobject(
-- BEGIN MODULEBUILDER FIELDS
rowid INTEGER AUTO_INCREMENT PRIMARY KEY,
nom VARCHAR(255) NOT NULL,
prenom VARCHAR(255) NOT NULL
-- END MODULEBUILDER FIELDS
) ENGINE=innodb;
第二个对应于动作和视图:
#card.php
<?php
// Load traductions files requiredby by page
$langs->loadLangs(array("moduletest@moduletest","other"));
// Get parameters
$id = GETPOST('id', 'int');
$ref = GETPOST('ref', 'alpha');
$action = GETPOST('action', 'alpha');
$cancel = GETPOST('cancel', 'aZ09');
$backtopage = GETPOST('backtopage', 'alpha');
// Initialize technical objects
$object=new MyObject($db);
/*
* Actions
*
* Put here all code to do according to value of "action" parameter
*/
// If create a request
if ($action == 'create')
{
$object = new FormFile($db);
$db->begin();
$nom = GETPOST('nom');
$prenom = GETPOST('prenom');
// If no name
if (empty($nom))
{
setEventMessages($langs->trans("Pas de nom"), null, 'errors');
$error++;
$action='create';
}
// If no firstname
if (empty($prenom))
{
setEventMessages($langs->trans("Pas de prénom"), null, 'errors');
$error++;
$action='create';
}
$result = 0;
if (! $error)
{
$object->nom = $nom;
$object->prenom = $prenom;
$result = $object->create($user); //return int
if ($result <= 0)
{
setEventMessages($object->errors, 'errors');
$error++;
}
}
// If no SQL error we redirect to the request card
if (! $error)
{
print $db->commit();
$db->commit();
header("Location: ".$_SERVER['PHP_SELF'].'?id='.$id);
exit;
}
else
{
$db->rollback();
}
}
/* ************************
View
*********************** */
$form=new Form($db);
$formfile=new FormFile($db);
llxHeader('', $langs->trans('Formulaire de test'));
// Part to create
if ($action == 'create')
{
// Formulaire de test
print load_fiche_titre($langs->trans('Formulaire de test'), '', 'title_hrm.png');
// Formulaire de test
print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'" onsubmit="return valider()" name="Formulaire de test">'."\n";
print '<input type="hidden" name="action" value="create" />'."\n";
dol_fiche_head();
print '<table class="border" width="100%">';
print '<tbody>';
// Nom
print '<tr>';
print '<td class="fieldrequired">'.$langs->trans("Nom").'</td>';
print '<td>';
print '<input type="text" name="nom" value="'.$object->nom.'">';
print '</td></tr>';
// Prenom
print '<tr>';
print '<td class="fieldrequired">'.$langs->trans("Prenom").'</td>';
print '<td>';
print '<input type="text" name="prenom" value="'.$object->prenom.'">';
print '</td></tr>';
print '</tbody>';
print '</table>';
dol_fiche_end();
print '<div class="center">';
print '<input type="submit" value="'.$langs->trans("Valider le formulaire").'" name="bouton" class="button">';
print ' ';
print '<input type="button" value="'.$langs->trans("Cancel").'" class="button" onclick="history.go(-1)">';
print '</div>';
print '</form>'."\n";
}
// End of page
llxFooter();
$db->close();
我有一个create()
函数:$result = $object->create($user); //return int
此函数允许在我的数据库中创建对象:
#moduletestmyobject.class.php
function create($user, $notrigger=0)
{
global $conf, $langs;
$error=0;
// Clean parameters
if (isset($this->prop1)) $this->prop1=trim($this->prop1);
if (isset($this->prop2)) $this->prop2=trim($this->prop2);
// Check parameters
// Put here code to add control on parameters values
// Insert request
$sql = "INSERT INTO ".MAIN_DB_PREFIX."moduletest_myobject(";
$sql.= " field1,";
$sql.= " field2";
$sql.= ") VALUES (";
$sql.= " '".$this->prop1."',";
$sql.= " '".$this->prop2."'";
$sql.= ")";
$this->db->begin();
dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
$resql=$this->db->query($sql);
if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
if (! $error)
{
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."moduletest_myobject");
}
// Commit or rollback
if ($error)
{
foreach($this->errors as $errmsg)
{
dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
$this->error.=($this->error?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
}
else
{
$this->db->commit();
return $this->id;
}
}
我在apache2日志中遇到了这个问题:
Uncaught Error: Call to undefined method FormFile::create()
答案 0 :(得分:0)
我将获得空白页,这是因为您检查的是action ==“ create”还是加载页面时的action没有默认值。
问题:未捕获的错误:调用未定义的方法FormFile :: create()->将$object = new FormFile($db);
替换为$object=new MyObject($db);
答案 1 :(得分:0)
首先,您必须在php页面的开头加载dolibarr环境
// Load Dolibarr environment
$res=0;
// Try main.inc.php into web root known defined into CONTEXT_DOCUMENT_ROOT (not always defined)
if (! $res && ! empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) $res=@include($_SERVER["CONTEXT_DOCUMENT_ROOT"]."/main.inc.php");
// Try main.inc.php into web root detected using web root calculated from SCRIPT_FILENAME
$tmp=empty($_SERVER['SCRIPT_FILENAME'])?'':$_SERVER['SCRIPT_FILENAME'];$tmp2=realpath(__FILE__); $i=strlen($tmp)-1; $j=strlen($tmp2)-1;
while($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i]==$tmp2[$j]) { $i--; $j--; }
if (! $res && $i > 0 && file_exists(substr($tmp, 0, ($i+1))."/main.inc.php")) $res=@include(substr($tmp, 0, ($i+1))."/main.inc.php");
if (! $res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i+1)))."/main.inc.php")) $res=@include(dirname(substr($tmp, 0, ($i+1)))."/main.inc.php");
// Try main.inc.php using relative path
if (! $res && file_exists("../main.inc.php")) $res=@include("../main.inc.php");
if (! $res && file_exists("../../main.inc.php")) $res=@include("../../main.inc.php");
if (! $res && file_exists("../../../main.inc.php")) $res=@include("../../../main.inc.php");
if (! $res) die("Include of main fails");
include_once(DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php');
include_once(DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php');