WordPress / WordPress/WordPress-Coding-Standards

Add support for `INSERT INTO ... VALUES ...` syntax in SQL queries

Open
#2,498 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
2.8k
Forks
521
Avg merge
5d 20h
Merged PRs (30d)
1

Description

Bug Description

Prepared SQL queries with INSERT INTO ... VALUES ... filled with the correct number of placeholders using the syntax commonly used for queries with IN(...) clauses (implode() + array_fill()) emit a warning.

Minimal Code Snippet

The issue happens when running this command:

phpcs --standard=WordPress -s testfile.php

... over a file containing this code:

$all_relationships = array(
	array( 1, 1 ),
	array( 1, 2 ),
	array( 1, 3 ),
);
$wpdb->query(
	$wpdb->prepare(
		sprintf(
			"INSERT INTO {$wpdb->term_relationships} (object_id, term_taxonomy_id) VALUES %s",
			implode( ',', array_fill( 0, count( $all_relationships ), '(%d,%d)' ) )
		),
		array_merge( ...$all_relationships )
	)
);

Error Code

Incorrect number of replacements passed to $wpdb->prepare().
Found 1 replacement parameters, expected 0.
(WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber)

Environment

Question Answer
PHP version 8.0.28
PHP_CodeSniffer version 3.10.3
WordPressCS version 3.1.0
PHPCSUtils version 1.0.12
PHPCSExtra version 1.2.1
WordPressCS install type Composer project local

Additional Context (optional)

Tested Against develop Branch?

  • I have verified the issue still exists in the develop branch of WordPressCS.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running phpcs --standard=WordPress -s testfile.php against the provided PHP snippet and trace WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber. Investigate how the INSERT INTO ... VALUES ... query is interpreted, then verify that the command accepts the example without the incorrect replacement-count warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
php, sql
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.