PHP MySQL Novice Ninja - Adding jokes

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';
?>

If you test your input again using a string containing spaces eg “This and that” and only “This” is entered into your database then you may discover your string is being truncated at the first space.

This is likely to be caused because your html form value has not been correctly quoted.

ie


<?php
$value = "This and that";
?>
.. your form
<input type=text value=<?php echo $value; ?> name=mytext >
... your form

Should be


<?php
$value = "This and that";
?>
.. your form
<input type=text value="<?php echo $value; ?>" name=mytext >
... your form

This is only a guess based on what you said and a common cause of that symptom. It may well be caused by your foreign character, but if you can confirm it is not then someone might be able help you further.

It’s the foreign characters. If I write something like “Hello. It’s a nice day, isn’t it?”, it will work just fine. But as soon as I use åäö or even ï, the string is being truncated(?) just before that character.

Can it be something with the SET NAMES ‘utf8’, not working correctly?

Hi, thanks for eliminating that possibility anyway.

Here is my 2c worth:

If you are going to be using utf-8 then you have to be totally utf-8 from the start.

The PHP files themselves must be utf-8 encoded (check this using a good IDE)

The webpage encoding must be utf-8.

The tables must be set utf-8 encoded.

From start to end all must be utf-8, that is the only way I could get this stuff to work – after reading this guys valuable advice [google]kore nordmann utf8[/google]. YMMV.

Yeah, might be something like that. While inserting data manually with the terminal into the database, it looks like this in the browser while using a select query: Vi testar med sådana här tecken

Probably missed something when creating the DB.

Thanks for the help. Will try to start over and see if it’ll work.