-1

データベースからのセレクション ID、セレクション、および定義の組み合わせを表示するフォームがあります。フォームは動的です...ユーザーによっては、これらの組み合わせがいくつでもある可能性があります。3 つのペアリングの例を次に示します。

選択 ID、選択、定義

選択 ID、選択、定義

選択 ID、選択、定義

ページは問題なく表示されますが、ユーザーが選択または定義を編集しようとすると、フォームの送信時に次のエラーが表示されます (注: 行 41 は更新クエリです)。

"Notice: Undefined offset: 3 in (link to my php file) on line 41"

通知は、クエリが3つの配列の情報を読み取っていないことを示していると思います...しかし、クエリに何を入れるべきかがわからないため、正しく読み取られます。ご助力ありがとうございます。

if(isset($_POST['submit'])){    
    $selection_id = array();
    $selection = array();
    $definition = array();

foreach ($_POST as $key => $value){     
    // If array variable starts with "pd_selection_id_for_" save to $selection_id array, otherwise continue to the next array.  

    if(strpos($key, 'pd_selection_id_for_') !== false){
        $selection_id[] = mysql_real_escape_string($value);
    }else if(strpos($key, 'selection_for_') !== false){
        $selection[] = mysql_real_escape_string($value);
    }else if(strpos($key, 'definition_for_') !== false){
        $definition[] = mysql_real_escape_string($value);
    }
}

// Count one of the arrays to select the paired fields and update the database.

$total = count($definition);

for ($i=1; $i <= $total; $i++){
    // Update query for the paired selections and definitions.
    $query = mysql_query("UPDATE `pd_selections` SET `pd_selection` = '$selection[$i]', `pd_definition` = '$definition[$i]' WHERE `pd_selection_id` = '$selection_id[$i]' ") or die(mysql_error());
}

}

4

2 に答える 2

0

行 41 のマイナーな構文エラーの修正を以下に示します。

$query = mysql_query("UPDATE `pd_selections` SET `pd_selection` = '".$selection[$i]."', `pd_definition` = '".$definition[$i]."' WHERE `pd_selection_id` = '".$selection_id[$i]."'") or die(mysql_error());
于 2013-08-12T20:18:16.053 に答える