0

ユーザーが値を編集し、送信時にデータベースを更新できる数値の表があります。

これまでのところ、私の欠陥のあるコードは、入力内容に関係なく、送信時にすべてのフィールドを値 1 で更新しています。

送信時のコード:

    //If the confirm button has been hit:
if (isset($_POST['submit'])) {

//Create the foreach loop
foreach($_POST['classtoupdate'] as $classes){

  //Grab the POST data and turn it into integers
    $class_id = (int)$classes; 
    $totalspcs = (int)$_POST['allspaces']; 
    $totalbkgs = (int)$_POST['allbookings']; 
    $newbies = (int)$_POST['noobs'];

//Change the booking numbers:
  $newdeets = "UPDATE classes SET total_spaces = '$totalspcs', total_bookings = '$totalbkgs', free_spaces = ('$totalspcs' - '$totalbkgs'), newbies = '$newbies' WHERE class_id = '$class_id')";
  echo $newdeets;
  mysqli_query($dbc, $newdeets);
}
  mysqli_close($dbc);
  echo 'All good, yay! <a href="admin.php">Go look</a>';

}

形:

//create the form
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '" >';

      echo '<tr><td>' . $class . '</td>';
      echo'<td>' . $new_date . '</td>';
      echo '<td>' . $new_time . '</td>';
      echo '<td><input type="text" maxlength="2" class="input-mini" name="noobs[]" id="noobs[]" value="' . $newbies . '">';
      echo '<td><input type="text" maxlength="2" class="input-mini" name="allspaces[]" id="allspaces[]" value="' . $totalspaces . '">';
      echo '<td><input type="text" maxlength="2" class="input-mini" name="allbookings[]" id="allbookings[]" value="' . $bookings . '"
>';
      echo '<td>' . $freespaces . '</td>';
      echo' <input type="hidden" name="classtoupdate[]" id="classtoupdate[]" value="' . $class . '" />';
    }
    echo'</table>';
    // Make booking button
      echo '<input type="submit" name="submit" class="btn btn-large btn-primary pull-right" value="Update">';
      echo '</form>';

次の形式でランダムな値 (1 ではない) を入力すると、エコーされたクエリの結果が表示されます。

UPDATE classes SET total_spaces = '1', total_bookings = '1', free_spaces = ('1' - '1'), newbies = '1' WHERE class_id = '26')
UPDATE classes SET total_spaces = '1', total_bookings = '1', free_spaces = ('1' - '1'), newbies = '1' WHERE class_id = '16')

..テーブルの各行についても同様です。SO やマニュアルを広範囲に検索しても再現された問題が見つかりません。

POST の結果で intval()、serialize、array_map を試しました (おそらく間違っています)。それらを整数に変換するためにさまざまな foreach ループを試しましたが、まだ喜びはありません。

何かアドバイス?

4

5 に答える 5

0

これを試して:

foreach($_POST['classtoupdate'] as $index => $classes){

$totalspcs = (int)$_POST['allspaces'][$index]; 
$totalbkgs = (int)$_POST['allbookings'][$index]; 
$newbies = (int)$_POST['noobs'][$index];

$index各行の に注意してください。

于 2013-08-21T10:21:09.937 に答える
0

問題を解決するために使用できる例を作成しました。

<?php
$foo = array('1','2','3');
$bar = array('4','5','6');

// Simulates the $_POST variable
$baz = array('foo'=>$foo, 'bar'=>$bar);

// You should ensure that all entries have an equal length before iterating through!
if(count($baz['foo']) === count($baz['bar'])){
    foreach($baz['foo'] as $key=>$value){
        $x = (int)$value;
        $y = (int)$bar[$key];
        var_dump($x, $y);
    }
}       
?>

あなたが抱えている問題は、ループしているにもかかわらず、まだ配列である他のフィールドを$_POST['classtoupdate']使用していることです。$_POST['allbookings']

配列が空でない場合、配列を整数値に変換すると、常に 1 が返されます。したがって、それらから値を抽出する必要があります。

于 2013-08-21T10:22:41.607 に答える
0
$i=0;
//Create the foreach loop
foreach($_POST['classtoupdate'][$i] as $classes){

  //Grab the POST data and turn it into integers
    $class_id = (int)$classes; 
    $totalspcs = (int)$_POST['allspaces'][$i]; 
    $totalbkgs = (int)$_POST['allbookings'][$i]; 
    $newbies = (int)$_POST['noobs'][$i];

//Change the booking numbers:
  $newdeets = "UPDATE classes SET total_spaces = '$totalspcs', total_bookings = '$totalbkgs', free_spaces = ('$totalspcs' - '$totalbkgs'), newbies = '$newbies' WHERE class_id = '$class_id')";
  echo $newdeets;
  mysqli_query($dbc, $newdeets);
$i++;
}
于 2013-08-21T10:18:14.507 に答える
0

POST 変数は配列です (例: allspaces[])

したがって、配列を整数にキャストしようとしているため、この割り当ては結果として常に 1 を返します。

$totalspcs = (int)$_POST['allspaces']; 

配列を循環させて、別の方法でデータを使用する必要があります。配列が必要ない場合は、入力名の角括弧を削除してください。

于 2013-08-21T10:19:04.017 に答える
0

問題は、入力フィールドに名前を付けた方法にあります。<input type="text" maxlength="2" class="input-mini" name="noobs[]">これ[]は、データが配列としてサーバーにポストバックされることを意味し、配列を整数に型キャストしているため、どこでも値として 1 が取得されます。を失い[]ます。<input type="text" maxlength="2" class="input-mini" name="noobs">問題を修正する必要があります。

于 2013-08-21T10:21:47.233 に答える