我正在从事有关数据映射的项目。实现了几项检查:
导入好的文件
餐桌选择
列表的选择
数据分页
我目前只是部分数据类型。我将这些各种数据存储在一个数组中。问题是我不完全知道如何检测选择的列数以将数据存储在数据库中 我正在使用Symfony。尝试存储数据时出现此错误:
SQLSTATE [21S01]:插入值列表与列列表不匹配:1136 列数与第1行的值数不符
/**
* @Route("/result/", name="result")
* @param Request $request
*/
function resultAction(Request $request)
{
$session = $request->getSession();
$choiceMapping = $session->get('choiceMapping');
$tableChoice = $session->get('tableChoice');
$selectChoiceFields = implode(",", $session->get('selectChoiceFields'));
$choiceData = $session->get('choiceData');
var_dump($choiceData);
$config = new Configuration();
$connectionParams = array(
'dbname' => 'mapping',
'user' => 'root',
'password' => '',
'host' => '127.0.0.1',
'driver' => 'pdo_mysql',
);
$conn = DriverManager::getConnection($connectionParams, $config);
$sm = $conn->getSchemaManager();
$tableDetails = $sm->listTableColumns($session->get('tableChoice'));
if(sizeof($selectChoiceFields[0]) === 0)
{
$addFlash = $this->addFlash('danger', 'Please choose at least one field !');
return $this->redirectToRoute('mapping');
}
$host = '127.0.0.1';
$db = 'JAgestionDonnees';
$user = 'root';
$pass = '';
$charset = 'utf8';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try
{
$cnx = new PDO($dsn, $user, $pass, $opt);
}
catch(PDOException $e)
{
die('Connection failed :'. $e->getMessage());
}
if($choiceMapping === "completion")
{
//$values = str_replace(",", ' UNION SELECT ', $choiceData);
$content = $cnx->prepare('INSERT INTO '.$session->get('tableChoice').' ('.$selectChoiceFields.') VALUES ('.$choiceData.')');
$addFlash = $this->addFlash('success', "record completed !");
}
}
else
{
$content = $cnx->prepare('Truncate '.$session->get('tableChoice'));
$content->execute();
$sql = $cnx->prepare('INSERT INTO '.$session->get('tableChoice').' ('.$selectChoiceFields.') VALUES (:choiceData)');
$sql->bindValue(':choiceData', $choiceData[0]);
$sql->execute();
$addFlash = $this->addFlash('success', "record completed !");
}
return $this->render('result.html.twig');
}