I have 2 tables in my MySQL DB where column “campaignid” in table “pages” refference (= to) column “id” in the table “campaigns.
ALTER TABLE pages ADD FOREIGN KEY (campaignid) REFERENCES campaigns(id);
The data in these columns is called by this public function:
public function get_pages_by_campaign_id($campaignID) {
$campaignID = $this->real_escape_string($campaignID);
return $this->query("SELECT pid, campaignid FROM pages campaigns WHERE campaignid = 'campaigns.id'");
}
However when I try to delete a “page” using:
public function delete_page($pageID) {
$this->query("DELETE FROM pages WHERE id = " . $pageID);
}
Nothing happens. Should I have set something like ON DELETE SET NULL | ON UPDATE CASCADE or something????
At the moment I cannot update, delete or save anything to the DB, which has been the case for 2 days now … any help would be Greatly Appreciated!
Your syntax is incorrect. Your query uses
campaignid = 'campaigns.id'but the quotes indicate that is a variable, when I assume its the column you’re looking for. You need commas between the table names. You also don’t use the variable you pass into the function.I assume you want to get data from the campaigns table in your query too? Otherwise you don’t need to include it in the query.
EDIT: AS stated in the comments you use id in your delete – should that be pid like you use in the select? Also, put quotes around the variable (not necessary but prevents DB error is the variable is empty)