Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this
      if ($_POST["manufacturers"]) {
        $where = addWhere($where, "make IN (".htmlspecialchars(implode(",", $_POST["manufacturers"])).")");
      }
      $sql = "SELECT * FROM vehicles";
      if ($where) {
        $sql .= " WHERE $where";
      }
echo $sql;

mysql query i get is:
SELECT * FROM vehicles WHERE make IN (Tesla,Ford);

This query doesn't work because there's no quotation marks. Example ('Tesla','Ford').
For example if column value was a number, for example id, then it would work. So, how can i add quotation marks between values in parentheses?

What I have tried:

Tried changing column name and values in parentheses. If values inside of the parentheses are numbers than query works.
Posted
Updated 24-Feb-18 10:52am

Reading documentation is the first thing to do, there is 2 solutions in this page:
PHP: Strings - Manual[^]

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
$where = addWhere($where, "type IN (".htmlspecialchars(implode(",", array_map(function($string) {
return '"' . $string . '"';
  }, $data))).")");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900