Qual: Remove unnecessary quotes around integer values in SQL queries (#39290)

* FIX: Do not quote NULL for fk_user_author & fk_element on insert

* Qual: Remove unnecessary quotes around integer values in SQL queries

# Qual: Remove unnecessary quotes around integer values in SQL queries

Removed unnecessary single quotes around integer values in SQL queries across multiple files.
This commit is contained in:
MDW 2026-07-27 12:59:14 +02:00 committed by GitHub
parent 99ac62d86e
commit cec87d9425
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 48 additions and 45 deletions

View file

@ -335,7 +335,7 @@ class Mailing extends CommonObject
$sql .= ", evenunsubscribe = ".((int) $this->evenunsubscribe);
$sql .= ", note_public = '".$this->db->escape($this->note_public)."'";
$sql .= ", note_private = '".$this->db->escape($this->note_private)."'";
$sql .= ", fk_project = '".((int) $this->fk_project)."'";
$sql .= ", fk_project = ".((int) $this->fk_project);
$sql .= " WHERE rowid = ".(int) $this->id;
dol_syslog(__METHOD__, LOG_DEBUG);

View file

@ -1,6 +1,7 @@
<?php
/* Copyright (C) 2025 Cloned from htdocs/comm/mailing/class/mailing.class.php then modified
* Copyright (C) 2025 Jon Bendtsen <jon.bendtsen.github@jonb.dk>
* Copyright (C) 2026 MDW <mdeweerd@users.noreply.github.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -408,16 +409,16 @@ class MailingTarget extends CommonObject
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
$sql .= " SET fk_mailing = '".((int) $this->fk_mailing)."'";
$sql .= ", fk_contact = '".((int) $this->fk_contact)."'";
$sql .= " SET fk_mailing = ".((int) $this->fk_mailing);
$sql .= ", fk_contact = ".((int) $this->fk_contact);
$sql .= ", lastname = '".$this->db->escape($this->lastname)."'";
$sql .= ", firstname = '".$this->db->escape($this->firstname)."'";
$sql .= ", email = '".$this->db->escape($this->email)."'";
$sql .= ", other = '".$this->db->escape($this->other)."'";
$sql .= ", tag = '".$this->db->escape($this->tag)."'";
$sql .= ", statut = '".((int) $this->statut)."'";
$sql .= ", statut = ".((int) $this->statut);
$sql .= ", source_url = '".$this->db->escape($this->source_url)."'";
$sql .= ", source_id = '".((int) $this->source_id)."'";
$sql .= ", source_id = ".((int) $this->source_id);
$sql .= ", source_type = '".$this->db->escape($this->source_type)."'";
if ($this->date_envoi) {
$sql .= ", date_envoi = '".$this->db->idate($this->date_envoi)."'";

View file

@ -272,7 +272,7 @@ class CEmailTemplate extends CommonObject
if (is_null($this->fk_user)) {
$sql .= " NULL,";
} else {
$sql .= " '".((int) $this->fk_user)."',";
$sql .= " ".((int) $this->fk_user).",";
}
if (is_null($this->datec)) {
$sql .= " '".$this->db->idate($now)."',";
@ -284,12 +284,12 @@ class CEmailTemplate extends CommonObject
if (is_null($this->enabled)) {
$sql .= " 1,";
} else {
$sql .= " '".((int) $this->enabled)."',";
$sql .= " ".((int) $this->enabled).",";
}
if (is_null($this->active)) {
$sql .= " 1,";
} else {
$sql .= " '".((int) $this->active)."',";
$sql .= " ".((int) $this->active).",";
}
if (is_null($this->email_from)) {
$sql .= " NULL,";

View file

@ -114,9 +114,9 @@ class Comment extends CommonObject
$sql .= ") VALUES (";
$sql .= "'".$this->db->escape($this->description)."'";
$sql .= ", ".($this->datec != '' ? "'".$this->db->idate($this->datec)."'" : 'null');
$sql .= ", '".(isset($this->fk_element) ? ((int) $this->fk_element) : "null")."'";
$sql .= ", ".(isset($this->fk_element) ? ((int) $this->fk_element) : "null");
$sql .= ", '".$this->db->escape($this->element_type)."'";
$sql .= ", '".(isset($this->fk_user_author) ? ((int) $this->fk_user_author) : "null")."'";
$sql .= ", ".(isset($this->fk_user_author) ? ((int) $this->fk_user_author) : "null");
$sql .= ", ".((int) $user->id);
$sql .= ", ".(!empty($this->entity) ? ((int) $this->entity) : '1');
$sql .= ", ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");

View file

@ -477,9 +477,9 @@ interface Database
* Encrypt sensitive data in database
* Warning: This function includes the escape and add the SQL simple quotes on strings.
*
* @param string $fieldorvalue Field name or value to encrypt
* @param int $withQuotes Return string including the SQL simple quotes. This param must always be 1 (Value 0 is bugged and deprecated).
* @return string XXX(field) or XXX('value') or field or 'value'
* @param string $fieldorvalue Field name or value to encrypt
* @param int<1,1> $withQuotes Return string including the SQL simple quotes. This param must always be 1 (Value 0 is bugged and deprecated).
* @return string XXX(field) or XXX('value') or field or 'value'
*/
public function encrypt($fieldorvalue, $withQuotes = 1);

View file

@ -629,9 +629,9 @@ class DoliDBMysqli extends DoliDB
* Encrypt sensitive data in database
* Warning: This function includes the escape and add the SQL simple quotes on strings.
*
* @param string $fieldorvalue Field name or value to encrypt
* @param int $withQuotes Return string including the SQL simple quotes. This param must always be 1 (Value 0 is bugged and deprecated).
* @return string XXX(field) or XXX('value') or field or 'value'
* @param string $fieldorvalue Field name or value to encrypt
* @param int<1,1> $withQuotes Return string including the SQL simple quotes. This param must always be 1 (Value 0 is bugged and deprecated).
* @return string XXX(field) or XXX('value') or field or 'value'
*/
public function encrypt($fieldorvalue, $withQuotes = 1)
{

View file

@ -885,9 +885,9 @@ class DoliDBPgsql extends DoliDB
* Encrypt sensitive data in database
* Warning: This function includes the escape and add the SQL simple quotes on strings.
*
* @param string $fieldorvalue Field name or value to encrypt
* @param int $withQuotes Return string including the SQL simple quotes. This param must always be 1 (Value 0 is bugged and deprecated).
* @return string XXX(field) or XXX('value') or field or 'value'
* @param string $fieldorvalue Field name or value to encrypt
* @param int<1,1> $withQuotes Return string including the SQL simple quotes. This param must always be 1 (Value 0 is bugged and deprecated).
* @return string XXX(field) or XXX('value') or field or 'value'
*/
public function encrypt($fieldorvalue, $withQuotes = 1)
{

View file

@ -766,9 +766,9 @@ class DoliDBSqlite3 extends DoliDB
* Encrypt sensitive data in database
* Warning: This function includes the escape and add the SQL simple quotes on strings.
*
* @param string $fieldorvalue Field name or value to encrypt
* @param int $withQuotes Return string including the SQL simple quotes. This param must always be 1 (Value 0 is bugged and deprecated).
* @return string XXX(field) or XXX('value') or field or 'value'
* @param string $fieldorvalue Field name or value to encrypt
* @param int<1,1> $withQuotes Return string including the SQL simple quotes. This param must always be 1 (Value 0 is bugged and deprecated).
* @return string XXX(field) or XXX('value') or field or 'value'
*/
public function encrypt($fieldorvalue, $withQuotes = 1)
{

View file

@ -1912,9 +1912,9 @@ class DolibarrModules // Can not be abstract, because we need to instantiate it
if ($row[0] == 0) { // If not found
$sql = "INSERT INTO ".MAIN_DB_PREFIX."const (name, type, value, note, visible, entity)";
$sql .= " VALUES (";
$sql .= $this->db->encrypt($name);
$sql .= $this->db->encrypt($name); // Note: encrypt returns SQL expression or quoted string
$sql .= ", '".$this->db->escape($type)."'";
$sql .= ", ".(($val != '') ? $this->db->encrypt($val) : "''");
$sql .= ", ".(($val != '') ? $this->db->encrypt($val) : "''"); // Note: encrypt returns SQL expression or quoted string
$sql .= ", ".($note ? "'".$this->db->escape($note)."'" : "null");
$sql .= ", '".$this->db->escape($visible)."'";
$sql .= ", ".((int) $entity);

View file

@ -102,7 +102,7 @@ class CdavLib
if ($ouri === false) {
$sql .= ' AND a.id = '.((int) $oid);
} else {
$sql .= ' AND (a.id = '.((int) $oid)." OR ac.uuidext = '".((int) $ouri)."')";
$sql .= ' AND (a.id = '.((int) $oid)." OR ac.uuidext = ".((int) $ouri).")";
}
}

View file

@ -796,7 +796,7 @@ class EcmDirectory extends CommonObject
// Update request
$sql = "UPDATE ".MAIN_DB_PREFIX."ecm_directories SET";
$sql .= " cachenbofdoc = '".count($filelist)."'";
$sql .= " cachenbofdoc = ".count($filelist);
if (empty($all)) { // By default
$sql .= " WHERE rowid = ".((int) $this->id);
} else {

View file

@ -1,6 +1,7 @@
<?php
/* Copyright (C) 2024 Laurent Destailleur (eldy) <eldy@users.sourceforge.net>
* Copyright (C) 2024 Lionel Vessiller <lvessiller@open-dsi.fr>
* Copyright (C) 2026 MDW <mdeweerd@users.noreply.github.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -84,7 +85,7 @@ if ($action == 'updateselectbatchbywarehouse' && $permissiontowrite) {
}
$sql .= " WHERE ps.fk_product = ".((int) $product_id);
if ($warehouse_id > 0) {
$sql .= " AND fk_entrepot = '".((int) $warehouse_id)."'";
$sql .= " AND fk_entrepot = ".((int) $warehouse_id);
}
$sql .= " ORDER BY e.ref, pb.batch";

View file

@ -1,6 +1,7 @@
<?php
/* Copyright (C) 2019 Laurent Destailleur (eldy) <eldy@users.sourceforge.net>
* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
* Copyright (C) 2026 MDW <mdeweerd@users.noreply.github.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -75,7 +76,7 @@ if ($action == 'updateselectbatchbywarehouse' && $permissiontoproduce) {
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "entrepot as e on e.rowid = ps.fk_entrepot AND e.entity IN (" . getEntity('stock') . ")";
$sql .= " WHERE ps.fk_product = " .((int) $fk_product);
if ($warehouse_id > 0) {
$sql .= " AND fk_entrepot = '" . ((int) $warehouse_id) . "'";
$sql .= " AND fk_entrepot = " . ((int) $warehouse_id);
}
$sql .= " ORDER BY e.ref, pb.batch";

View file

@ -1,6 +1,6 @@
<?php
/* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
* Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
* Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
*/
/*
* This program is free software; you can redistribute it and/or modify
@ -90,10 +90,10 @@ if ($action == "existbarcode" && !empty($barcode) && $user->hasRight('stock', 'l
$sql .= " WHERE p.barcode = '".$db->escape($barcode)."'";
}
if (!empty($fk_entrepot)) {
$sql .= " AND ps.fk_entrepot = '".((int) ($fk_entrepot))."'";
$sql .= " AND ps.fk_entrepot = ".((int) ($fk_entrepot));
}
if (!empty($fk_product)) {
$sql .= " AND ps.fk_product = '".((int) ($fk_product))."'";
$sql .= " AND ps.fk_product = ".((int) ($fk_product));
}
$result = $db->query($sql);
if ($result) {

View file

@ -649,7 +649,7 @@ if (isset($search_tobuy) && dol_strlen($search_tobuy) > 0 && $search_tobuy != -1
$sql .= " AND p.tobuy = ".((int) $search_tobuy);
}
if (isset($search_stockable_product) && dol_strlen($search_stockable_product) > 0 && $search_stockable_product != -1) {
$sql .= " AND p.stockable_product = '". ((int) $search_stockable_product) . "'";
$sql .= " AND p.stockable_product = ". ((int) $search_stockable_product);
}
if (isset($search_tobatch) && dol_strlen($search_tobatch) > 0 && $search_tobatch != -1) {
$sql .= " AND p.tobatch = ".((int) $search_tobatch);

View file

@ -523,7 +523,7 @@ class Salary extends CommonObject
$sql .= ", entity";
$sql .= ") ";
$sql .= " VALUES (";
$sql .= "'".((int) $this->fk_user)."'";
$sql .= ((int) $this->fk_user);
//$sql .= ", '".$this->db->idate($this->datep)."'";
//$sql .= ", '".$this->db->idate($this->datev)."'";
$sql .= ", ".((float) $this->amount);
@ -537,7 +537,7 @@ class Salary extends CommonObject
$sql .= ", '".$this->db->escape($this->label)."'";
$sql .= ", '".$this->db->idate($this->datesp)."'";
$sql .= ", '".$this->db->idate($this->dateep)."'";
$sql .= ", '".((int) $user->id)."'";
$sql .= ", ".((int) $user->id);
$sql .= ", '".$this->db->idate($now)."'";
$sql .= ", NULL";
$sql .= ", ".((int) $conf->entity);

View file

@ -1049,7 +1049,7 @@ if (is_object($objcanvas) && $objcanvas->displayCanvasExists($canvasdisplayactio
// Define if customer/prospect or supplier status is set or not
if (GETPOST("type", 'aZ') != 'f') {
$object->client = -1;
$object->client = 0;
if (getDolGlobalString('THIRDPARTY_CUSTOMERPROSPECT_BY_DEFAULT')) {
$object->client = 3;
}

View file

@ -476,9 +476,9 @@ class CompanyBankAccount extends Account
$sql .= ",bic='".$this->db->escape($this->bic)."'";
$sql .= ",iban_prefix = '".$this->db->escape(dolEncrypt($this->iban))."'";
$sql .= ",currency_code = '".$this->db->escape($this->currency_code)."'";
$sql .= ",fk_country = '".((int) $this->fk_country)."'";
$sql .= ",state_id = '".((int) $this->state_id)."'";
$sql .= ",status = '".((int) $this->status)."'";
$sql .= ",fk_country = ".((int) $this->fk_country);
$sql .= ",state_id = ".((int) $this->state_id);
$sql .= ",status = ".((int) $this->status);
$sql .= ",domiciliation = '".$this->db->escape($this->address)."'";
$sql .= ",proprio = '".$this->db->escape($this->owner_name)."'";
$sql .= ",owner_address = '".$this->db->escape($this->owner_address)."'";

View file

@ -1261,7 +1261,7 @@ class User extends CommonObject
// les charactis for the module, permissions and sub-permission of this permission.
$sql = "SELECT module, perms, subperms";
$sql .= " FROM ".$this->db->prefix()."rights_def";
$sql .= " WHERE id = '".((int) $rid)."'";
$sql .= " WHERE id = ".((int) $rid);
$sql .= " AND entity IN (".$this->db->sanitize($entity, 0, 0, 0, 0).")";
$result = $this->db->query($sql);
@ -2349,8 +2349,8 @@ class User extends CommonObject
$sql .= ", address = '".$this->db->escape($this->address)."'";
$sql .= ", zip = '".$this->db->escape($this->zip)."'";
$sql .= ", town = '".$this->db->escape($this->town)."'";
$sql .= ", fk_state = ".((!empty($this->state_id) && $this->state_id > 0) ? "'".((int) $this->state_id)."'" : "null");
$sql .= ", fk_country = ".((!empty($this->country_id) && $this->country_id > 0) ? "'".((int) $this->country_id)."'" : "null");
$sql .= ", fk_state = ".((!empty($this->state_id) && $this->state_id > 0) ? ((int) $this->state_id) : "null");
$sql .= ", fk_country = ".((!empty($this->country_id) && $this->country_id > 0) ? ((int) $this->country_id) : "null");
$sql .= ", office_phone = '".$this->db->escape($this->office_phone)."'";
$sql .= ", office_fax = '".$this->db->escape($this->office_fax)."'";
$sql .= ", user_mobile = '".$this->db->escape($this->user_mobile)."'";
@ -2371,10 +2371,10 @@ class User extends CommonObject
$sql .= ", note_public = '".$this->db->escape($this->note_public)."'";
$sql .= ", photo = ".($this->photo ? "'".$this->db->escape($this->photo)."'" : "null");
$sql .= ", openid = ".($this->openid ? "'".$this->db->escape($this->openid)."'" : "null");
$sql .= ", fk_user = ".($this->fk_user > 0 ? "'".((int) $this->fk_user)."'" : "null");
$sql .= ", fk_user_modif = ".($this->user_modification_id > 0 ? "'".((int) $this->user_modification_id)."'" : "null");
$sql .= ", fk_user_expense_validator = ".($this->fk_user_expense_validator > 0 ? "'".((int) $this->fk_user_expense_validator)."'" : "null");
$sql .= ", fk_user_holiday_validator = ".($this->fk_user_holiday_validator > 0 ? "'".((int) $this->fk_user_holiday_validator)."'" : "null");
$sql .= ", fk_user = ".($this->fk_user > 0 ? ((int) $this->fk_user) : "null");
$sql .= ", fk_user_modif = ".($this->user_modification_id > 0 ? ((int) $this->user_modification_id) : "null");
$sql .= ", fk_user_expense_validator = ".($this->fk_user_expense_validator > 0 ? ((int) $this->fk_user_expense_validator) : "null");
$sql .= ", fk_user_holiday_validator = ".($this->fk_user_holiday_validator > 0 ? ((int) $this->fk_user_holiday_validator) : "null");
if (isset($this->thm) || $this->thm != '') {
$sql .= ", thm= ".($this->thm != '' ? "'".$this->db->escape($this->thm)."'" : "null");
}