Josh R
Josh R

Reputation: 29

running a mysqli update query in php and it returns that it updated when it doesnt

So what is happening is when I submit the page it should register that a user has already submitted a hope and then update that to their new one they just submitted instead of creating a new one, however when runs, it returns that it has successfully updated when it has not, any ideas on what to change to make it run correctly?

I have checked that it is grabbing all the information that is needed to run the update query by echoing out both the hope and the hopeId retrieved from the server and they come out correctly. Then I ran the update query directly in mysql workbench with the returned hope and hopeId and it successfully updates the row. From this I believe that have made a mistake while writing the statement in the MySQLDAO.

This is my code, its over a few pages so I grabbed all the code that I believed was relevant and labeled them separately(if you need any other code just shout and ill add it as an edit as soon as I can):

This is the todaysHope.php

$dao = new MySQLDAO($dbhost, $dbuser, $dbpassword, $dbname);
$dao->openConnection();

$userHope = $dao->getHope($userId);

if(!empty($userHope))
{
    $hopeId=$userHope["hopeId"];

    $updateResult =$dao->updateTodaysHope($hopeId, $hope);

    if ($updateResult)
    {
        $returnValue["status"]="200";
        $returnValue["message"]="Hope updated";
        $returnValue["hope"]=$hope;
    } else {
        $returnValue["status"]="400";
        $returnValue["message"]="A problem has occured";
    }

} else  {

    $result =$dao->saveTodaysHope($userId, $hope);

    if ($result)
    {
        $returnValue["status"]="200";
        $returnValue["message"]="Hope submitted";  
    } else {
        $returnValue["status"]="400";
        $returnValue["message"]="A problem has occured";
    }

}

This is the MySQLDAO.php

public function saveTodaysHope($userId, $hope) 
    {
        $sql = "insert into todaysHope set userId=?, hope=?";
        $statement = $this->conn->prepare($sql);

        if (!statement)
            throw new Exception ($statement->error);

        $statement->bind_param("is", $userId, $hope);
        $returnValue = $statement->execute();

        return $returnValue;

}

public function updateTodaysHope($hope, $hopeId) 
    {
        $sql = "update todaysHope set hope=? where hopeId=?";
        $statement = $this->conn->prepare($sql);

        if (!statement)
            throw new Exception ($statement->error);

        $statement->bind_param("si", $hope, $hopeId);
        $returnValue = $statement->execute();

        return $returnValue;

}

Sorry if this is something really straight forward that I am just missing but I have been trying to get this bit of code to work for a couple days now and cant think of anything else to try. Thank you for helping.

Upvotes: 0

Views: 230

Answers (3)

Mairu
Mairu

Reputation: 56

If MySQLDAO is a subclass of PDO then $statement->execute() will return a boolean value. The number of affected rows can be retrieved with $statement->rowCount().

http://php.net/manual/en/pdostatement.execute.php

http://php.net/manual/en/pdostatement.rowcount.php


For mysqli it is quite the same, execute() returns just boolean value, but there then it is not a method but a property to get the affected rows - would be $statement->affected_rows

http://php.net/manual/en/mysqli-stmt.execute.php

http://php.net/manual/en/mysqli-stmt.affected-rows.php

Upvotes: 0

Michael Hommé
Michael Hommé

Reputation: 1726

You're missing a $ in your $statement variable. Change this if (!statement) to if (!$statement) in both tests.

Upvotes: 3

Ian
Ian

Reputation: 590

It looks like you're passing your variables the wrong way round to the function:

$updateResult =$dao->updateTodaysHope($hopeId, $hope);

public function updateTodaysHope($hope, $hopeId)

Upvotes: 3

Related Questions