0

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;";
    }
}
Geoff_S
  • 4,917
  • 7
  • 43
  • 133
  • First things first, please [sanitise your input](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)! This allows for sql injections. – Stratadox Sep 16 '18 at 23:23
  • Unless you are using a sort of framework for sql queries and using mysqli, try replacing the line `$existingPanel->count == 0` with `$existingPanel->num_rows == 0`. NB, you will probably run into the next error on line `$cont_id = $mysqlConn->insert_id;` – Yolo Sep 16 '18 at 23:28
  • @yolo when I previously had just the inserts it's been working fine with no errors so the only issue now is adding the if/else and the update. I basically just want to make it so that the first submission does what I was previously doing but if they edit it then it updates – Geoff_S Sep 16 '18 at 23:38
  • @Yolo num_rows did not work either unfortunately – Geoff_S Sep 16 '18 at 23:48
  • Well the error you are referring to "undefined property of mysqli_result::$count" means that count does not exist, which may equal to 0, which in turn means that statement is always correct and the insert is always chosen over update. Have a look at the [documentation](http://php.net/manual/en/mysqli-result.num-rows.php). I notice that you are not calling `$mysqlConn->query($updateContent)` after setting `$updateContent`. so no update happening from my point of view. – Yolo Sep 16 '18 at 23:48
  • @Yolo right that's true I haven't set that yet just trying to see if I can even get that far. So when I test the initial select in mysql workbench it returns 0 for the count, which I would think would suffice for it to do the inserts there – Geoff_S Sep 16 '18 at 23:50
  • But `$existingPanel->count == 0` will always be true because count does not exists in your query object. So it does not matter what you do, insert will always be carried out. See the link to documentation in my previous comment. – Yolo Sep 16 '18 at 23:53
  • I see that, but the inserts are not even working currently. So even going by that error, It's not even inserting the first time like it was previously – Geoff_S Sep 16 '18 at 23:55
  • I did actually get num_rows to get past that error but it then goes to undefined property for mysqli_result::$existingContent near the end of my code, so it's not paying attention to the if else, since it returned 0 as the count but still hit this line in my else block – Geoff_S Sep 16 '18 at 23:58
  • @Yolo ah, I just dupmed $existingPanels and it has num_rows as 1, because count returning 0 will always be a row, so that's why it's not inserting but then it also wasn't updating – Geoff_S Sep 17 '18 at 00:04

0 Answers0