Hi all,
I’m following this book and have some problems adding jokes to the database. Whenever I add characters like åäö the string seems to get cut where that character appears before it’s added to the database. Anyone know what I’m doing wrong?
Example:
String: Detta är ett exempel
Becomes: Detta (when added to the database)
<?php
if (get_magic_quotes_gpc()) {
$process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
while (list($key, $val) = each($process)) {
foreach ($val as $k => $v) {
unset($process[$key][$k]);
if (is_array($v)) {
$process[$key][stripslashes($k)] = $v;
$process[] = &$process[$key][stripslashes($k)];
} else {
$process[$key][stripslashes($k)] = stripslashes($v);
}
}
}
unset($process);
}
if (isset($_GET['addjoke'])){
include 'form.html.php';
exit();
}
try {
$pdo = new PDO('mysql:host=localhost;dbname=ijdb', 'ijdbuser', 'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
}
catch (PDOException $e) {
$output = 'Unable to connect to the database server: ' .
$e->getMessage();
include 'error.html.php';
exit();
}
if (isset($_POST['joketext'])) {
try {
$sql = 'INSERT INTO joke SET joketext = :joketext, jokedate = CURDATE()';
$s = $pdo->prepare($sql);
$s->bindValue(':joketext', $_POST['joketext']);
$s->execute();
}
catch (PDOException $e) {
$error = 'Error adding submitted joke: ' . $e->getMessage();
include 'error.html.php';
exit();
}
}
try {
$sql = 'SELECT joketext FROM joke';
$result = $pdo->query($sql);
}
catch (PDOEception $e) {
$error = 'Error fetching jokes: ' . $e->getMessage();
include 'error.html.php';
exit();
}
/*while ($row = $result->fetch()) {
$jokes[] = $row['joketext'];
}*/
foreach ($result as $row) {
$jokes[] = $row['joketext'];
}
include 'jokes.html.php';
?>