まず、紛らわしいスレッドタイトルで申し訳ありません。私はより良いものを思いつくことができませんでした。
フォーム要素の名前に連想配列を使用して、フォームの処理中に挿入クエリを実行しやすくしています。何かのようなもの:
<input type="text" name="v[fname]" />
<select name="v[location]">
<option val="1">ABC</option>
<option val="2">DEF</option>
</select>
<textarea name="v[comments]"></textarea>
私が簡単にできるように:
$v = $_POST[v];
// single line execution for insert
"INSERT INTO ".$tableName." ( ". implode( ',', array_keys( $v) ) .") VALUES( '". implode( "','", $v ) . "')"
時々、私が扱うフォームには<input type="file" />
要素が含まれている必要があります。次の方法があるかどうか疑問に思っていました:
- ファイルがアップロードされているかどうかを検出する
- はいの場合は、ファイルのパスを内部に保存して
$v
、上記と同じ方法でその配列を使用できるようにします
だから、一言で言えば、私はこのようなものを探しています:
if(isset($_POST['add'])) // when submit button is clicked
{
$v = $_POST['v']; // store other element values
if(condition to check if a file is being uploaded through the form)
{
$path = 'get the path where it will be uploaded'; //This part I can handle. What I'm having trouble with is finding a way to get into **this** if condition
$v['path'] = $path; // store the path inside $v
}
//proceed with the insert statement as usual
}