18.PHPでユーザの更新
 1)更新ユーザを選択するためのページ(userUpDateMenu.php)
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>データベースの更新</title>
</head>
<body>
データベースの更新<br>
<?php
$conn=mysqli_connect('localhost','root','******') or exit("MySQLへ接続できません。");
mysqli_select_db($conn,'userid_db') or exit("データベース名が間違っています。");
$sql="SELECT * FROM userid_tbl;";
$result=mysqli_query($conn,$sql) or exit("データの抽出に失敗しました。");
?>
<form method="post" action="userUpDate.php">
<table border="1">
<tr>
<th>更新の有無</th><th>userName</th><th>passWd</th>
<?php
while($userid=mysqli_fetch_array($result,MYSQL_ASSOC)){
?>
<tr>
<td><input type="checkbox" name="checkId[]" value="<?= $userid['id'] ?>"></td>
<input type="hidden" name="fId[]" value="<?= $userid['id'] ?>">
<td><input type="text" name="fUserName[]" value="<?= $userid['userName'] ?>"></td>
<td><input type="text" name="fPassWd[]" value="<?= $userid['passWd'] ?>"></td>
</tr>
<?php
}
mysqli_close($conn);
?>
<br>
</table>
<input type="submit" value="更新">
</form>
</body>
</html>
 2)ユーザを更新するページ(userUpDate.php)
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>データベースの更新</title>
</head>
<body>
データの更新<br>
<?php
$conn=mysqli_connect('localhost','root','******') or exit("MySQLへ接続できません。");
mysqli_select_db($conn,'userid_db') or exit("データベース名が間違っています。");
if(isset($_POST['checkId'])){
for($i=0;$i<count($_POST['checkId']);$i++){
for($j=0;$j<count($_POST['fId']);$j++){
if($_POST['checkId'][$i]==$_POST['fId'][$j]){
$sql="update userid_tbl set userName='{$_POST['fUserName'][$j]}',
passWd='{$_POST['fPassWd'][$j]}'
WHERE id={$_POST['fId'][$j]};";
$result=mysqli_query($conn,$sql) or exit("データの更新に失敗しました。");①
}
}
}
$sql="SELECT * FROM userid_tbl;";
$result=mysqli_query($conn,$sql) or exit("データの抽出に失敗しました。");
?>
以下のように更新しました。<br>
(もう一度更新できます。)<br>
<form method="post" action="userUpDate.php">
<table border="1">
<tr>
<th>更新の有無</th><th>userName</th><th>passWd</th>
<?php
while($userid=mysqli_fetch_array($result,MYSQL_ASSOC)){
?>
<tr>
<td><input type="checkbox" name="checkId[]" value="<?= $userid['id'] ?>"></td>
<input type="hidden" name="fId[]" value="<?= $userid['id'] ?>">
<td><input type="text" name="fUserName[]" value="<?= $userid['userName'] ?>"></td>
<td><input type="text" name="fPassWd[]" value="<?= $userid['passWd'] ?>"></td>
</tr>
<?php
}
}
else{
?>
更新するデータが選択されていません。<br>
もう一度やり直してください。<br>
<form method="post" action="userUpDate.php">
<table border="1">
<tr>
<th>更新の有無</th><th>userName</th><th>passWd</th>
<?php
$sql="SELECT * FROM userid_tbl;";
$result=mysqli_query($conn,$sql) or exit("データの抽出に失敗しました。<br>\n");
while($userid=mysqli_fetch_array($result,MYSQL_ASSOC)){
?>
<tr>
<td><input type="checkbox" name="checkId[]" value="<?= $userid['id'] ?>"></td>
<input type="hidden" name="fId[]" value="<?= $userid['id'] ?>">
<td><input type="text" name="fUserName[]" value="<?= $userid['userName'] ?>"></td>
<td><input type="text" name="fPassWd[]" value="<?= $userid['passWd'] ?>"></td>
</tr>
<?php
}
}
?>
</table>
<input type="submit" value="更新">
</form>
<?php
mysqli_close($conn);
?>
</body>
</html>
<課題1>
    2)のプログラム中の「①」で「for文」の中でSQL文を作成しクエリーを実行していますが、「for文」で更新するすべての行を更新するSQL文を作成し、
   クエリーの実行は、「for文」の外で一回のみでできるようにプログラムを修正してください。