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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:00:38+00:00 2026-06-15T16:00:38+00:00

I am trying to put a 2D array into a database. My code is

  • 0

I am trying to put a 2D array into a database. My code is as follows (this is PHP):

public function addspot($linearray,$data){
    $dbname=$data['dbname'];
    try {
        /* Create a connections with the supplied values */
        $pdo = new PDO("mysql:host=" . Config::read('hostname') . ";dbname=" . Config::read('database'). "", Config::read('username'), Config::read('password'), array(PDO::ATTR_PERSISTENT => true));
    } catch(PDOException $e) {
        /* If any errors echo the out and kill the script */
        return 'Database conncetion fail in assets/garage.class.php!Make sure your database information is correct';
    }

    foreach ($linearray as $lines) {
        $spot="INSERT INTO `$dbname`(`floor`, `spot`, `status`, `uid`, `type`, `time`) VALUES ('$lines[0]', '$lines[1]', '$lines[2]', '$lines[3]', '$lines[4]', CURRENT_TIMESTAMP);";
        $statement = $pdo->prepare($spot);
        if($statement->execute()){
            //silent
        } else {
            return 'Spot not added!';
        }
    }
}

The config values are being read correctly, as well as the statement to add a spot is correct.
I know this because when I run the function it correctly adds 1 “spot” but not the rest of the rows in my 2D array.

My array is as follows:

array (size=16)
0 => 
array (size=5)
  0 => string '1' (length=1)
  1 => string '1' (length=1)
  2 => string '1' (length=1)
  3 => string '0' (length=1)
  4 => string '1' (length=1)
1 => 
array (size=5)
  0 => string '1' (length=1)
  1 => string '2' (length=1)
  2 => string '1' (length=1)
  3 => string '0' (length=1)
  4 => string '1' (length=1)
 (and onwards)

My issue is that the function I have written only writes the first line (line[0]) to the database and the other ones do not get written.

Update
Output (using print_r) of statements:
Placed after the prepare

PDOStatement Object
(
[queryString] => INSERT INTO `Garage2`(`floor`, `spot`, `status`, `uid`, `type`, `time`) VALUES ('1', '1', '1', '0', '1', CURRENT_TIMESTAMP);
)
PDOStatement Object
(
[queryString] => INSERT INTO `Garage2`(`floor`, `spot`, `status`, `uid`, `type`, `time`) VALUES ('1', '2', '1', '0', '1', CURRENT_TIMESTAMP);
)

print_r($pdo->errorInfo()); output
Placed in the else (fail) part of the execute statement

Array
(
[0] => 00000
[1] => 
[2] => 
)
  • 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-15T16:00:40+00:00Added an answer on June 15, 2026 at 4:00 pm

    Finally got it.
    For those who run into this problem in the future, be sure to check that if you have a primary key on your DB, you are not using the same value twice. My first cell was my primary ID, and thus when the first and second statements both had 1 as the value, it failed.

    To find this, I added this right after my prepare()\

    $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a php function that goes to my database and
Using PHP, I'm trying to break an array up into multiple arrays based on
hi guys im trying to take the value of data(0) and put it into
I am trying to put the contents of an array into a variable to
I am trying to put a timeago in each post data inside an array
I'm trying put an if statement directly into a select field in rails, with
I'm trying to put some custom list adapters into their own classes to make
I have previously serialized an array in PHP and submitted it to a database.
I am trying to get column data from sqlLite DB and put that data
I am trying to put together an SQL database but don't really know how

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.