Click to See Complete Forum and Search --> : $mysqli->affected_rows = 0 ?


pkng
03-27-2010, 06:12 AM
Hi,

When I use $mysqli->affected_rows after a delete, the result is 0, despite that one row has been ereased!? The strange thing is that this only happens when using Firefox (3.6.2), but not with Internet Explorer!?

Here is a part of the code:
$html .= "<p>Query={$query}</p><p>Rows affected: {$mysqli->affected_rows}</p>";

Anyone knows more about this and could help me?

Thanks!

NogDog
03-27-2010, 02:05 PM
The browser is not going to (directly) affect how the server-side script and DB function, so my guess would be that something about the HTML form you use to fire off the action is being processed differently on IE, and as a result some form value is not received by the server-side script and nothing, in fact, is being deleted.

pkng
03-27-2010, 02:16 PM
Thanks for the reply!

I'm not using a form. I just call another page together with an id of the row to be deleted like delete.php?id=1212. And in the delete.php page I use this code:

$idLarare = isset($_GET['idLarare']) ? $_GET['idLarare'] : '';

and

$query = <<<EOD
DELETE FROM Larare
WHERE idLarare = {$idLarare}
LIMIT 1;
EOD;

$res = $mysqli->query($query)
or die("Could not query database");

$html = "<h2>Radera lärare</h2>";
$html .= "<p>Query={$query}</p><p>Rows affected: {$mysqli->affected_rows}</p>";
$html .= "<p><a href='PSelectVLarare_09.php'>Visa alla lärare</a></p>";

and at the end

echo $html;

Strange because rows are getting deleted! Any other ideas? Thansk!

NogDog
03-27-2010, 04:14 PM
One thing you might try, with the added benefit of preventing SQL injection attacks, would be to ensure the value is treated as an integer:

$idLarare = isset($_GET['idLarare']) ? (int) $_GET['idLarare'] : '';

For debugging purposes you might also want to echo the value to see if it's properly set, maybe also echoing the $query string?

pkng
03-27-2010, 04:45 PM
Hi again and thanks for reply. I'm using is_numeric, but I didn't copy that into the example. But I still wonder why I get 0 when using Firefox? No clue?

if(!is_numeric($idLarare)) {
die("idLarare måste vara ett integer. Välj vilken lärare du vill radera och försök igen.");
}

Thanks!