I'm having a bit of an issue here.
I'm submitting a form, and when it submits fort he first time I just need the 2 inserts to happen ( I have to insert the content, then insert a panel record that associates with the content) but if the panel type exists for that page ID already, I need to get the content record that exists and update it.
The problem is that I'm getting an error on the line if($existingPanel->count == 0){
for an undefined property of mysqli_result::$count so it's no longer inserting, let alont updating.
It worked before adding the new if/else based on the count.
How can I better structure this so that it will work correctly?
$content = $_POST['page_content'];
$panelID = $_POST['panel_type'];
$pageID = $_POST['page_id'];
//Check is a panel record exists for the page
$checkIfExists = "SELECT COUNT(*) AS count FROM panels WHERE panel_type_id = $panelID AND page_id = $pageID";
$existingPanel = $mysqlConn->query($checkIfExists);
//if no record exists for the panel type in this page
if($existingPanel->count == 0){
//add content record
$addContent = "
INSERT INTO content(content)
VALUES('$content');
";
if ($mysqlConn->query($addContent) === TRUE) {
$cont_id = $mysqlConn->insert_id;
$data['last_insert_id'] = $cont_id;
echo json_encode($data);
} else {
echo "Error: " . $addContent . "<br>" . $mysqlConn->error;
}
//add panel record with association to content
$addPanel = "
INSERT INTO panels(panel_type_id, page_id, cont_id)
VALUES ('$panelID', '$pageID', '$cont_id');
";
if ($mysqlConn->query($addPanel) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $addPanel . "<br>" . $mysqlConn->error;
}
//if the count is 1 from $checkExisting
}else {
//get content ID for the existing content
$checkContentExists = "SELECT cont_id as existingContent FROM panels WHERE panel_type_id = $panelID AND page_id = $pageID";
$existingContent = $mysqlConn->query($checkContentExists);
//execute and update content for the content ID
if($mysqlConn->query($checkContentExists)){
$updateContent = "
UPDATE content
SET content = '$content'
WHERE id = $existingContent->existingContent;";
}
}