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

  • SEARCH
  • Home
  • 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 8732201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:23:38+00:00 2026-06-13T09:23:38+00:00

I am building a SQL query using the following foreach loop: $sql = UPDATE

  • 0

I am building a SQL query using the following foreach loop:

$sql =  "UPDATE table01 SET ";  
        foreach(array_combine($values, $variables) as $value=>$variable)
        {
        $sql .="$value = $variable,";
        }
        $sql = rtrim($sql,','); 
        $sql .=" WHERE id = '$id'";

$values is an array of field titles to be updated (column headers in db) while $variables is an array of variables containing the data.

An example of the query built by such a loop is here:

UPDATE table01
SET sv_21 = 123, sv_22 = 123, sv_23 = 2011, sv_I8 = 1 sachet every day, sv_I9 = 3
WHERE id = '001'

SQL of recognises “every day” as syntax and it breaks the update command.

How can I enter this data in ? In reality these queries are usually much larger and several strings cause syntax errors.

Thanks for your consideration

If it is relevent, here is the PDO I am using to execute the UPDATE:

try 
{
$pdo = new PDO('mysql:host=localhost; dbname=tables', $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $pdo->prepare($sql);
$stmt->execute();         




# Affected Rows?
  echo $stmt->rowCount(); // 1
}
  • 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-13T09:23:39+00:00Added an answer on June 13, 2026 at 9:23 am

    Whenever you enter text data (as opposed to numerical data) into an SQL field, you need to put it in quotes. For example:

    UPDATE table01
    SET sv_21 = 123, sv_22 = 123, sv_23 = 2011, sv_I8 = '1 sachet every day', sv_I9 = 3
    WHERE id = '001'
    

    Some types of SQL, such as MySQL, will let you put any data in quotes, so you should update your PHP to work like this:

    $sql =  "UPDATE table01 SET ";  
            foreach(array_combine($values, $variables) as $value=>$variable)
            {
            $sql .="$value = '$variable',"; // output: sv_I8 = '1 sachet every day',
            }
            $sql = rtrim($sql,','); 
            $sql .=" WHERE id = '$id'";
    

    Before you do this, however, you should know that this is a dangerous way to use your database. If a user were to put an apostrophe (') into their data, then they could add their own SQL command into your database. This is called SQL injection, and its one of the leading ways that websites get hacked.

    A safer way to update your database would be to write the full SQL statement, with placeholders, and explicitly define the value for each placeholder. You can learn more about this method here:
    http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using a c# asp.net page, building a SQL query string to run against an
I'm building up a query using LINQ to SQL and C# and when I
I'm building a LINQ query using a loop that appends predicates using an array:
In sql 2005, instead of building a query from dateparts year, month and date,
So I'm building an App using the Entity Framework on top of SQL Compact
I am stuck trying to build the following SQL query in JPA (still new
i am building some basic sql query and have come unstuck on a 'group
I am writing a SQL query creator using some parameters. In Java, it's very
I'm building an abstract gem. i need a sql query that looks like this
I have a controller that is building a query from Linq to Sql to

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.