0

I'm having an issue with using a value for a select in a different update statement.

Below, I run a select statement and the print_r($existingContent) is printing the object with num_rows, field_count etc. But I can't figure out how to dump the ID I need.

I'm currently getting an error at this line:

WHERE id = $existingContent->existingContent

saying that $existingContent is an undefined property. I'm assuming it's becuase of how I'm setting it from the select but I can't debug because my print_r isn't showing how my actual response is structured. I just need the ID from that select statement to be used in my where clause

$checkContentExists = "SELECT cont_id as existingContent FROM panels WHERE panel_type_id = $panelID AND page_id = $pageID";

$existingContent = $mysqlConn->query($checkContentExists);

print_r($existingContent);  

    $updateContent = "
        UPDATE content
            SET content = '$content'
            WHERE id = $existingContent->existingContent;";

    if($mysqlConn->query($updateContent) === TRUE){
        echo "Record Updated";
    }

UPDATE

while ($row = mysqli_fetch_assoc($existingContent)){

    print_r($row);

   //If i use the commented out code I get a 500 error

        // $updateContent = "
   //       UPDATE content
   //           SET content = '$content'
   //           WHERE id = $row[existingContent];
   //      ";

   //      $updateResult = $mysqlConn->query($updateContent);
 }
Whisou138
  • 451
  • 5
  • 21

1 Answers1

0

Once you solve the problem by avoiding sql injection susceptible code, the problem will go away.

In this case, more complex string interpolations like $existingContent->existingContent cannot occur in PHP.

You can also combine the SELECT and UPDATE into a single UPDATE query.

UPDATE content
JOIN panels ON content.id = panels.cont_id
SET content = :content
WHERE page_id = :page
danblack
  • 12,130
  • 2
  • 22
  • 41