@jackytom

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

PHPでInvalid parameter numberエラー

Q&A

Closed

PHPで、
Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters
のエラーが出ます。
89行目に問題があるようです。
検索したら、
https://qiita.com/sefp-maru/items/b8850c752def93106e34
を見つけたので、bindvalueに問題があるのかと思い、ソースコードを確認して修正しましたが、
エラーが消えません。
エラー原因の究明にご協力いただければ幸いです。
ソースコードを添付します。

<?php
try{
	require_once("db_connect.php");
	$pdo = new PDO("mysql:host=$SERV;dbname=$DB;charset=$CHAR",$USER,$PASS);
}
catch(PDOException $e){echo "次がエラーの内容です。;" .$e->getMessage();
}
?>
<!doctype html>
<html lang=ja>
	<head>
		<meta charset="utf-8">
		<title>News一覧</title>
		<meta name="description" content="Salon Yuri-rinの管理者ページ">
		<!--リセットcss-->
		<link rel="stylesheet" href="https://unpkg.com/ress/dist/ress.min.css">
		<!--css-->
		<link rel="stylesheet" href="css/news_style.css">
		<!--レスポンシブ対応-->
		<meta name="viwport" content="width=device-width, initial-scale=1">
		<!--Googleフォント-->
		<link href="https://fonts.googleapis.com/css2?family=Sawarabi+Gothic&display=swap" rel="stylesheet">
		<link href="https://fonts.googleapis.com/css2?family=Shippori+Mincho:wght@500&display=swap" rel="stylesheet">
	</head>
	<body>
		<header>
			<h1>News一覧</h1>
			<form class="conditions" action="news_table.php" method="post">
			<div class="wrapper">
				<div class="date1">
					<label>日付
						<input type="date" name="sdate" id="sdate">
					</label>
				</div>
				<div class="date2">
					<label><input type="date" name="edate" id="edate">
					</label>
				</div>
				<div class="dai">
					<label>タイトル
						<input type="text" name="title" id="title">
					</label>
				</div>
				<div class="naiyo">
					<label>内容
						<input type="text" name="content" id="title">
					</label>
				</div>
				<div class="jotai">
					<label>ステータス
						<select name="status" id="status">
							<option value="0">未公開</option>
							<option value="1">公開</option>
						</select>
					</label>
				</div>
			</div>
				<button type="submit" id="search" value="検索" class="s_button">検索</button>
			</form>
		</header>
		<main>
			<table class="tablehead">
				<tr>
					<th class="header">日付</th>
					<th class="header">タイトル</th>
					<th class="header">内容</th>
					<th class="header">公開ステータス</th>
					<th class="header2">
						<form class="create" action="news_create.php">
							<button type="submit" name="button" value="登録" class="button_a">登録</button>
						</form>
					</th>
					<th class="header2"></th>
				</tr>
			</table>
			<?php
				if(isset($_REQUEST['sdate']) && isset($_REQUEST['edate']) && isset($_REQUEST['title']) && isset($_REQUEST['content']) && isset($_REQUEST['status'])){
				$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
				//$title = "%".$_REQUEST['title']."%";
				//$content = "%".$_REQUEST['content']."%";
				$sql =
					('select * from news where 
						(pub_date >= ? and pub_date <= ?)
						and title like :title
						and content like :content
						and status = ?');
				$stmt = $pdo->prepare($sql);
				$stmt ->execute(
					[htmlspecialchars(date_format(date_create($_REQUEST['sdate']),'Y-m-d'), ENT_QUOTES | ENT_HTML5, 'UTF-8'),
					htmlspecialchars(date_format(date_create($_REQUEST['edate']),'Y-m-d'), ENT_QUOTES | ENT_HTML5, 'UTF-8'),
					htmlspecialchars($_REQUEST['title'], ENT_QUOTES | ENT_HTML5, 'UTF-8'),
					htmlspecialchars($_REQUEST['content'], ENT_QUOTES | ENT_HTML5, 'UTF-8'),
					htmlspecialchars($_REQUEST['status'], ENT_QUOTES | ENT_HTML5, 'UTF-8'),]);
				$stmt->bindvalue(":title", "%" . $_REQUEST['title'] . "%", PDO::PARAM_STR);
				$stmt->bindvalue(":content", "%" . $_REQUEST['content'] . "%", PDO::PARAM_STR);
				$row = $stmt->fetchAll(PDO::FETCH_ASSOC);}?>

				 <form action="?" method="post" class = "newstblform">';
				 	<div class = "table1">
				 		<?php foreach($stmt as $data){}?>
				 		<table class="newstable">
				 			<tr>
								<input type="hidden" name="seq" value="' . htmlspecialchars($data['id'], ENT_QUOTES | ENT_HTML5, 'UTF-8') . '" id="seq">';
							<td class="dt">
								<input type="text" name="pub_date" value="' . htmlspecialchars($data['pub_date'], ENT_QUOTES | ENT_HTML5, 'UTF-8') . '" id="sei" readonly="readonly">
							</td>
							<td class="dt">
								<input type="text" name="title" value="' . htmlspecialchars($data['title'], ENT_QUOTES | ENT_HTML5, 'UTF-8') . '" id=mei readonly="readonly">
							</td>
							<td class="dt">
								<input type="text" name="content" value="' . htmlspecialchars($data['content'], ENT_QUOTES | ENT_HTML5, 'UTF-8') .'" id="sei_kana">
							</td>
							<td class="dt">
								<input type="text" name="status" value="' . htmlspecialchars($data['status'], ENT_QUOTES | ENT_HTML5, 'UTF-8') .'" id="sei_kana">
							</td>
							<td class="bt">
								<input type="submit" name=button value="修正" formaction="news_update.php" class="button_a">
							</td>
							<td class="bt">
								<input type="submit" name=button value="削除" formaction="news_delete_conf.php" class="button_a">
							</td>
							</tr>
						</table>
					</div>
				</form>
		</main>
	</body>
</html>
0 likes

1Answer

エラーで “mixed named and positional parameters” と言われているように、名前付きパラメータ :title, :content と位置パラメータ ? は1つのクエリに混ぜて使えません。どちらかに揃えてください。
今回のコードでは :title:content を ? にすれば動くと思います。

1Like

Comments

  1. @jackytom

    Questioner

    コメントが遅くなり、申し訳ありません。
    回答ありがとうございました。

Your answer might help someone💌