Ben bir MySQL veritabanı kayıtları yöneten bir web formu var. Ben hem yeni kayıt oluşturma ve bunları düzenlemek için bir düzenleme arayüz görüntülemek için bir yöntem var
if ($_POST['new_page']) {
print "<h2>Create new page</h2>\n";
$isNew=1;
$this->EditForm();
} else if($_POST['edit']){
print "<h2>Edit page</h2>\n";
$isNew=0;
$this->EditForm();
}
Ben bir kayıt eklenmesi veya güncelleştirilmesi için nerede belirlemek için global değişken $ isNew kullanmaya çalışıyorum. Benim SaveChanges () fonksiyonu çalıştırıldığında zaman Ancak, $ isNew her zaman 0'dır. $ IsNew tüm fonksiyonları dışında, hemen sınıf ilanından sonra ilan edilir.
class Editor{
public $isNew;
Tam kod örneği (dan http://pastebin.com/40TQFEd5):
When the object is created in index.php, the method HTMLEditorHandler() is called
<?php
class HTMLEditor{
var $isNew;
function SaveChanges($author, $company, $title, $content, $new){
// Get AuthorID
// Search database for ID
$sql="SELECT ID";
$sql.=" FROM authors";
$sql.=" WHERE Name = '$author'";
$author_id=$this->db->getOne($sql);
// If author not found, add to database
if(!$author_id){
$sql="INSERT INTO authors(Name)";
$sql.="VALUES ('{$author}')";
$this->db->query($sql);
$author_id=mysql_insert_id();
}
print "isNew: ".$this->isNew;
/*if($this->isNew==1){
$sql="INSERT INTO pages(CompanyID, AuthorID, Title, Content, DateCreated, DateUpdated)";
$sql.=" VALUES ('{$company}', '{$author_id}', '{$title}', '{$content}', NOW(), NOW())";
$this->db->query($sql);
} else if($this->isNew==0){
print "Not new";
}*/
}
function EditForm($isNew){
if(isset($_POST['pageID'])){
$sql="SELECT Name, Title, Content, CompanyID";
$sql.=" FROM pages, authors\n";
$sql.=" WHERE pages.AuthorID = authors.ID";
$sql.=" AND pages.ID = '".$_POST['pageID']."'";
$result=$this->db->query($sql);
$row=$result->fetchRow();
$company=$row['CompanyID'];
}
print "<form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">\n";
print "<table width=\"100%\"summary=\"New Page\"\n>";
print "<tr>\n";
print "<th>Author: </th>\n";
print "<td><input type=\"text\" name=\"author\"";
if(isset($row['Name'])){
print "value=\"".$row['Name']."\"";
}
print "/></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<th>Company: </th>\n";
print "<td>\n";
$this->ShowCompanies($company);
print "</td>\n";
print "</tr>\n";
print "<tr>\n";
print "<th>Title: </th>\n";
print "<td><input type=\"text\" name=\"title\"";
if(isset($row['Title'])){
print "value=\"".$row['Title']."\"";
}
print "/></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<th>Content: </th>\n";
print "<td>\n";
print $this->myToolBar->EditableArea("content", htmlspecialchars($row['Content']), "100%", 400, "NoSave");
print "</td>\n";
print "</tr>\n";
print "</table>\n";
print "<input type=\"submit\" name=\"save\" value=\"Save\"/>\n";
print "<input type=\"submit\" name=\"\" value=\"Cancel\"/>\n";
print "</form>\n";
}
function DefaultForm(){
print "<form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">\n";
print "<input type=\"submit\" name=\"new_page\" value=\"Create a new page\"/>";
print "<h2>Edit an existing page</h2>\n";
print "<table summary=\"Edit Page\">\n";
print "<tr><th>Year</th><td>";
print "<select name=\"year\" onchange=\"showPages()\" id=\"year_select\">\n";
for ($year=date('Y'), $max_year=date('Y')-10; $year > $max_year; $year--) {
print "<option value=\"".$year."\">".$year."</option>\n";
}
print "</select>\n";
print "</td></tr>";
print "<tr><th>Company: </th><td>";
$sql="SELECT organisations.OrgID, companynames.CompanyName";
$sql.=" FROM qsvision.organisations";
$sql.=" LEFT JOIN qsvision.companynames";
$sql.=" ON qsvision.organisations.CompanyID=qsvision.companynames.CompanyID";
$sql.=" WHERE CompanyName!=''";
$sql.=" GROUP BY companynames.CompanyID";
$sql.=" ORDER BY companynames.CompanyName ASC";
$organisations=$this->db->getAll($sql);
print "<select name=\"org_id\" onchange=\"showPages()\" id=\"org_id\">\n";
print "<option value=\"\">[Select...]</option>\n";
for($i=0, $max_i=count($organisations); $i<$max_i; $i++){
print "<option value=\"{$organisations[$i]['OrgID']}\"";
if($site['OrgID']==$organisations[$i]['OrgID']){
print " selected=\"selected\"";
}
print ">".htmlspecialchars($organisations[$i]['CompanyName'])."</option>\n";
}
print "</select>\n";
print "</td></tr>\n";
print "</table>";
print "<div id=\"results_table\"></div>";
print "</form>";
}
function HTMLEditorHandler(){
if ($_POST['new_page']) {
print "<h2>Create new page</h2>\n";
$this->EditForm(true);
} else if($_POST['edit']){
print "<h2>Edit page</h2>\n";
$this->EditForm(false);
} else if($_POST['delete']){
$this->DeletePage();
$this->DefaultForm();
} else if($_POST['save']){
$this->SaveChanges($_POST['author'], $_POST['org_id'], $_POST['title'], $_POST['content'],$this->isNew);
$this->DefaultForm();
} else {
$this->DefaultForm();
}
}
}
?>