PHP and MySql accents and ñ problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BETA
Forum Commoner
Posts: 47
Joined: Fri Jul 25, 2008 3:21 am

PHP and MySql accents and ñ problems

Post by BETA »

Hello first i would like to say hello to all the community since im new here :P
second here is my problem...
I have a PHP news system i have made with a tutorial and i have a problem with adding news... when editing a new you can put accents on the news and even ñ but when i'm adding it, i mean adding a new one... it gives me error.
Here's my code...
Add news:

Code: Select all

<?php
include ('connect.php');
if (isset($_POST['submitted'])) {
if (empty($_POST['title'])) {
echo '<p><font color="red">Introduce un título válido.</font></p>';
} else {
$title = $_POST['title'];
}
if (empty($_POST['message'])) {
echo '<p><font color="red">Introduce un mensaje válido.</font></p>';
} else {
$message = $_POST['message'];
}
if ($title && $message) {
$query = "INSERT INTO news_posts (title, author, post, DATE) VALUES ('$title', '$nam', '$message', NOW())";
$result = mysql_query($query);
if ($result) {
echo '<p><font color="red">Noticia añadida!</font></p>';
echo '<a href="http://otakunosekai.es/index.php">Inicio.</a>';
} else {
echo '<font color="red"><p>Error, por favor inténtalo de nuevo.</p></font>';
}
} else {
echo '<p><font color="red">Introduce la información necesaria porfavor.</font></p>';
}
}
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<p><b>Título de la noticia :</b><br />
<input type="input" name="title" size="25" maxlength="60" value="<?php if(isset($_POST['title'])) echo $_POST['title']; ?>" /></p>
<p><b>Mensaje :</b><br />
<textarea rows="8" cols="55" name="message"><?php if(isset($_POST['message'])) echo $_POST['message']; ?></textarea></p>
<p><input type="submit" name="submit" value="Añadir" /></p>
<input type="hidden" name="submitted" value="TRUE" /></p>
</form>
Edit news:

Code: Select all

include('connect.php');
if ((isset($_GET['id'])) && (is_numeric($_GET['id'])) ) {
$id = $_GET['id'];
} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) {
$id = $_POST['id'];
} else {
echo 'Elige un post para editar primero.';
exit();
}
if (isset($_POST['submitted'])) {
$errors = array();
if (empty($_POST['title'])) {
$errors[] = 'Introduce un título válido.';
} else {
$title = $_POST['title'];
}
if (empty($_POST['message'])) {
$errors[] = 'Introduce un mensaje válido.';
} else {
$message = $_POST['message'];
}
if (empty($errors)) {
$query = "UPDATE news_posts SET title='$title', author='$nam', post='$message' WHERE id=$id";
$result = mysql_query($query);
if ($result) {
echo "Noticia actualizada!";
} else {
echo "Error, la noticia no se pudo actualizar.";
}
} else {
echo 'Multiples errores ocurrieron :<br />';
foreach ($errors as $msg) {
echo " - $msg<br />\n";
}
}
} else {
$query = "SELECT title, post, id FROM news_posts WHERE id=$id";
$result = mysql_query($query);
$num = mysql_num_rows($result);
$row = mysql_fetch_array ($result, MYSQL_NUM);
$title = $row['0'];
$message = $row['1'];
if ($num == 1) {
echo '<h3>Editar Noticia</h3>
<form action="?id=editarnoticias&num='.$id.'" method="post">
<p>Título : <input type="text" name="title" size="25" maxlength="255" value="'.$title.'" /></p>
<p>Mensaje : <br /><textarea rows="5" cols="40" name="message">'.$message.'</textarea></p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" /></p>
<input type="hidden" name="id" value="'.$id.'" />';
} else {
echo 'Error, por favor inténtalo de nuevo.';
}
}
?>
Messages are in spanish but i don't think that's a problem :P
I tried changing mysql collation but it didn't work...
Thx in advance!!
sureshmaharana
Forum Commoner
Posts: 30
Joined: Thu Jul 03, 2008 4:20 am
Contact:

