追加・更新ページの実行
Q&A
Closed
解決したいこと
追加・更新のページを作成したのですが、正しくソースコードを書いたのですが、上手く動かないのでどのように修正したらいいのか
わかりやすく解説をお願いします。
発生している問題・エラー
Parse error: syntax error, unexpected token "foreach" in C:\xampp\htdocs\課題7\View\edit2.php on line 45
edit.php
該当するソースコード
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>更新追加</title>
<link rel="stylesheet" href="css/search.css">
</head>
<body>
<form action="edit2.php" method="post">
<p><input type="radio" name="1" value="person">人</p>
<br>
<p><input type="radio" name="1" value="verb">動詞</p>
<br>
<p><input type="radio" name="1" value="interrogative">疑問詞</p>
<br>
<p><input type="radio" name="1" value="ajective">形容詞</p>
<br>
<p><input type="radio" name="1" value="others">その他</p>
<br>
<br>
<p><input type="submit" value="次へ"></p>
</form>
</body>
</html>
edit2.php
該当するソースコード
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>更新追加</title>
<link rel="stylesheet" href="css/search.css">
</head>
<body>
<?php
switch ($_REQUEST['1']) {
case 'person':
echo'<div class="th0">単語</div>';
echo'<div class="th1">~は</div>';
echo'<div class="th2">~の</div>';
echo'<div class="th3">~に</div>';
echo'<div class="th4">~のもの</div>';
$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 id=word');
$sql->execute(
[htmlspecialchars($_REQUEST['word']), $_REQUEST['mean'],
$_REQUEST['word2'], $_REQUEST['word3'], $_REQUEST['word4']]);
break;
}
}
foreach($pdo->query('select * from person_table') as $row) {
echo '<form class="ib" action="edit2.php" method="post">';
echo '<input type="hidden" name="command" value="update">';
echo '<div class="td1">';
echo '<input type="text" name="word" value="', $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 '<div class="td1">';
echo '<input type="text" name="word2" value="', $row['word3'], '">';
echo '</div> ';
echo '<div class="td1">';
echo '<input type="text" name="word2" value="', $row['word4'], '">';
echo '</div> ';
echo '<div class="td2">';
echo '<input type="submit" value="更新">';
echo '</div> ';
echo "\n";
}
<form action="edit2.php" method="post">
<input type="hidden" name="command" value="insert">
<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>
break;
case 'verb':
echo'<div class="th0">単語</div>';
echo'<div class="th1">~する</div>';
echo'<div class="th2">~した</div>';
echo'<div class="th3">~られる</div>';
echo'<div class="th4">~している</div>';
$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 id=word');
$sql->execute(
[htmlspecialchars($_REQUEST['word']), $_REQUEST['mean'],
$_REQUEST['past'], $_REQUEST['past participle'], $_REQUEST['ing']]);
break;
}
}
foreach ($pdo->query('select * from verb_table') as $row) {
echo '<form class="ib" action="edit2.php" method="post">';
echo '<input type="hidden" name="command" value="update">';
echo '<div class="td1">';
echo '<input type="text" name="word" value="', $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['past'], '">';
echo '</div> ';
echo '<div class="td1">';
echo '<input type="text" name="word2" value="', $row['past participle'], '">';
echo '</div> ';
echo '<div class="td1">';
echo '<input type="text" name="word2" value="', $row['ing'], '">';
echo '</div> ';
echo '<div class="td2">';
echo '<input type="submit" value="更新">';
echo '</div> ';
echo "\n";
}
<form action="edit2.php" method="post">
<input type="hidden" name="command" value="insert">
<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>
break;
case 'interrogative':
echo'<div class="th0">単語</div>';
echo'<div class="th1">意味</div>';
echo'<div class="th2">例文</div>';
echo'<div class="th3">和訳</div>';
$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['example'])
|| empty($_REQUEST['Japanese'])) break;
$sql=$pdo->prepare('insert into interrogative_table values(?,?,?,?)');
$sql->execute(
[htmlspecialchars($_REQUEST['word']), $_REQUEST['mean'], $_REQUEST['example']
, $_REQUEST['Japanese']]);
break;
case 'update':
if (empty($_REQUEST['word']) || empty($_REQUEST['mean']) || empty($_REQUEST['example'])
|| empty($_REQUEST['Japanese'])) break;
$sql=$pdo->prepare(
'update interrogative_table set mean=?, example=?, Japanese=? where id=word');
$sql->execute(
[htmlspecialchars($_REQUEST['word']), $_REQUEST['mean'],
$_REQUEST['example'], $_REQUEST['Japanese']]);
break;
}
}
foreach ($pdo->query('select * from interrogative_table') as $row) {
echo '<form class="ib" action="edit2.php" method="post">';
echo '<input type="hidden" name="command" value="update">';
echo '<div class="td1">';
echo '<input type="text" name="word" value="', $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['example'], '">';
echo '</div> ';
echo '<div class="td1">';
echo '<input type="text" name="word2" value="', $row['Japanese'], '">';
echo '</div> ';
echo '<div class="td2">';
echo '<input type="submit" value="更新">';
echo '</div> ';
echo '</form> ';
echo "\n";
}
<form action="edit2.php" method="post">
<input type="hidden" name="command" value="insert">
<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="example"></div>
<div class="td1"><input type="text" name="Japanese"></div>
<div class="td2"><input type="submit" value="追加"></div>
</form>
break;
case 'ajectiveve':
echo'<div class="th0">単語</div>';
echo'<div class="th1">意味</div>';
echo'<div class="th2">~より</div>';
echo'<div class="th3">いちばん</div>';
$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['er'])
|| empty($_REQUEST['est'])) break;
$sql=$pdo->prepare('insert into ajective_table values(?,?,?,?)');
$sql->execute(
[htmlspecialchars($_REQUEST['word']), $_REQUEST['mean'], $_REQUEST['er']
, $_REQUEST['est']]);
break;
case 'update':
if (empty($_REQUEST['word']) || empty($_REQUEST['mean']) || empty($_REQUEST['er'])
|| empty($_REQUEST['est'])) break;
$sql=$pdo->prepare(
'update ajective_table set mean=?, er=?, est=? where id=word');
$sql->execute(
[htmlspecialchars($_REQUEST['word']), $_REQUEST['mean'],
$_REQUEST['er'], $_REQUEST['est']]);
break;
}
}
foreach ($pdo->query('select * from ajective_table') as $row) {
echo '<form class="ib" action="edit2.php" method="post">';
echo '<input type="hidden" name="command" value="update">';
echo '<div class="td1">';
echo '<input type="text" name="word" value="', $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['er'], '">';
echo '</div> ';
echo '<div class="td1">';
echo '<input type="text" name="word2" value="', $row['est'], '">';
echo '</div> ';
echo '<div class="td2">';
echo '<input type="submit" value="更新">';
echo '</div> ';
echo '</form> ';
echo "\n";
}
<form action="edit2.php" method="post">
<input type="hidden" name="command" value="insert">
<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="er"></div>
<div class="td1"><input type="text" name="est"></div>
<div class="td2"><input type="submit" value="追加"></div>
</form>
break;
case 'others':
echo'<div class="th0">単語</div>';
echo'<div class="th1">意味</div>';
echo'<div class="th2">例文</div>';
echo'<div class="th3">和訳</div>';
$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['example'])
|| empty($_REQUEST['Japanese'])) break;
$sql=$pdo->prepare('insert into others_table values(?,?,?,?)');
$sql->execute(
[htmlspecialchars($_REQUEST['word']), $_REQUEST['mean'], $_REQUEST['example']
, $_REQUEST['Japanese']]);
break;
case 'update':
if (empty($_REQUEST['word']) || empty($_REQUEST['mean']) || empty($_REQUEST['example'])
|| empty($_REQUEST['Japanese'])) break;
$sql=$pdo->prepare(
'update others_table set mean=?, example=?, Japanese=? where id=word');
$sql->execute(
[htmlspecialchars($_REQUEST['word']), $_REQUEST['mean'],
$_REQUEST['example'], $_REQUEST['Japanese']]);
break;
}
}
foreach ($pdo->query('select * from others_table') as $row) {
echo '<form class="ib" action="edit2.php" method="post">';
echo '<input type="hidden" name="command" value="update">';
echo '<div class="td1">';
echo '<input type="text" name="word" value="', $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['example'], '">';
echo '</div> ';
echo '<div class="td1">';
echo '<input type="text" name="word2" value="', $row['Japanese'], '">';
echo '</div> ';
echo '<div class="td2">';
echo '<input type="submit" value="更新">';
echo '</div> ';
echo '</form> ';
echo "\n";
}
<form action="edit2.php" method="post">
<input type="hidden" name="command" value="insert">
<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="example"></div>
<div class="td1"><input type="text" name="Japanese"></div>
<div class="td2"><input type="submit" value="追加"></div>
</form>
break;
}
echo'<div class="box1">';
echo'<a href="search-input.php">チェック画面に戻る</a>';
echo'</div>';
print_r('<br>');
print_r('<br>');
echo'<div class="box1">';
echo'<a href="main.php">メイン画面に戻る</a>';
echo'</div>';
?>
</form>
</body>
</html>
0