简体   繁体   中英

How can I store data from an array into the mySQL database?

I want to store data from an array into my database.

This is how my Array looks when I do print_r($animals); :

Array
(
    [0] => Array
        (
            [animal] => Cat
            [name] => Tom
        )

    [1] => Array
        (
            [animal] => Dog
            [name] => Bob
        )

    [2] => Array
        (
            [animal] => Bird
            [name] => Sam
        )
    [3] => ....

This is how I try to store the data, but for some reason it doesn't work, nothing is stored:

$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO animals (animal,name) values(?,?) ";
$q = $pdo->prepare($sql);
foreach($animals as $row) {
   $q->execute(array(
      $row['animal'],
      $row['name'],));
}
Database::disconnect();

If I write the following for example the storing works, but only the first entry is stored (Cat and Tom)

foreach($animals as $row) {             
    $name = $row['name'];
    $animal = $row['animal'];
}

$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO animals (animal,name) values(?,?) ";
$q = $pdo->prepare($sql);
$q->execute(array($animal,$name));
Database::disconnect();      

Your first attempt made more sense. Remember you only need to prepare a parameterised statement once, then you can run it 1000 times if you like as long as you replace the parametes each time.

Also as you are setting PDO to emit Exceptions on error you should code this in a Try/Catch block

    try {
        $pdo = Database::connect();
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "INSERT INTO animals (animal,name) values(?,?) ";

        $q = $pdo->prepare($sql);

        foreach($animals as $row) {

            $q->execute(array($row['animal'], $row['name']));
        }
    }
    catch (PDOException $pe) {
        echo $pe->getMessage();
    }
    catch (Exception $e ) {
        echo $e->getMessage();
    }
    Database::disconnect();

Modify your code like this, it will work.

$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
foreach($animals as $row) {
   $sql = "INSERT INTO animals (animal,name) values($row['animal'], $row['name']) ";
   $q = $pdo->prepare($sql);
   $q->execute();
}
Database::disconnect();

I can't explain it well, but I know the CodeIgniter Framework used PDO this way for SQL execution.

The first one you got syntax error extra comma:

foreach($animals as $row) {
   $q->execute(array(
      $row['animal'],
      $row['name'],));//extra comma
}

The second one you got the loop at the top overwriting the variables. This means it will insert the last row.

So remove the comma in that for each or use for loop:

for($i=0; $i< count($animals); $i++) {
   $q->execute(array(
      $animals[$i]['animal'],
      $animals[$i]['name']));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM