[RISOLTO]Inserimento nel database

  • Creatore Discussione Creatore Discussione Altutto
  • Data di inizio Data di inizio

Altutto

Utente Attivo
30 Set 2013
262
0
16
stubborn.altervista.org
PHP:
  if(isset($_POST['messaggio'])){
$idartic = $_GET['articolo'];
$datta = date("d-m-Y H:i");
    $mexx_chat = $_POST['messaggio'];
$sostituire = array ('à','è','ì','ò','ù','<','>');
$con = array ('&agrave;','&egrave;','&igrave;','&ograve;','&ugrave;','&lt;','&gt;');
$mexy_chat = str_replace($sostituire, $con, $mexx_chat);
$mex_chat = nl2br(addslashes($mexy_chat));
    $query = "INSERT INTO tb_chat (user_chat, mex_chat, data, id_articolo) 
    VALUES ('$usern','$mex_chat','$datta','$idartic')";
var_dump($query);
    @mysql_query($query)or die (mysql_error());
    @mysql_close();
  }
è un codice che non funziona se $_POST['messaggio'] contiene lettere accentate: se non ci sono accenti, funziona alla grande, altrimenti, viene fuori: Incorrect string value: '\xF2' for column 'mex_chat' at row 1
Ho letto che il problema potrebbe essere dovuto alla collazione di mex_chat... Quale dovrei scegliere?
 
Prova a vedere se funziona commentando questa riga:
PHP:
$mex_chat = nl2br(addslashes($mexy_chat));
Poi se funziona vediamo come risolvere.
 
Fatto:
PHP:
$mex_chat = str_replace($sostituire, $con, $mexx_chat);
//$mex_chat = nl2br(addslashes($mexy_chat));
    $query = "INSERT INTO tb_chat (user_chat, mex_chat, data, id_articolo) 
    VALUES ('$usern','$mex_chat','$datta','$idartic')";
Viene sempre fuori
Incorrect string value: '\xE8' for column 'mex_chat' at row 1
 
Ok rimetti come prima e stampa il contenuto della variabile $mex_chat e posta qui.
 
Avevo scritto
Ciao,
questo è un test
E stampando a video $mex_chat viene effettivamente fuori
Ciao,
questo è un test
...
Il var_dump di $query è venuto
string(140) "INSERT INTO tb_chat (user_chat, mex_chat, data, id_articolo) VALUES ('altutto','Ciao,
questo è un test','08-11-2013 15:14','10')"
E poi viene fuori l'errore
Incorrect string value: '\xE8 un t...' for column 'mex_chat' at row 1
 
Ciao, molto probabilmente è un problema di codifica della tabella prova cosi
PHP:
$query = "INSERT INTO tb_chat (user_chat, mex_chat, data, id_articolo) 
    VALUES ('$usern','" . utf8_decode($_POST['messaggio']) . "','$datta','$idartic')";
nel caso posta la struttura della tabella
 
Ora ha inserito
Ciao
Questo ? un test
(codice
PHP:
    $mexx_chat = $_POST['messaggio'];
$sostituire = array ('à','è','ì','ò','ù','<','>');
$con = array ('&agrave;','&egrave;','&igrave;','&ograve;','&ugrave;','&lt;','&gt;');
$mexy_chat = str_replace($sostituire, $con, $mexx_chat);
$mex_chat = nl2br(addslashes(utf8_decode($mexy_chat)));
    $query = "INSERT INTO tb_chat (user_chat, mex_chat, data, id_articolo) 
    VALUES ('$usern','$mex_chat','$datta','$idartic')";
//var_dump($query);
    @mysql_query($query)or die (mysql_error());
)
La collazione attuale di mex_chat è utf8_unicode_ci
 
togli quello che riguarda str_replace(), non serve con la funzione utf8_decode()
 
ciao
non vorrei dire una bestialità, ma c'è qualcosa che non torna sul quel str_replace
se provi questo
PHP:
<?php
$mexx_chat="questo è un test";
echo $mexx_chat;
$sostituire = array ('à','è','ì','ò','ù','<','>');
$con = array ('&agrave;','&egrave;','&igrave;','&ograve;','&ugrave;','&lt;','&gt;');
$mexy_chat = str_replace($sostituire, $con, $mexx_chat);
echo "<br>";
echo $mexx_chat;
echo "<br>";
$mexx_chat="questo &egrave; un test";
echo $mexx_chat;
?>
e guardi la pagina con vedi sorgente sembra che non rimpiazzi
questo è un test<br>questo è un test<br>questo &egrave; un test
anche se ha video vedi per tutti i tre casi
questo è un test
questo è un test
questo è un test
 
Booh :confused:
In ogni caso, l'ho rimosso:
PHP:
    $mexx_chat = $_POST['messaggio'];
$mex_chat = nl2br(addslashes(utf8_decode($mexx_chat)));
    $query = "INSERT INTO tb_chat (user_chat, mex_chat, data, id_articolo) 
    VALUES ('$usern','$mex_chat','$datta','$idartic')";
Però viene sempre
ciao
questo ? un test
 
ciao
prova così
PHP:
<?php
$mexx_chat="questo è un test";
echo $mexx_chat;
$mexx_chat = htmlentities($mexx_chat);
echo "<br>";
echo $mexx_chat;
echo "<br>";
$mexx_chat="questo &egrave; un test";
echo $mexx_chat;
?>
con sorce
questo è un test<br>questo &egrave; un test<br>questo &egrave; un test
e a video
questo è un test
questo è un test
questo è un test
 
ciao
devi provare a sostituire questa parte
PHP:
<?php
//.....
$mexx_chat = $_POST['messaggio'];
$sostituire = array ('à','è','ì','ò','ù','<','>');
$con = array ('&agrave;','&egrave;','&igrave;','&ograve;','&ugrave;','&lt;','&gt;');
$mexy_chat = str_replace($sostituire, $con, $mexx_chat);
$mex_chat = nl2br(addslashes($mexy_chat));
//....
?>
con
PHP:
<?php
//.....
$mexx_chat = htmlentities($_POST['messaggio']);
//....
?>
e vedere sa fa quello che vuoi
se fa quello che vuoi poi prova ad aggiungere l'nl2br (da verificare se prima o dopo) mentre laddlsashes non credo che serva
 

Discussioni simili