[RISOLTO]Inserimento nel database

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?
 

Longo8

Utente Attivo
28 Mar 2011
1.694
0
36
Prova a vedere se funziona commentando questa riga:
PHP:
$mex_chat = nl2br(addslashes($mexy_chat));
Poi se funziona vediamo come risolvere.
 

Altutto

Utente Attivo
30 Set 2013
262
0
16
stubborn.altervista.org
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
 

Longo8

Utente Attivo
28 Mar 2011
1.694
0
36
Ok rimetti come prima e stampa il contenuto della variabile $mex_chat e posta qui.
 

Altutto

Utente Attivo
30 Set 2013
262
0
16
stubborn.altervista.org
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
 

criric

Super Moderatore
Membro dello Staff
SUPER MOD
MOD
21 Ago 2010
5.607
54
48
TN
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
 

Altutto

Utente Attivo
30 Set 2013
262
0
16
stubborn.altervista.org
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
 

criric

Super Moderatore
Membro dello Staff
SUPER MOD
MOD
21 Ago 2010
5.607
54
48
TN
togli quello che riguarda str_replace(), non serve con la funzione utf8_decode()
 

borgo italia

Super Moderatore
Membro dello Staff
SUPER MOD
MOD
4 Feb 2008
16.046
150
63
PR
www.borgo-italia.it
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
 

Altutto

Utente Attivo
30 Set 2013
262
0
16
stubborn.altervista.org
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
 

borgo italia

Super Moderatore
Membro dello Staff
SUPER MOD
MOD
4 Feb 2008
16.046
150
63
PR
www.borgo-italia.it
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
 

borgo italia

Super Moderatore
Membro dello Staff
SUPER MOD
MOD
4 Feb 2008
16.046
150
63
PR
www.borgo-italia.it
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
Autore Titolo Forum Risposte Data
A [RISOLTO] Inserimento nel database MySQL 5
C [RISOLTO]Inserimento variabile php in input html PHP 20
A [RISOLTO]Inserimento Immagini da pc a MySql PHP 15
M [risolto] Inserimento link in tabella da database PHP 3
H [RISOLTO]Inserimento file xml Ajax 3
pup3770 [RISOLTO]Pagina di inserimento con visualizzazione temporizzata PHP 7
M [RISOLTO] inserimento dati db PHP 22
O [risolto] Inserimento nuovo campo in mysql. Problema PHP 6
M [RISOLTO]Inserimento Slide Html HTML e CSS 5
Jonn Inserimento [RISOLTO] sui tread Supporto Mr.Webmaster 1
L (risolto) MySQL 0
B getElementById su piu id(Risolto) Javascript 7
L Esercitarsi con Js [RISOLTO] Javascript 4
L risolto visualizzazione e ordinamento dati PHP 1
moustache [RISOLTO] SQL PHP IIS PHP 8
Sergio Unia Ricezione email con destinatari multipli [Risolto] PHP 2
L update tabelle in php mysql [risolto] PHP 6
M Semplice visualizzatore di immagini [risolto con plugin wp] PHP 7
L [RISOLTO] Stampa a video risultato count in html PHP 13
L [RISOLTO] Eliminare una discussione creata PHP 3
tomorc [HTML] Problema con scroll bar (risolto) HTML e CSS 0
A [PHP] Problema query insert [RISOLTO] PHP 14
B [PHP] recuperare IP dei server in load balancing [RISOLTO] PHP 3
K [RISOLTO] Problema Griglia Php+Mysql PHP 13
S [RISOLTO] aggiorna tabella da select option asp classic Classic ASP 7
elpirata [RISOLTO][Javascript] Datapicker e autocompletamento campo input Javascript 2
elpirata [RISOLTO][Mysql] Problema insert valori apostrofati MySQL 1
elpirata [RISOLTO][Mysql] Contare le occorrenze in un campo tipo varchar MySQL 2
G [MS Access] Gestione biglietti [RISOLTO] MS Access 2
G [MS Access] Casella combinata & Query [RISOLTO] MS Access 4
G [MS Access] Query mese corrente con conteggio [RISOLTO] MS Access 2
M [RISOLTO]Windows media player non mi funziona più su win 10 pro 64 bit Windows e Software 2
C [RISOLTO][PHP] Errore di sintassi PHP 8
IT9-Gpp [RISOLTO] Leggere variabile restituita da success Ajax 3
Kolop [RISOLTO][PHP] Problema Pagination PHP 2
C [RISOLTO][PHP] Funzione ONclick PHP 14
C [RISOLTO][PHP] Conteggio righe di una tabella PHP 4
N [PHP] Utilizzo variabili di sessione [Risolto] PHP 13
Tommy03 [RISOLTO][PHP] Webserver o devserver? PHP 2
Sergio Unia Recupero dati da una vecchia versione MySql [Risolto] MySQL 4
spider81man [PHP] Problemi cancellazione dato su DB [RISOLTO] PHP 1
A [PHP] RISOLTO Invio Mail con Tabella PHP 2
felino Risolto - [Wordpress][WooCommerce] PayPal Checkout e campi di fatturazione WordPress 2
elpirata [PHP][RISOLTO] Sommare gli importi estratti da un ciclo while PHP 3
elpirata [PHP][RISOLTO] Effettuare la somma dei tempi di lavorazione PHP 3
elpirata [PHP] [RISOLTO]Sovrascrivere testo in una tabella PHP 2
A [RISOLTO]Recuperare dati inviati con json tramite php PHP 4
C [RISOLTO][PHP] Passaggio variabili senza refresh di pagina PHP 7
elpirata [PHP][RISOLTO] Errore di tipo Notice: Undefined index - Come risolvere quando si hanno tante var PHP 10
S Problema in PHP per invio file XML - RISOLTO- PHP 8

Discussioni simili