0

My question is very similar to this question. However, unlike in this question my array does not have fieldname values. My array looks like this:

      Array
    (
        [0] => Array
            (
                [0] => Extraction and characterisation of gelatin from the skin of striped catfish (Pangasianodon hypophthalmus) and studies on its colour improvement
                [1] => EMAIL FREE FULL TEXT                          DOWNLOAD FULL TEXT
                [2] => Prabjeet Singh,            Soottawat Benjakul
                [3] => 1-9
                [4] => http://dx.doi.org/10.5897/AJB2013.13121
            )

        [1] => Array
            (
                [0] => Electrolyte ions and glutathione enzymes as stress markers in Argania spinosa subjected to drought stress and recovery
                [1] => EMAIL FREE FULL TEXT                          DOWNLOAD FULL TEXT
                [2] => Abdelghani Chakhchar,            Mouna Lamaoui,            Salama Aissam,            Abderrahim Ferradous,            Said Wahbi,            Abdelhamid EI Mousadik,            Saad Ibnsouda-Koraichi,            Abdelkarim Filali-Maltouf,            Cherkaoui El Modafar
                [3] => 10-21
                [4] => http://dx.doi.org/10.5897/AJB2016.15234
            )

        [2] => Array
            (
                [0] => Molecular detection of disease resistance genes to powdery mildew (Blumeria graminis f. sp. tritici) in wheat (Triticum aestivum) cultivars
                [1] => EMAIL FREE FULL TEXT                          DOWNLOAD FULL TEXT
                [2] => Vincent Mgoli Mwale,            Xiuli Tang,            Eric Chilembwe
                [3] => 22-31
                [4] => http://dx.doi.org/10.5897/AJB2016.15720
            )
)

I am trying to get this array data into a mysql table. The php code i am using looks like this(see below):

foreach ($newref as $key=>$values){
    $title = $values[0];
    $authors = $values[2];
    $pages = $values[3];
    $doi = $values[4];
    $query = "INSERT INTO citations (id,title,authors,pages,doi) VALUES (:title,:authors,:pages,:doi)";
        $sql=$con->prepare($query);
        $sql->execute(array(
            ':title' => $title,
            ':authors' => $authors,
            ':pages' => $pages,
            ':doi' => $doi

        ));
}

Somehow i am not able to insert data into the table. My Db connection is okay and is not reporting any errors. What can i be possibly doing wrong? What is the best/easier way of inserting this kind of data into a mysql table? I am at my wits end...Thanking you in advance

tom sawyer
  • 47
  • 1
  • 2
  • 9
  • is this how you are going to always receive this array, like `description` on `0`, `title` on `1`, and so on... – popeye Jul 04 '18 at 08:56
  • What makes you think that the connection is not reporting any errors? Your query tries to insert four given values into five declared columns – Nico Haase Jul 04 '18 at 09:16

1 Answers1

1

If id attribute in table is auto-increment, then it should be removed from query. It automatically takes next incremented value and just insert other columns/fields.

Query should be like:

$query = "INSERT INTO citations (title,authors,pages,doi) VALUES (:title,:authors,:pages,:doi)";

By the way, you are trying in right direction.

Lovepreet Singh
  • 4,792
  • 1
  • 18
  • 36