Grim Vic
Grim Vic

Reputation: 47

Mysql restrict on delete check

I have a mysql database with restrict on delete setting. I have this delete query:

if(isset($_POST['delete_id']))
{
    $sqldelete="DELETE FROM tblAcqDetail WHERE ID=".$_POST['delete_id'];
    $resultdelete = $conn->query($sqldelete);
}

How can I check if the query does delete anything or is restricted by mysql.

I need to run an update query (see below) only if the delete query works. I tried:

if ($resultdelete->affected_rows> 0)  {
    // Escape user inputs for security
    $status = mysqli_real_escape_string($link, $_POST['status']);
    if(isset($_POST['status']))
    {
        $setsql="UPDATE tblInvoiceDetail SET TRANSFER = '0' WHERE ID='$status'";
        $setresult = $conn->query($setsql);
    }
}

I also tried

if ($resultdelete->num_rows > 0)  {

And also :

if ($resultdelete)  {

All of the above stop the update query from executing.

Upvotes: 1

Views: 361

Answers (5)

Sugumar Venkatesan
Sugumar Venkatesan

Reputation: 4038

If condition should be

  if ($conn->affected_rows> 0){}

not

  if ($resultdelete->affected_rows> 0){}       

Upvotes: 1

Funk Forty Niner
Funk Forty Niner

Reputation: 74219

You're using it wrong if ($resultdelete->affected_rows> 0) you're using num_rows() syntax with the >0 bit.

  • The connection is passed to the function and not from the result set.

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

Object oriented style int $mysqli->affected_rows;

Procedural style int mysqli_affected_rows ( mysqli $link )

From the manual:

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* Insert rows */
$mysqli->query("CREATE TABLE Language SELECT * from CountryLanguage");
printf("Affected rows (INSERT): %d\n", $mysqli->affected_rows);

$mysqli->query("ALTER TABLE Language ADD Status int default 0");

/* update rows */
$mysqli->query("UPDATE Language SET Status=1 WHERE Percentage > 50");
printf("Affected rows (UPDATE): %d\n", $mysqli->affected_rows);

/* delete rows */
$mysqli->query("DELETE FROM Language WHERE Percentage < 50");
printf("Affected rows (DELETE): %d\n", $mysqli->affected_rows);

/* select all rows */
$result = $mysqli->query("SELECT CountryCode FROM Language");
printf("Affected rows (SELECT): %d\n", $mysqli->affected_rows);

$result->close();

/* Delete table Language */
$mysqli->query("DROP TABLE Language");

/* close connection */
$mysqli->close();
?>

Procedural style
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

if (!$link) {
    printf("Can't connect to localhost. Error: %s\n", mysqli_connect_error());
    exit();
}

/* Insert rows */
mysqli_query($link, "CREATE TABLE Language SELECT * from CountryLanguage");
printf("Affected rows (INSERT): %d\n", mysqli_affected_rows($link));

mysqli_query($link, "ALTER TABLE Language ADD Status int default 0");

/* update rows */
mysqli_query($link, "UPDATE Language SET Status=1 WHERE Percentage > 50");
printf("Affected rows (UPDATE): %d\n", mysqli_affected_rows($link));

/* delete rows */
mysqli_query($link, "DELETE FROM Language WHERE Percentage < 50");
printf("Affected rows (DELETE): %d\n", mysqli_affected_rows($link));

/* select all rows */
$result = mysqli_query($link, "SELECT CountryCode FROM Language");
printf("Affected rows (SELECT): %d\n", mysqli_affected_rows($link));

mysqli_free_result($result);

/* Delete table Language */
mysqli_query($link, "DROP TABLE Language");

/* close connection */
mysqli_close($link);
?>

Upvotes: 0

Machavity
Machavity

Reputation: 31654

Your problem is you're referencing the wrong thing

if ($resultdelete->affected_rows> 0)  {

But

$resultdelete = $conn->query($sqldelete);

only returns a boolean(emphasis mine).

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE

You want to reference the connection itself for how many rows were affected

if ($conn->affected_rows> 0)  {

Upvotes: 1

Saty
Saty

Reputation: 22532

Rather than passing $resultdelete in to mysqli_affected_rows you actually want to pass the DB link (returned by mysqli_connect) which will give you the number of rows affected by the previous query

 $sqldelete="DELETE FROM tblAcqDetail WHERE ID=".$_POST['delete_id'];
 $resultdelete = $conn->query($sqldelete);

if ($conn->affected_rows > 0)  {// pass db link here

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

Upvotes: 1

Declan Deckerson
Declan Deckerson

Reputation: 108

You can use mysqli_affected_rows().

From the docs:

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

Note this is run on the connection object, not the result. So use $conn->affected_rows instead of $resultdelete->affected_rows.

Upvotes: 2

Related Questions