I'm having a problem where I made a website and I can successfully add data to every Student which works fine, however I'm trying to make a page, where I can basically search for the City(város in my php document) they live in, and it lists me all of those students data. I can succesfully insert into "város", however when I try to read the data, it doesn't work.
The Error message I get is:
SELECT * FROM Diák WHERE város = :város
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
my code is:
<?php
require "../config.php";
require "../common.php";
if (isset($_POST['submit'])) {
if (!hash_equals($_SESSION['csrf'], $_POST['csrf'])) die();
try {
$connection = new PDO($dsn, $username, $password, $options);
$sql = "SELECT *
FROM Diák
WHERE város = :város";
$város = $_POST['város'];
$statement = $connection->prepare($sql);
$statement->bindParam(':város', $város, PDO::PARAM_STR);
$statement->execute();
$result = $statement->fetchAll();
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
}
?>
<?php require "../../../htdocs/test/public/templates/header.php";?>
<?php
if (isset($_POST['submit'])) {
if ($result && $statement->rowCount() > 0) { ?>
<h2>Találatok</h2>
<table>
<thead>
<tr>
<th>Oktatási_id</th>
<th>Vezeték név</th>
<th>Keresztnév</th>
<th>Évfolyam</th>
<th>Születési dátum</th>
<th>Város</th>
<th>Utca</th>
<th>Házszám</th>
<th>Irányítószám</th>
<th>Szak</th>
<th>Kar</th>
</tr>
</thead>
<tbody>
<?php foreach ($result as $row) : ?>
<tr>
<td><?php echo escape($row["oktatási_id"]); ?></td>
<td><?php echo escape($row["vezeték_név"]); ?></td>
<td><?php echo escape($row["kereszt_név"]); ?></td>
<td><?php echo escape($row["évfolyam"]); ?></td>
<td><?php echo escape($row["születési_dátum"]); ?></td>
<td><?php echo escape($row["város"]); ?></td>
<td><?php echo escape($row["utca"]); ?> </td>
<td><?php echo escape($row["házszám"]); ?> </td>
<td><?php echo escape($row["irányítószám"]); ?> </td>
<td><?php echo escape($row["szak"]); ?> </td>
<td><?php echo escape($row["kar"]); ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php } else { ?>
<blockquote>Nem találtunk diákot, aki ilyen Városban lakna. <?php echo escape($_POST['város']); ?>.</blockquote>
<?php }
} ?>
<h2>Városon alapuló keresés.</h2>
<form method="post">
<input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>">
<label for="város">Város</label>
<input type="text" id="város" name="város">
<input type="submit" name="submit" value="View Results">
</form>
<a href="public/index.php">Vissza a fő oldalra.</a>
<?php require "../../../htdocs/test/public/templates/footer.php"; ?>
Apparently PDO doesn't understand named parameters that contain accented characters (I guess anything outside 7-bit ASCII). So change :város
to :varos
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.