我正在从事有关数据映射的项目。一些细节在其他主题中进行了解释: Splice data in array 关于选择值的恢复,我正在使用JQuery将它们存储在数组中。
恢复这些值后,我想根据映射选择,完成现有表。问题是在插入数据库之前如何检测列数。
下面的错误每次都返回给我:
SQLSTATE [21S01]:插入值列表与列列表不匹配:1136列计数与第1行的值计数不匹配
查询结果:
PDO-> prepare('INSERT INTO gestation(firstName,lastName,mail)VALUES (\'test \',\'jean \',\'marc \',\'marc \')')
var choiceFields = [];
var contentChoiceFields = [];
$('#selectFields').multipleSelect({
filter: true,
onClick: function(view)
{
choiceFields.push(view.value);
var length = choiceFields.length - 1;
if (view.checked === false)
{
choiceFields = jQuery.grep(choiceFields, function(value)
{
return value !== view.value;
});
}
contentChoiceFields = choiceFields;
console.log(contentChoiceFields);
$('.selectChoiceFields').val(contentChoiceFields);
},
onCheckAll: function()
{
/*var contentSelect = "";
$('#selectFields').each(function()
{
contentSelect = $(this).val();
choiceFields.push(contentSelect);
});
contentChoiceFields = contentSelect;*/
choiceFields = []; // delete all previous values
var options = document.querySelector('#selectFields');
for(var i=0;i<options.length;i++)
{
choiceFields.push(options[i].text);
contentChoiceFields = choiceFields;
}
$('.selectChoiceFields').val(contentChoiceFields);
},
onUncheckAll: function()
{
choiceFields = [];
$('.selectChoiceFields').val('');
}
});
$(".mapping").on('click', function(event) {
var data = [];
var mapping = $(this).closest('th').text();
$('th input:checked').each(function(index,element) {
var currentCell = element.parentNode;
$('td:nth-child(' + (currentCell.cellIndex+1) + ')').each(function(i,el) {
if($(el).text().match(/^([a-zA-Z]+)(\d+)([a-zA-Z]+)$/))
{
data.push("'" + $(el).text() + "'");
}
else if($(el).text().match(/^(\d+)/))
{
var value = parseInt($(el).text());
data.push("'" + value + "'");
}
else
{
data.push("'" + $(el).text() + "'");
}
});
});
var content = data;
console.log(content);
$('.choiceData').val(content);
});
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script src="https://rawgit.com/wenzhixin/multiple-select/master/multiple-select.js"></script>
<link rel="stylesheet" href="https://rawgit.com/wenzhixin/multiple-select/master/multiple-select.css">
</head>
<body>
<div class="select-box">
<label for="selectFields"><span class="label-FieldChoice">Choice fields</span></label>
<select id="selectFields" multiple="multiple" style="display: none;">
<option value="id">id</option>
<option value="username">username</option>
<option value="username_canonical">username_canonical</option>
<option value="email">email</option>
<option value="email_canonical">email_canonical</option>
<option value="enabled">enabled</option>
<option value="salt">salt</option>
<option value="password">password</option>
<option value="last_login">last_login</option>
<option value="confirmation_token">confirmation_token</option>
<option value="password_requested_at">password_requested_at</option>
<option value="roles">roles</option>
<option value="lastName">lastName</option>
<option value="firstName">firstName</option>
</select>
</div>
<table class="table table-striped table-hover" id="tableContent">
<tbody>
<tr>
<th>lastName<input name="lastName" class="mapping" id="lastName" type="checkbox">
</th>
<th> firstName<input name="firstName" class="mapping" id="firstName" type="checkbox">
</th>
<th> âge<input name="âge" class="mapping" id="âge" type="checkbox">
</th>
<th> phoneNumber<input name="phoneNumber" class="mapping" id="phoneNumber" type="checkbox">
</th>
<th> mail<input name="mail" class="mapping" id="mail" type="checkbox">
</th>
</tr>
<tr>
<td class="mappingContent">test</td>
<td> marc</td>
<td> 21</td>
<td> 0265412369</td>
<td> marc.test@bla.fr</td>
</tr>
<tr>
<td class="mappingContent">jean</td>
<td>marc</td>
<td>28</td>
<td>0692123456</td>
<td>marc.jean@orange.fr</td>
</tr>
</tbody>
</table>
</body>
</html>
if($choiceMapping === "completion")
{
if($session->get('tableChoice') == "user")
{
$password = password_hash('test', PASSWORD_DEFAULT);
$content = $cnx->prepare("INSERT INTO ".$session->get('tableChoice')." "."(".$selectChoiceFields[0].", enabled, password, roles, lastName, firstName) VALUES (".$choiceData.",1, '".$password."', 'a:1:{i:0;s:1:\"0\";}', 'test', 'test')");
$content->execute();
$addFlash = $this->addFlash('success', "Completed recording !");
}
else
{
$content = $cnx->prepare('INSERT INTO '.$session->get('tableChoice').' ('.$selectChoiceFields[0].') VALUES ('.$choiceData.')');
$content->execute();
$addFlash = $this->addFlash('success', "Completed recording !");
}
}
else
{
$content = $cnx->prepare('Truncate '.$session->get('tableChoice'));
$content->execute();
$sql = $cnx->prepare('INSERT INTO '.$session->get('tableChoice').' ('.$selectChoiceFields[0].') VALUES (:choiceData)');
$sql->bindValue(':choiceData', $choiceData);
$sql->execute();
$addFlash = $this->addFlash('success', "Completed recording !");
}
$ selectChoiceFields的结果:
array(1){[0] =>字符串(15)“ firstName,lastMail,mail”}
$ choiceData的结果:
string(30)“'test','jean','marc','marc'”