首页 > 解决方案 > 请问如何使用pdo将包含138个数组(如下所示)的php数组发布到mysql?

问题描述

我的数据库代码是:

CREATE TABLE IF NOT EXISTS symptoms_list (
id int(11) NOT NULL,
available_names varchar(255) NOT NULL

PRIMARY KEY (id)
);

我的PHP代码是:

<?php

require 'config.php';

$available_names = array (
    array(‘Abdominal pain’, ‘Stomach ache’, ’Stomach trouble’, ‘Painful tommy’, ‘Tommy ache’, ‘Tommy pain’, ‘Belly ache’, ‘Belly pain’, ‘Painful tommy’),
    array(‘RUQ abdominal pain’, ‘RUQ pain’, ’Right upper quadrant abdominal pain’, ‘Right upper quadrant pain’, ‘Right hypochondriac pain’ ),
    array(‘Epigastric pain’, ’mid-upper abdominal pain’),
    array(‘LUQ abdominal pain’, ‘LUQ pain’, ‘Left hypochondriac pain’, ‘Lt upper quadrant abdominal pain’),
    array(‘Right Para umbilical pain’, ‘Rt para umbilical pain’, ‘Rt para umbilical pain’, ‘Right para umbilical abdominal pain’, ‘Pain right side of the belly button’),
    array(‘Umbilical pain’, ‘Periumbilical abdominal pain’, ‘umbilical pain’, ‘Belly button pain’, ‘Umbilical ache’),
    array(‘RLQ abdominal pain’, ‘Rt Lower quadrant abdominal pain’, ‘Right lower quadrant pain’, ‘Right lower quadrant abdominal pain’, ‘RLQ pain’),
    array(‘suprapubic pain’, ‘Suprapubic pain’, ‘Pain below the umbilicus’, ‘Pain below the navel’, ‘Pain below the belly button’),
    array(‘LLQ abdominal pain’, ‘Lt lower quadrant abdominal pain’, ‘LLQ pain’, ‘LLQ abdominal discomfort’, ‘Left lower quadrant abdominal pain’, ‘Left lower quadrant pain’),
    array(‘Back pain’, ‘Pain in the back’, ‘Back ache’, ‘Back discomfort’, ‘Pain at the back’),
    array(‘Fatigue’, ‘Easy fatiguability’, ’Lassitude’, ’Tiredness’, ’General weakness’, ’Exhaustion’),
    array(‘Chest pain’, ‘Painful chest’, ‘Chest discomfort’, ‘Chest heaviness’),
    array(‘Otalgia’, ‘Ear ache’, ’Ear pain’, ‘Ear discomfort’, ‘Painful ear’),
    array(‘Otorrhea’, ‘Discharging ear’, ‘Ear discharge’)
    );
    // STORE ARRAY
    $sql = $db->prepare("INSERT INTO symptoms_list (id, available_names) VALUES (:id, :available_names)");
    // CONNECT TO DATABASE
    try {
      $stmt = $pdo->prepare($sql);
    // Start Transaction
    $stmt =  $db->beginTransaction();
    $array = json_decode($data, true);
    // Insert each record
    foreach($data as $insertRow){
    
       // now loop through each inner array to match binded values
       foreach($insertRow as $column => $value){
          $stmt->bindValue(":{$column}", $value);
      }
    }
    // Execute statement to add to transaction
    $stmt->execute();
    
    // Clear statement for next record (not necessary, but good practice)
    $stmt = null;
    }
    // Commit all inserts
    $db->commit();
    ?> 

每次我尝试将二维数组发布到 mysql 时,我都会收到错误消息:

“解析错误:语法错误,意外 ';',在第 4 行的 C:\xampp2\htdocs\public\symptoms7.php 中期待 ')'”

标签: php

解决方案


推荐阅读