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 8695873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:08:43+00:00 2026-06-13T01:08:43+00:00

I need to update an array into mysql table. this is my array [cat2]

  • 0

I need to update an array into mysql table.

this is my array

[cat2] => Array
                (
                    [0] => 34
                    [1] => 48
                    [2] => 49
                    [3] => 46
                )

and I want my output as 34,48,49,46 to move into a table called shops and column called category. This is code I written, $shop[‘cat2’] contains the array i posted above. Please help me. This code is keep only 34 in all fields of column category

foreach ($shop['cat2'] as $k => $v) {
    $query = "UPDATE shops SET categories2=$v";
    mysql_query($query);
    }
  • 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-13T01:08:44+00:00Added an answer on June 13, 2026 at 1:08 am

    In order to apply each of these array elements to one row of your category column, you will need to first fetch the a unique id from the shops table, and loop over it, using that unique identifier value in the update query’s WHERE clause:

    We’ll call mysql_fetch_assoc() inside the foreach() loop to pull one row per one array element. This assumes you have a column shops.id which is the unique identifier per shop.

    // If `id` is not the unique id per shop, substitute the correct column
    $rowids = mysql_query("SELECT id FROM shops ORDER BY id");
    if  ($rowids) {
      // Loop over your array, and do one update per array elem
      foreach ($cat2 as $cat) {
         $row = mysql_fetch_assoc($rowids);
         $id = row['id'];
         // Your values are ints, so no need to quote and escape.
         // If you do use other string values, be sure to mysql_real_escape_string() them and single-quote
         $upd = mysql_query("UPDATE shops SET category = $cat WHERE id = $id");
         // Report error on this iteration
         if (!$upd)  {
            echo "error updating $id" . mysql_error();
         }
      }
    }
    else {
       echo "Couldn't get row ids. " . mysql_error(); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need your help. I need to update the array data in the mysql table.
I need to insert array data into MySQL DB. My code is provided below.
I need to build an update query from an associative array from a POST
I need to update the Notes field in my table tblMain by concatenating the
I have loaded an associative array of records from a MySQL database table. The
I am working in PHP/MySQL. I have a table in my database called hourly
So, basically, I have a MySQL table called 'topics' and another one called 'replies',
I need to get the last inserted id of table that have multi-column primary
I have an array with 30000 plus entries that need to go into a
Following situation: I stored a checkbox array with implode in a mysql table field.

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.