user8010
user8010

Reputation: 35

Echo after Update Query

I would like to echo Test Closed once the update query has been run. If it fails I would like to echo test not closed.

 if (isset($_POST['closetest'])){
       $ts = $_GET["tid"];
       $stmt = mysqli_prepare($conn, "UPDATE test_set SET isOpen = 0 WHERE id= ?");
       $stmt->bind_param("i", $ts);
       $stmt->execute();

         echo 'Test Closed';

 }

    $stmt->close();
    $conn->close();

}

Upvotes: 0

Views: 143

Answers (3)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

You have a few options.

Either check for errors:

if(!$stmt->execute()){
    trigger_error("There was an error.... ".$conn->error, E_USER_WARNING);
}

or use mysqli_affected_rows()

Reference:

Object oriented style

int $mysqli->affected_rows;

Procedural style

int mysqli_affected_rows ( mysqli $link )

Returns the number of rows affected by the last INSERT, UPDATE, REPLACE or DELETE query.

Mind you, checking for errors at the same time would be best.

References:

Upvotes: 1

Fakhruddin Ujjainwala
Fakhruddin Ujjainwala

Reputation: 2553

Update your code like:

if (!$stmt->execute()){
    echo "Test Closed!";
}
else{
    echo "Test Not Closed!";
}

Upvotes: 1

RiggsFolly
RiggsFolly

Reputation: 94672

Not a difficult thing to do if you take a look at at the manual

if (isset($_POST['closetest']))
{

    $ts = $_GET["tid"];

    $stmt = mysqli_prepare($conn, "UPDATE test_set 
                                          SET isOpen = 0 WHERE id= ?");
    $stmt->bind_param("i", $ts);
    $result = $stmt->execute();

    if ( $result === FALSE ) {
        echo 'Test Not closed, in fact the query failed ' . $stmt->error;
    } else {
        echo "Test Closed!";     
    }

    $stmt->close();
    $conn->close();

}

Upvotes: 1

Related Questions