私はCodeigniterフレームワークを使用していますが、入力フィールドを持つフォームがあります。私はデータベースへの入力フィールドの値を挿入するとき、私はこのコードを使用しPHPのforeachからデータベースへのデータの挿入
function add($tableName)
{
$fieldsData = $this->db->field_data($tableName); // to get all fields name of the table like (id,title,post ..)
foreach ($fieldsData as $key => $field)
{
$datacc = array( $field->name => $this->input->post($field->name));
echo $this->input->post($field->name) ; // when I submit the form I get all that data I need like (mySubjet, MyPost...)
} // but when I insert the data it insert just the last filed like (cat_id = 3) only !// and the other fields are empty ..
$this->db->insert($tableName, $datacc);
}
ので、私は、最後のデータベースに挿入された値が、ときに私は次のようにforeach
ループ内のクエリ行を入れてもらう:
function add($tableName)
{
$fieldsData = $this->db->field_data($tableName);
$datacc = "";
foreach ($fieldsData as $key => $field)
{
$datacc = array( $field->name => $this->input->post($field->name));
$this->db->insert($tableName, $datacc); // inside the loop !
}
}
を
15レコード/行(表のフィールド数)を挿入し、すべての行に一意の値を挿入します。最初の行にTITLE
フィールドを挿入し、次の行のPOST
フィールドと3番目のフィールドにdateOfpost
フィールドを挿入します。
WaW! ..私は私の目を信じることができません.. 最後にそれは働いている! Gregmacありがとうございます。だから私はこのコードに疲れました。私は3日間、何百回も、12時間/日に挑戦しました! あなたの助けに本当に感謝しています。私はそれを忘れることはありません! :) –