2017-03-16 5 views
0

あなたの助けが必要です。配列のデータをphp pdo経由でDBに格納する際にいくつかの問題がありました。私はアマチュアfrond-end開発者です。それはバックエンドからかなり離れているので、私を助けるあなた以外の人はいません!私はいくつかの列を持っているDBテーブルでは、 "myActions" - 入力からすべてのデータをname = "action []"という名前でこの列に行ごとに配置する必要があります。 HTMLコード内 私はそのような入力の名前を持っている:DB(php、pdo)に配列を入れるには?

<div id="field"> 
    <input autocomplete="off" class="input form-control" id="field1" name="action[]" type="text" placeholder="Type something" data-items="8"/> 
    <button id="b1" class="btn add-more" type="button">+</button> 
</div> 

のphpファイルで:

<?php 
$incident_number = $_POST['incident_number']; 
$incident_type = $_POST['incident_type']; 
$incident_subject = $_POST['incident_subject']; 
$incident_time = $_POST['incident_time']; 
$status = $_POST['status']; 
$wasdone = $_POST['action']; 
try { 
/*** connect to SQLite database ***/ 
$dbh = new PDO("sqlite:myDB2"); 
/*** echo a message saying we have connected ***/ 
//echo 'Connected to database<br />'; 
/*** The SQL SELECT statement ***/ 
$Log = date(DATE_RFC2822)." Creation".PHP_EOL; 
//echo $Log; 
$sql = "INSERT INTO myData 
(incident_number,incident_type,incident_subject,incident_time,status) values 
(:incident_number,:incident_type,:incident_subject,:incident_time,:status);" 
$query = $dbh->prepare($sql); 
$query->bindParam(':incident_number', $incident_number); 
$query->bindParam(':incident_type', $incident_type); 
$query->bindParam(':incident_subject', $incident_subject); 
$query->bindParam(':incident_time', $incident_time); 
$query->bindParam(':status', $status); 

//$query->bindParam(':Log', $Log, PDO::PARAM_STR); 
$query->execute(); 
//$query->execute(array(':NameImp'=>$NameImp)); 
// Close file db connection 
$dbh = null; 
    } 

    catch(PDOException $e) 
{ 
echo $e->getMessage(); 
} 
try { 

/*** connect to SQLite database ***/ 
$dbh = new PDO("sqlite:myDB2"); 
/*** echo a message saying we have connected ***/ 
//echo 'Connected to database<br />'; 
/*** The SQL SELECT statement ***/ 
$Log = date(DATE_RFC2822)." Creation".PHP_EOL; 
//echo $Log; 
$sql = "INSERT INTO myActions (action) values (:wasdone);"; 
foreach ($wasdone as $key => &$value) { //pass $value as a reference to the array item 
$query->bindParam($key, $value); // bind the variable to the statement 
} 
//$query->bindParam(':Log', $Log, PDO::PARAM_STR); 
$query->execute(); 
//$query->execute(array(':NameImp'=>$NameImp)); 
// Close file db connection 
$dbh = null; 
    } 

    catch(PDOException $e) 
{ 
echo $e->getMessage(); 
} 
?> 

答えて

0
あなたはで区切られた単一の文字列に配列の要素を結合し、PHPの implode()機能を、使用することができ

カスタム部分文字列。

データベースからクエリを実行するときに、後でexplode()を使用して文字列を有効な配列に戻すことができます。

注:これは迅速なデータセット内の配列が大きくなりすぎた場合、ご使用のケースのために働く可能性溶液または複雑でプロジェクトが増加している間、あなたはdata normalizationに見て、あなたのactionsアレイの追加テーブルを作成する必要があります優れたデータ管理プラクティスとしての要素

関連する問題