Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Global variable in class is reset
    primarykey
    data
    text
    <p>I have a web form that manipulates records in a MySQL database. I have a method for displaying an edit interface for both creating new records and editing them</p> <pre><code>if ($_POST['new_page']) { print "&lt;h2&gt;Create new page&lt;/h2&gt;\n"; $isNew=1; $this-&gt;EditForm(); } else if($_POST['edit']){ print "&lt;h2&gt;Edit page&lt;/h2&gt;\n"; $isNew=0; $this-&gt;EditForm(); } </code></pre> <p>I am trying to use the global variable $isNew to determine where a record is to be added or updated. However, whenever my SaveChanges() function is run, $isNew is always 0. $isNew is declared immediately after the class declaration, outside all of the functions.</p> <pre><code>class Editor{ public $isNew; </code></pre> <hr> <p>Full code sample (from <a href="http://pastebin.com/40TQFEd5" rel="nofollow noreferrer">http://pastebin.com/40TQFEd5</a>):</p> <pre><code>When the object is created in index.php, the method HTMLEditorHandler() is called &lt;?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-&gt;db-&gt;getOne($sql); // If author not found, add to database if(!$author_id){ $sql="INSERT INTO authors(Name)"; $sql.="VALUES ('{$author}')"; $this-&gt;db-&gt;query($sql); $author_id=mysql_insert_id(); } print "isNew: ".$this-&gt;isNew; /*if($this-&gt;isNew==1){ $sql="INSERT INTO pages(CompanyID, AuthorID, Title, Content, DateCreated, DateUpdated)"; $sql.=" VALUES ('{$company}', '{$author_id}', '{$title}', '{$content}', NOW(), NOW())"; $this-&gt;db-&gt;query($sql); } else if($this-&gt;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-&gt;db-&gt;query($sql); $row=$result-&gt;fetchRow(); $company=$row['CompanyID']; } print "&lt;form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\"&gt;\n"; print "&lt;table width=\"100%\"summary=\"New Page\"\n&gt;"; print "&lt;tr&gt;\n"; print "&lt;th&gt;Author: &lt;/th&gt;\n"; print "&lt;td&gt;&lt;input type=\"text\" name=\"author\""; if(isset($row['Name'])){ print "value=\"".$row['Name']."\""; } print "/&gt;&lt;/td&gt;\n"; print "&lt;/tr&gt;\n"; print "&lt;tr&gt;\n"; print "&lt;th&gt;Company: &lt;/th&gt;\n"; print "&lt;td&gt;\n"; $this-&gt;ShowCompanies($company); print "&lt;/td&gt;\n"; print "&lt;/tr&gt;\n"; print "&lt;tr&gt;\n"; print "&lt;th&gt;Title: &lt;/th&gt;\n"; print "&lt;td&gt;&lt;input type=\"text\" name=\"title\""; if(isset($row['Title'])){ print "value=\"".$row['Title']."\""; } print "/&gt;&lt;/td&gt;\n"; print "&lt;/tr&gt;\n"; print "&lt;tr&gt;\n"; print "&lt;th&gt;Content: &lt;/th&gt;\n"; print "&lt;td&gt;\n"; print $this-&gt;myToolBar-&gt;EditableArea("content", htmlspecialchars($row['Content']), "100%", 400, "NoSave"); print "&lt;/td&gt;\n"; print "&lt;/tr&gt;\n"; print "&lt;/table&gt;\n"; print "&lt;input type=\"submit\" name=\"save\" value=\"Save\"/&gt;\n"; print "&lt;input type=\"submit\" name=\"\" value=\"Cancel\"/&gt;\n"; print "&lt;/form&gt;\n"; } function DefaultForm(){ print "&lt;form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\"&gt;\n"; print "&lt;input type=\"submit\" name=\"new_page\" value=\"Create a new page\"/&gt;"; print "&lt;h2&gt;Edit an existing page&lt;/h2&gt;\n"; print "&lt;table summary=\"Edit Page\"&gt;\n"; print "&lt;tr&gt;&lt;th&gt;Year&lt;/th&gt;&lt;td&gt;"; print "&lt;select name=\"year\" onchange=\"showPages()\" id=\"year_select\"&gt;\n"; for ($year=date('Y'), $max_year=date('Y')-10; $year &gt; $max_year; $year--) { print "&lt;option value=\"".$year."\"&gt;".$year."&lt;/option&gt;\n"; } print "&lt;/select&gt;\n"; print "&lt;/td&gt;&lt;/tr&gt;"; print "&lt;tr&gt;&lt;th&gt;Company: &lt;/th&gt;&lt;td&gt;"; $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-&gt;db-&gt;getAll($sql); print "&lt;select name=\"org_id\" onchange=\"showPages()\" id=\"org_id\"&gt;\n"; print "&lt;option value=\"\"&gt;[Select...]&lt;/option&gt;\n"; for($i=0, $max_i=count($organisations); $i&lt;$max_i; $i++){ print "&lt;option value=\"{$organisations[$i]['OrgID']}\""; if($site['OrgID']==$organisations[$i]['OrgID']){ print " selected=\"selected\""; } print "&gt;".htmlspecialchars($organisations[$i]['CompanyName'])."&lt;/option&gt;\n"; } print "&lt;/select&gt;\n"; print "&lt;/td&gt;&lt;/tr&gt;\n"; print "&lt;/table&gt;"; print "&lt;div id=\"results_table\"&gt;&lt;/div&gt;"; print "&lt;/form&gt;"; } function HTMLEditorHandler(){ if ($_POST['new_page']) { print "&lt;h2&gt;Create new page&lt;/h2&gt;\n"; $this-&gt;EditForm(true); } else if($_POST['edit']){ print "&lt;h2&gt;Edit page&lt;/h2&gt;\n"; $this-&gt;EditForm(false); } else if($_POST['delete']){ $this-&gt;DeletePage(); $this-&gt;DefaultForm(); } else if($_POST['save']){ $this-&gt;SaveChanges($_POST['author'], $_POST['org_id'], $_POST['title'], $_POST['content'],$this-&gt;isNew); $this-&gt;DefaultForm(); } else { $this-&gt;DefaultForm(); } } } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload