0

テーブルからすべてのユーザーを出力するテーブルがあります。各ユーザーの横には、選択ボックスと更新ボタンがあります。私が抱えている問題は、スクリプトが最後の選択ボックスからのみ読み取ることです。選択ボックスを動的にして、選択ボックスがどの行からのものであるかをスクリプトが認識できるようにするにはどうすればよいですか?

選択ボックスのコード:

<select name="level" >
<option value="basic" SELECTED>Basic</option>
<option value="premium">Premium</option>
<option value="platinum">Platinum</option>
</select>

<button type="submit" name="update" value="<?php echo $row['id'] ?>">Update</button>

送信ボタンが押されたときに有効になる別のスクリプトのコード:

if ( isset( $_POST['update'] ) ) {
 //Get the ID of the account that will be updated
 $userID = $_POST['update'];
 //Get the level that the admin wishes to change to
 $level= $_POST['level'];

//Connect to the DB
mysql_connect("localhost", "xxxx", "xxxx")or die("Cannot connect to database");
mysql_select_db("xxxx")or die("Database cannot be selected or it doesnt exist");

//Updates the users access level
mysql_query("UPDATE users SET level='$level' WHERE id='$userID' ");
4

3 に答える 3

0

HTMLを見るだけで、選択ボックスの値はすべて同じ名前であるため、上書きされています。

最も簡単な解決策は、選択ボックスの名前を配列に変換することです。

<select name="level[<?php echo $row['id']; ?>]" >

いいえ、サーバー側で次のようにアクセスできます。

$_POST['level'][$userID]

また、SQL インジェクションの問題を回避するために、バインドされた変数を使用して PDO / mysqli および準備済みステートメントに切り替える必要があります。

于 2012-11-28T14:44:52.317 に答える
0

これには jquery を使用できます。これを試して、

 $(document).ready(function(){
  var values = [];
  $('select[name=level]').each(function() {
    values[] = $(this).val(); 
  });

  $('button[name=update]').val(values);

 });

最後の行のみを読み取る理由は$row['id']、反復された結果セットの最後の行のみが含まれているためです。

于 2012-11-28T14:46:40.290 に答える
0

これを使って:

<input type="hidden" name="userid[]" value="#THEUSERID"/>
<select name="level[]" >
<option value="basic" SELECTED>Basic</option>
<option value="premium">Premium</option>
<option value="platinum">Platinum</option>
</select>

if ( isset( $_POST['update'] ) ) {
    foreach($_POST['userid'] as $k=>$userID){

 //Get the level that the admin wishes to change to
        $level= $_POST['level'][$k];

//Connect to the DB
        mysql_connect("localhost", "xxxx", "xxxx")or die("Cannot connect to database");
        mysql_select_db("xxxx")or die("Database cannot be selected or it doesnt exist");

//Updates the users access level
        mysql_query("UPDATE users SET level='$level' WHERE id='$userID' ");
    }
}
于 2012-11-28T14:47:02.503 に答える