Using value from select in update, getting undefined property

1601 views php
5

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";
    }

answered question

what happened with the first post?

I actually got past it by using num_rows but I realized my issue now was that it was getting to this point and failing at the where clause.

2 Answers

13

Try assigning $existingContent->existingContent to a variable first and then use it in the query

$content=$existingContent->existingContent;

$updateContent = "
        UPDATE content
            SET content = '$content'
            WHERE id = $content;";

Also as mentioned in a comment, you risk SQL injections.

posted this
3

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

posted this

Have an answer?

JD

Please login first before posting an answer.