更新・追加の内容が反映されない
Q&A
Closed
解決したいこと
更新・追加をクリックしても内容が反映されません。
どのソースコードが原因でしょうか?
原因のphpはverb-edit.phpです。
同じように作成したperson-edit.phpはちゃんと機能します
verb-edit.php
該当するソースコード
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>更新追加</title>
<link rel="stylesheet" href="css/edit2.css">
</head>
<body>
<div class="th0">単語</div>
<div class="th1">~する</div>
<div class="th1">~した</div>
<div class="th1">~られる</div>
<div class="th1">~している</div>
<?php
$pdo=new PDO('mysql:host=localhost;dbname=english word;charset=utf8',
'staff', 'password');
if (isset($_REQUEST['command'])) {
switch ($_REQUEST['command']) {
case 'insert':
if (empty($_REQUEST['word']) || empty($_REQUEST['mean']) || empty($_REQUEST['past'])
|| empty($_REQUEST['past participle']) || empty($_REQUEST['ing'])) break;
$sql=$pdo->prepare('insert into verb_table values(?,?,?,?,?)');
$sql->execute(
[htmlspecialchars($_REQUEST['word']), $_REQUEST['mean'], $_REQUEST['past']
, $_REQUEST['past participle'], $_REQUEST['ing']]);
break;
case 'update':
if (empty($_REQUEST['word']) || empty($_REQUEST['mean']) || empty($_REQUEST['past'])
|| empty($_REQUEST['past participle'])|| empty($_REQUEST['ing'])) break;
$sql=$pdo->prepare(
'update verb_table set mean=?, past=?, past participle=?, ing=? where word=?');
$sql->execute(
[htmlspecialchars($_REQUEST['mean']), $_REQUEST['past'], $_REQUEST['past participle']
, $_REQUEST['ing'], $_REQUEST['word']]);
break;
}
}
foreach ($pdo->query('select * from verb_table') as $row) {
echo '<form class="ib" action="verb-edit.php" method="post">';
echo '<input type="hidden" name="command" value="update">';
echo '<input type="hidden" name="word" value="', $row['word'], '">';
echo '<div class="td0">';
echo $row['word'];
echo '</div> ';
echo '<div class="td1">';
echo '<input type="text" name="mean" value="', $row['mean'], '">';
echo '</div> ';
echo '<div class="td1">';
echo '<input type="text" name="past" value="', $row['past'], '">';
echo '</div> ';
echo '<input type="text" name="past participle" value="', $row['past participle'], '">';
echo '</div> ';
echo '<div class="td1">';
echo '<input type="text" name="ing" value="', $row['ing'], '">';
echo '</div> ';
echo '<div class="td2">';
echo '<input type="submit" value="更新">';
echo '</div> ';
echo '</form> ';
echo "\n";
}
?>
<form action="verb-edit.php" method="post">
<input type="hidden" name="command" value="insert">
<div class="td0"></div>
<div class="td1"><input type="text" name="word"></div>
<div class="td1"><input type="text" name="mean"></div>
<div class="td1"><input type="text" name="past"></div>
<div class="td1"><input type="text" name="past participle"></div>
<div class="td1"><input type="text" name="ing"></div>
<div class="td2"><input type="submit" value="追加"></div>
</form>
<div class="box1">
<a href="edit.php">前の画面に戻る</a>
</div>
<br>
<div class="box1">
<a href="main.php">メイン画面に戻る</a>
</div>
</body>
</html>
person-edit.php
正常に機能するソースコード
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>更新追加</title>
<link rel="stylesheet" href="css/edit2.css">
</head>
<body>
<div class="th0">単語</div>
<div class="th1">~は</div>
<div class="th1">~の</div>
<div class="th1">~に</div>
<div class="th1">~のもの</div>
<?php
$pdo=new PDO('mysql:host=localhost;dbname=english word;charset=utf8',
'staff', 'password');
if (isset($_REQUEST['command'])) {
switch ($_REQUEST['command']) {
case 'insert':
if (empty($_REQUEST['word']) || empty($_REQUEST['mean']) || empty($_REQUEST['word2'])
|| empty($_REQUEST['word3']) || empty($_REQUEST['word4'])) break;
$sql=$pdo->prepare('insert into person_table values(?,?,?,?,?)');
$sql->execute(
[htmlspecialchars($_REQUEST['word']), $_REQUEST['mean'], $_REQUEST['word2']
, $_REQUEST['word3'], $_REQUEST['word4']]);
break;
case 'update':
if (empty($_REQUEST['word']) || empty($_REQUEST['mean']) || empty($_REQUEST['word2'])
|| empty($_REQUEST['word3'])|| empty($_REQUEST['word4'])) break;
$sql=$pdo->prepare(
'update person_table set mean=?, word2=?, word3=?, word4=? where word=?');
$sql->execute(
[htmlspecialchars($_REQUEST['mean']), $_REQUEST['word2'], $_REQUEST['word3']
, $_REQUEST['word4'], $_REQUEST['word']]);
break;
}
}
foreach ($pdo->query('select * from person_table') as $row) {
echo '<form class="ib" action="person-edit.php" method="post">';
echo '<input type="hidden" name="command" value="update">';
echo '<input type="hidden" name="word" value="', $row['word'], '">';
echo '<div class="td0">';
echo $row['word'];
echo '</div> ';
echo '<div class="td1">';
echo '<input type="text" name="mean" value="', $row['mean'], '">';
echo '</div> ';
echo '<div class="td1">';
echo '<input type="text" name="word2" value="', $row['word2'], '">';
echo '</div> ';
echo '<input type="text" name="word3" value="', $row['word3'], '">';
echo '</div> ';
echo '<div class="td1">';
echo '<input type="text" name="word4" value="', $row['word4'], '">';
echo '</div> ';
echo '<div class="td2">';
echo '<input type="submit" value="更新">';
echo '</div> ';
echo '</form> ';
echo "\n";
}
?>
<form action="person-edit.php" method="post">
<input type="hidden" name="command" value="insert">
<div class="td0"></div>
<div class="td1"><input type="text" name="word"></div>
<div class="td1"><input type="text" name="mean"></div>
<div class="td1"><input type="text" name="word2"></div>
<div class="td1"><input type="text" name="word3"></div>
<div class="td1"><input type="text" name="word4"></div>
<div class="td2"><input type="submit" value="追加"></div>
</form>
<div class="box1">
<a href="edit.php">前の画面に戻る</a>
</div>
<br>
<div class="box1">
<a href="main.php">メイン画面に戻る</a>
</div>
</body>
</html>
0