php - No mySQL error thrown while trying to UPDATE an empty row

896

Building a small project and trying to learn as I go along.

I get error codes fine if tables are misnamed etc but if I try to UPDATE an empty row I do not get an error. I want to but it isn't telling me I have screwed up. Is this normal?

public function updateMessage($id){  //done
    try{
        global $pdo;
        $temp=$this->_message;
        $sql = "UPDATE message SET content=:val  WHERE id=$id";
        $s = $pdo->prepare($sql);
        $s->bindValue(':val',$temp);
        $s->execute();
    }
    catch (PDOException $e)
    { $loc = $_SERVER['PHP_SELF'];
    $output = "Unable to connect to the database server: $loc <br><h3>Please contact 
    Steve via text on ###### quoting:</h3><h5>" . $e->getMessage() .  "<br>
    Found at $loc.</h5>
    <h3>Thanks. </h3>". "<br>"."<br>" ;
    include $_SERVER['DOCUMENT_ROOT'] ."/beta01/includes/output.html.php";
    exit();
    }
}

As I said it works as expected with most errors just not on the empty update problem.

116

Answer

Solution:

$sql = "UPDATE message SET content=:val  WHERE id=$id";

If it's an "empty row" (assuming I understand you right), then it doesn't meet the conditionWHERE id=$id, so it worked - it updated the contents of message for every row with that id (there were none).

It sounds like you want to know if no rows were affected by the query, in which case you could do:

$s->execute();
if ($s->rowCount() < 1) {
    //throw an exception, show a warning, whatever you want to do
}

People are also looking for solutions to the problem: if statement - php- trying to have a text that can have 2 hyperlinks

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.