Make sure sql last insert id always returns integer

This commit is contained in:
Bartek Fabiszewski 2019-12-28 18:50:29 +01:00
parent f1873d0410
commit 3e8e6b08c5
3 changed files with 16 additions and 20 deletions

View File

@ -114,7 +114,7 @@ require_once(ROOT_DIR . "/helpers/upload.php");
$positionId = false;
if (is_numeric($lat) && is_numeric($lon) && is_numeric($timestamp) && is_numeric($userId) && is_numeric($trackId)) {
$track = new uTrack($trackId);
if ($track->isValid && $track->userId == $userId) {
if ($track->isValid && $track->userId === $userId) {
try {
$table = self::db()->table('positions');
$query = "INSERT INTO $table
@ -125,7 +125,7 @@ require_once(ROOT_DIR . "/helpers/upload.php");
$params = [ $userId, $trackId,
$timestamp, $lat, $lon, $altitude, $speed, $bearing, $accuracy, $provider, $comment, $image ];
$stmt->execute($params);
$positionId = self::db()->lastInsertId("${table}_id_seq");
$positionId = (int) self::db()->lastInsertId("${table}_id_seq");
} catch (PDOException $e) {
// TODO: handle error
syslog(LOG_ERR, $e->getMessage());
@ -299,7 +299,7 @@ require_once(ROOT_DIR . "/helpers/upload.php");
* @return bool True if success, false otherwise
*/
public static function removeImages($userId, $trackId = NULL) {
if (($positions = uPosition::getAllWithImage($userId, $trackId)) !== false) {
if (($positions = self::getAllWithImage($userId, $trackId)) !== false) {
/** @var uUpload $position */
foreach ($positions as $position) {
try {

View File

@ -84,7 +84,7 @@
$stmt = self::db()->prepare($query);
$params = [ $userId, $name, $comment ];
$stmt->execute($params);
$trackId = self::db()->lastInsertId("${table}_id_seq");
$trackId = (int) self::db()->lastInsertId("${table}_id_seq");
} catch (PDOException $e) {
// TODO: handle exception
syslog(LOG_ERR, $e->getMessage());
@ -158,7 +158,7 @@
$ret = false;
if (empty($name)) { $name = $this->name; }
if (is_null($comment)) { $comment = $this->comment; }
if ($comment == "") { $comment = NULL; }
if ($comment === "") { $comment = NULL; }
if ($this->isValid) {
try {
$query = "UPDATE " . self::db()->table('tracks') . " SET name = ?, comment = ? WHERE id = ?";
@ -184,21 +184,17 @@
*/
public static function deleteAll($userId) {
$ret = false;
if (!empty($userId)) {
// remove all positions
if (uPosition::deleteAll($userId) === true) {
// remove all tracks
try {
$query = "DELETE FROM " . self::db()->table('tracks') . " WHERE user_id = ?";
$stmt = self::db()->prepare($query);
$stmt->execute([ $userId ]);
$ret = true;
} catch (PDOException $e) {
// TODO: handle exception
syslog(LOG_ERR, $e->getMessage());
}
if (!empty($userId) && uPosition::deleteAll($userId) === true) {
// remove all tracks
try {
$query = "DELETE FROM " . self::db()->table('tracks') . " WHERE user_id = ?";
$stmt = self::db()->prepare($query);
$stmt->execute([ $userId ]);
$ret = true;
} catch (PDOException $e) {
// TODO: handle exception
syslog(LOG_ERR, $e->getMessage());
}
}
return $ret;
}

View File

@ -84,7 +84,7 @@
$query = "INSERT INTO $table (login, password) VALUES (?, ?)";
$stmt = self::db()->prepare($query);
$stmt->execute([ $login, $hash ]);
$userid = self::db()->lastInsertId("${table}_id_seq");
$userid = (int) self::db()->lastInsertId("${table}_id_seq");
} catch (PDOException $e) {
// TODO: handle exception
syslog(LOG_ERR, $e->getMessage());