13)商品メーカ名を更新できるようにする。
  以下の表(表1)を表示させてメーカを選択して更新できるようにしてみる。
  表1
 
メーカ名の更新
ID 商品コード 商品名 単価 在庫数 メーカ名
2 101 万年筆(赤) 50 25
3 102 色鉛筆(黄) 70 100
4 103 色鉛筆(黒) 50 30
5 104 ボールペン(赤) 80 50
6 105 色鉛筆(青) 100 20

  ①上記表を表示するプログラム
   ファイル名:figuer1UpdateMenu.php
<!doctype html>
<html>
<head>
<meta charset="utf-8"> <title>メーカ名の更新</title>
</head>
<body>
<?php
echo "メーカ名の更新<br>\n";
$conn=mysqli_connect('localhost','******','******');
mysqli_select_db($conn,'product_db');
$sql1="SELECT * FROM maker_tbl order by id asc;";
$sql2="*************************************************************";
if($result=mysqli_query($conn,$sql1)){
$i=0;
while($row=mysqli_fetch_array($result,MYSQL_ASSOC)){
$id[$i]=$row['id'];
$maker[$i]=$row['makerName'];
$i++;
}
}
else{
echo "クエリが実行できませんでした。\n";
mysqli_close($conn);
exit();
}
if($result=mysqli_query($conn,$sql2)){
if(mysqli_num_rows($result)!=0){
echo "<form name=\"frm\" method=\"post\" action=\"figuer1Update.php\">\n";
echo "<table border=\"1\">\n
<tr><td>ID</td>
<td>商品コード</td>
<td>商品名</td>
<td>単価</td>
<td>在庫数</td>
<td>メーカ名</td></tr>\n";
while($row=mysqli_fetch_array($result,MYSQL_ASSOC)){
echo "<tr>\n";
echo "<td style=\"text-align:right;\">{$row['id']}</td>\n
<td style=\"text-align:right;\">{$row['productId']}</td>\n
<td>{$row['productName']}</td>\n
<td style=\"text-align:right;\">{$row['unitPrice']}</td>\n
<td style=\"text-align:right;\">{$row['stockNumber']}</td>\n";
echo "<td><select name=\"makerN[]\">\n";
$i=0;
while($i<count($maker)){
if($row['makerId']==$id[$i])
echo "<option value=\"{$row['productId']},{$row['makerId']},{$id[$i]},{$maker[$i]}\" selected>{$maker[$i]}</option>\n";
else
echo "<option value=\"{$row['productId']},{$row['makerId']},{$id[$i]},{$maker[$i]}\">{$maker[$i]}</option>\n";
$i++;
}
echo "</select></td></tr>\n";
}
echo "</table>\n";
echo "<input type=\"submit\" value=\"更新\">";
echo "</form>\n";
}
}
else{
echo "条件に合ったデータは存在しません<br>\n";
}
mysqli_close($conn);
?>
</body>
</html>
  ②選択したメーカ名で「puroductmaker_tbl」の「makerId」を変更する。
   ファイル名:figuer1Update.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>表1のデータアップデート</title>
</head>
<body>
<?php
if(isset($_POST['makerN'])){
$conn=mysqli_connect('localhost','******','******');
mysqli_select_db($conn,'product_db');
$sql="";
for($i=0;$i<count($_POST['makerN']);$i++){
$makerN=explode(",",$_POST['makerN'][$i]);
if($makerN[1]!=$makerN[2]){
$sql="****************************************************************************";
if(mysqli_query($conn,$sql)){
}
else{
echo "アップデートに失敗しました。\n";
tableUpdate($conn);
mysqli_close($conn);
exit();
}
}
}
if($sql==""){
echo "更新が選択されていません<br>\n";
tableUpdate($conn);
mysqli_close($conn);
exit();
}
echo "以下のように更新されました。<br>\n";
tableUpdate($conn);
mysqli_close($conn);
exit();
}
else{
echo "データが送信されてきていません。\n";
}
mysqli_close($conn);

function tableUpdate($conn){
$sql1="SELECT * FROM maker_tbl order by id asc;";
$sql2="*************************************************************************";
if($result=mysqli_query($conn,$sql1)){
$i=0;
while($row=mysqli_fetch_array($result,MYSQL_ASSOC)){
$id[$i]=$row['id'];
$maker[$i]=$row['makerName'];
$i++;
}
}
else{
echo "クエリが実行できませんでした。\n";
mysqli_close($conn);
exit();
}
if($result=mysqli_query($conn,$sql2)){
if(mysqli_num_rows($result)!=0){
echo "<form name=\"frm\" method=\"post\" action=\"figuer1Update.php\">\n";
echo "<table border=\"1\">\n
<tr><td>ID</td>
<td>商品コード</td>
<td>商品名</td>
<td>単価</td>
<td>在庫数</td>
<td>メーカ名</td></tr>\n";
while($row=mysqli_fetch_array($result,MYSQL_ASSOC)){
echo "<tr>\n";
echo "<td style=\"text-align:right;\">{$row['id']}</td>\n
<td style=\"text-align:right;\">{$row['productId']}</td>\n
<td>{$row['productName']}</td>\n
<td style=\"text-align:right;\">{$row['unitPrice']}</td>\n
<td style=\"text-align:right;\">{$row['stockNumber']}</td>\n";
echo "<td><select name=\"makerN[]\">\n";
$i=0;
while($i<count($maker)){
if($row['makerId']==$id[$i])
echo "<option value=\"{$row['productId']},{$row['makerId']},{$id[$i]},{$maker[$i]}\" selected>{$maker[$i]}</option>\n";
else
echo "<option value=\"{$row['productId']},{$row['makerId']},{$id[$i]},{$maker[$i]}\">{$maker[$i]}</option>\n";
$i++;
}
echo "</select></td></tr>\n";
}
echo "</table>\n";
echo "<input type=\"submit\" value=\"更新\">";
echo "</form>\n";
}
}
else{
echo "条件に合ったデータは存在しません<br>\n";
}
}
?>
</body>
</html>