Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7896161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:49:21+00:00 2026-06-03T07:49:21+00:00

Assume I have an array: $elements = array(‘foo’, ‘bar’, ‘tar’, ‘dar’); Then I want

  • 0

Assume I have an array:

 $elements = array('foo', 'bar', 'tar', 'dar');

Then I want to build up a DELETE IN SQL query:

 $SQL = "DELETE FROM elements
               WHERE id IN ('" . implode(',', $elements) . "')";

The problem is that the ids in the elements array aren’t quoted each individually. I.E the query looks like:

 $SQL = "DELETE FROM elements
               WHERE id IN ('foo,bar,tar,dar');

What’s the best, most elegants way to fix this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:49:22+00:00Added an answer on June 3, 2026 at 7:49 am

    Add the quotes into the implode call: (I’m assuming you meant implode)

    $SQL = 'DELETE FROM elements
               WHERE id IN ("' . implode('", "', $elements) . '")';
    

    This produces:

    DELETE FROM elements WHERE id IN ("foo", "bar", "tar", "dar")
    

    The best way to prevent against SQL injection is to make sure your elements are properly escaped.

    An easy thing to do that should work (but I haven’t tested it) is to use either array_map or array_walk, and escape every parameter, like so:

    $elements = array();
    $elements = array_map( 'mysql_real_escape_string', $elements);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets assume we have the following array var arr = new string[] {foo,bar,jar,\r,a,b,c,\r,x,y,z,\r); Also
I have an array of 1000-2000 elements which are pointers to objects. I want
I have n elements in a set U (lets assume represented by an array
An array is defined of assumed elements like I have array like String[] strArray
Assume that I have two arrays as follow: $array1 = array(1, 3, 5); $array2
Assume that we have multiple arrays of integers. You can consider each array as
I have an array of nearly sorted values 28 elements long. I need to
Let's say I have an array of elements for which a total ordering exists.
So suppose I have a JavaScript array containing a bunch of elements, but these
Let's assume I have an array of object properties I'd like to access: $properties

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.