Re: PHP and MySql accents and ñ problems

Post by sureshmaharana »

Can you tell me what error you are getting.
BETA
Forum Commoner
Posts: 47
Joined: Fri Jul 25, 2008 3:21 am

Re: PHP and MySql accents and ñ problems

Post by BETA »

Look at Line 52: echo 'Error, por favor inténtalo de nuevo.'; = (Try again.)
It's one of the errors from the error handler i made...
:cry:
sureshmaharana
Forum Commoner
Posts: 30
Joined: Thu Jul 03, 2008 4:20 am
Contact:

Re: PHP and MySql accents and ñ problems

Post by sureshmaharana »

Check Line no 37 i think your query is not working properly. It is fetching 0 records.
BETA
Forum Commoner
Posts: 47
Joined: Fri Jul 25, 2008 3:21 am

Re: PHP and MySql accents and ñ problems

Post by BETA »

no no no sorry i was wrong :banghead: it was in the first chunk of the code... the problem is when adding not when editing...
its error line is 21... :banghead:
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: PHP and MySql accents and ñ problems

Post by ghurtado »

In your error handler, just before you print the error to the screen, add this:

Code: Select all

 
echo mysql_error();
 
So that we may see the actual error MySQL is throwing at you. My guess is that some field was declared as NOT NULL and now you cant insert a record without specifying that field.

Buena suerte! (de donde eres? yo de Caceres!)
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: PHP and MySql accents and ñ problems

Post by ghurtado »

I just noticed this too: unless you are defining the variable "$nam" elsewhere, it is empty at the point when you execute the query, and that may be what MySQL doesn't like about your query.
BETA
Forum Commoner
Posts: 47
Joined: Fri Jul 25, 2008 3:21 am

Re: PHP and MySql accents and ñ problems

Post by BETA »

It is defined :) i just didn't post the entire code as it was not really necessary for the error...
Soy de valencia :)
MySql error is:Incorrect string value: '\xF1awda ...' for column 'post' at row 1
And yes value is not null for that table...
Oh and mysql collation is UTF8_general_ci

P.S: I noticed that i can use &ntilde; or &(vowel)acute; and it works but it's a smurf hard work and i don't think ppl know all the &; thingies... so i want to make my users be able to write accents and Ñs :banghead:
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: PHP and MySql accents and ñ problems

Post by ghurtado »

It sounds like you may have some kind of character set conversion issue. It is strange, cause the ñ is usually in every basic ASCII table, but there are so many places the character set can be set, that it can require some troubleshooting to find the source of the problem. It can be set globally at the Mysql server level, on the database, the tables, and even on the client connection, so there's a few places to check right there.

If you can't figure out the character set issue, you could always escape user input with htmlentities, which is something you would have to do when you display the data anyway. In HTML, you are supposed to escape characters like ñ to &ntilde;
BETA
Forum Commoner
Posts: 47
Joined: Fri Jul 25, 2008 3:21 am

Re: PHP and MySql accents and ñ problems

Post by BETA »

If i tell u the truth i really struggle with charsets so i'm gonna use htmlentities i think... gonna try it...
will tell u the results...thx!

P.D:Si tienes msn me lo podrias dar y asi preguntarte alguna cosilla mas rapido? es q los charsets me estan mareando y tengo mas de un problema con ellos... :cry:
BETA
Forum Commoner
Posts: 47
Joined: Fri Jul 25, 2008 3:21 am

Re: PHP and MySql accents and ñ problems

Post by BETA »

OK it worked perfectly!! Thx man!!!

P.D:Todavia tengo algunas dudas :S si eres tan amable me agregarias a metalninja@hotmail.com?
weno gracias de todas formas!!
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: PHP and MySql accents and ñ problems

Post by ghurtado »

You are welcome :)

(Te agregare al MSN, pero estoy en EEUU, con el cambio horario y el trabajo, no estoy casi nunca conectado, sera dificil pillarme)
Post Reply