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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:17:56+00:00 2026-05-26T12:17:56+00:00

How can insert values on input in database mysql by json_encode and foreach, this

  • 0

How can insert values on input in database mysql by json_encode and foreach, this html code and php code don’t work true and done insert value incomplete in database table as: “[” Or “1”

DEMO: http://codepad.viper-7.com/LAOJXC

I want they as in database table row:

           Column data_1           | Column static
Row1: ["1111111111", "2222222222"] | 12
Row2: ["3333333333", "4444444444"] | 34
Row1: ["5555555555", "6666666666"] | 56

<input name="data_1[]" value="1111111111">
<input name="data_1[]" value="2222222222">
<input name="static[]" value="12">

<input name="data_1[]" value="3333333333">
<input name="data_1[]" value="4444444444">
<input name="static[]" value="34">

<input name="data_1[]" value="5555555555">
<input name="data_1[]" value="6666666666">
<input name="static[]" value="56">

$data = array();
$data_1 = json_encode($_POST[data_1]);
$static = $_POST[static];
foreach($static as $idx=>$val){
    $data[] = array(
              'data_1' => $data_1[$idx],
              'static' => $static[$idx]
                   )
}

$this->db->insert_batch('MyTable', $data);
  • 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-05-26T12:17:56+00:00Added an answer on May 26, 2026 at 12:17 pm

    The reason why your code only inserts single characters to the database is because you convert the array $_POST['data_1'] to a string using json_encode() and then try to access this string as an array later.

    When you access a string with the square brackets notation (in your code: $data_1[$idx]), PHP interprets this as a character-access in the string, resulting in only a single character.

    From the PHP manual:

    Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose.

    Look at this working example:

    <form method="post">
    <input name="data_1[]" value="1111111111">
    <input name="data_1[]" value="2222222222">
    <input name="static[]" value="12">
    
    <input name="data_1[]" value="3333333333">
    <input name="data_1[]" value="4444444444">
    <input name="static[]" value="34">
    
    <input name="data_1[]" value="5555555555">
    <input name="data_1[]" value="6666666666">
    <input name="static[]" value="56">
    <input type="submit">
    </form>
    
    <?php
    $data = array();
    $data_1 = $_POST['data_1'];
    $static = $_POST["static"];
    foreach($static as $idx=>$val){
        $data[] = array(
                  'data_1' => json_encode(Array($data_1[$idx*2],$data_1[$idx*2+1])),
                  'static' => $static[$idx]
                       );
    }
    echo "<pre>";
    print_r($data);
    ?>
    

    DEMO http://codepad.viper-7.com/ca51ZB

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

Sidebar

Related Questions

My Code : <?php function dbAdd($first_name , $image) { //mysql connect database code... mysql_query("INSERT
How can I insert values from a comma-separated input parameter with a stored procedure?
This is my code so far: The HTML: <form> <label>First Name</label> <input type=text class=first
Is there a way that I can insert values into a VB.NET Dictionary when
How i can insert data into more than one table of an Access database
I have a question about inserting values into an input field from my MYSQL
This is what I want: foreach($_POST['something'] as $something){ foreach($_POST['example'] as $example){ $query = mysql_query(INSERT
As I know there is a way to input data into a mysql database
I want to make a 2 field input form to my MySQL database. I
I tested source code from this tutorial http://query7.com/php-jquery-todo-list-part-1 and just deploy their source taken

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.