2010-03-03 1 views
12

Uso joomla per gestire un sito Web ... e sto sviluppando un'applicazione php autonoma che inserirà e modificherà i dati nelle tabelle utilizzate da joomla per memorizzare l'html delle pagine Web che crea dinamicamente ...Inserimento codice html in una tabella mysql

Il modo in cui funziona è usare un componente joomla per creare contenuto e il codice html di questi articoli è memorizzato in un campo in una tabella, diciamo content_table, di joomla .. Questo codice html è in seguito recuperato per costruire una parte di una pagina web.

Voglio fare lo stesso con la mia app standalone ... aggiungo il codice html al file in content_table che può essere successivamente recuperato da joomla per costruire la parte della pagina.

Il problema è: il codice html, naturalmente, ha ovviamente molte virgolette singole e doppie e questo pone un problema durante l'inserimento nel database .. Ho provato mysql_escape_string() e ottengo ancora errori di sintassi. .

posso usare addslashes(), ma dal momento che joomla si recupera il codice in seguito, non è possibile utilizzare stripslashes() durante il recupero in un secondo momento ....

c'è qualche cosa che posso aggiungere il codice html il campo del tavolo ...

Grazie per i vostri suggerimenti ... !!

Edit: Dopo aver aggiunto mysql_escape_string() ottengo

Error adding details. Reason : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'fulltext = '\n 

Questa è la mia domanda:

UPDATE $jos_content 
SET introtext = '$intro_code', 
     fulltext = '$article_code' 
WHERE id = '$article_id'"; 

La stringa di input è la seguente:

$article_code = '<hr id="system-readmore" /> 
<center>{loadposition user50}</center> 
<p style="text-align: center;"> 
<span style="color: rgb(0, 255, 255);"> 
<i> 
<b> 
<span style="font-size: x-large;"> 
<span style="font-family: Arial;"> 
&nbsp; 
</span> 
</span> 
</b> 
</i> 
</span> 
<span style="color: rgb(0, 255, 255);"> 
<i> 
<b> 
<span style="font-size: x-large;"> 
<span style="font-family: Arial;"> 
<?php echo $title; ?> 
</span> 
</span> 
</b> 
</i> 
</span> 
<span style="color: rgb(0, 255, 255);"> 
<i> 
<b> 
<span style="font-size: x-large;"> 
<span style="font-family: Arial;"> 
<br /> 
</span> 
</span> 
</b> 
</i> 
</span> 
</p> 
<p style="text-align: center;"> 
<img height="269" width="515" border="3" 
title="<?php echo $title; ?>" 
alt=" <?php echo $title; ?>" 
src="<?php echo $article_image;?>" 
</p> 
<p> 
<span style="font-size: small;"> 
<span style="font-family: Arial;"> 
<span style="color: rgb(153, 204, 255);"> 
<p style="margin-top: 2px; margin-bottom: 2px; margin-left: 120px; text-align: left;"> 
<i> 
<span style="color: rgb(0, 255, 0);"> 
<strong> 
Cast&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 
</strong> 
<b> 
</b> 
</span> 
</i> 
<span style="color: rgb(0, 255, 255);"> 
<b> 
<?php echo $cast; ?> 
</b> 
</span> 
<i> 
<span style="color: rgb(0, 255, 255);"> 
<b> 
<br /> 
</b> 
</span> 
</i> 
<span style="font-family: Arial;"> 
<span style="font-size: small;"> 
<span style="color: rgb(153, 204, 255);"> 
</span> 
</span> 
<span style="color: rgb(0, 255, 0);"> 
<i> 
<strong> 
Direction&nbsp;&nbsp;&nbsp; 
</strong> 
</i> 
<strong> 
: 
</strong> 
<b> 
</b> 
</span> 
<span style="color: rgb(0, 255, 255);"> 
<b> 
<span class="href" 
id="ctl00_ContentPlaceHolderMainContent_FormView1_Director"> 
<?php echo $director; ?> 
</span> 
</b> 
</span> 
</span> 
<span style="font-family: Arial;"> 
<br /> 
<span style="color: rgb(0, 255, 0);"> 
<i> 
<strong> 
Production 
</strong> 
</i> 
<strong> 
: 
</strong> 
<b> 
</b> 
</span> 
<span style="color: rgb(0, 255, 255);"> 
<b> 
<?php echo $direction; ?> 
</b> 
</span> 
<span style="color: rgb(255, 102, 0);"> 
<i> 
<b> 
<br /> 
</b> 
</i> 
</span> 
<span style="font-family: Arial;"> 
<span style="color: rgb(0, 255, 0);"> 
<span style="font-family: Arial;"> 
<span style="font-size: small;"> 
<i> 
<strong> 
Music&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
</strong> 
</i> 
<strong> 
: 
</strong> 
</span> 
</span> 
</span> 
</span> 
<span style="color: rgb(0, 255, 255);"> 
<b> 
<i> 
</i> 
<?php echo $music; ?> 
<i> 
<br /> 
<span style="color: rgb(0, 255, 0);"> 
Lyrics&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
</span> 
</i> 
<span style="color: rgb(0, 255, 0);"> 
: 
</span> 
<i> 
</i> 
</b> 
</span> 
<span style="color: rgb(0, 255, 255);"> 
<b> 
<?php echo $lyrics; ?> 
</b> 
</span> 
<span style="color: rgb(0, 255, 255);"> 
<b> 
<i> 
<br /> 
</i> 
<span style="color: rgb(0, 255, 0);"> 
<i> 
Year&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
</i> 
: 
</span> 
<?php echo $year; ?> 
</b> 
</span> 
</span> 
<i> 
<span style="color: rgb(0, 255, 255);"> 
<b> 
</b> 
</span> 
</i> 
</p> 
</span> 
</span> 
</span> 
</p> 
<p> 
<left> 
{loadposition user14} 
&nbsp; 
</left> 
</p> 
<div style="text-align: center;"> 
<p> 
<i> 
<span style="font-family: Arial;"> 
<b> 
<span style="font-size: medium;"> 
<span style="color: rgb(51, 255, 255);"> 
Click 
<img src="images/stories/Play button1.png" 
alt="alt" /> 
in the Playlist to Download Songs 
</span> 
</span> 
</b> 
</span> 
</i> 
</p> 
</div> 
<table border="0" align="center"> 
<tbody> 
<tr> 
<td> 
<h4 style="text-align: center;"> 
<i> 
<span style="color: rgb(102, 255, 0);"> 
<b> 
<b> 
&nbsp;High Bandwidth Users 
</b> 
</b> 
</span> 
</i> 
<i> 
<span style="color: rgb(102, 255, 0);"> 
<b> 
<b> 
&nbsp; 
</b> 
</b> 
</span> 
</i> 
<span style="color: rgb(102, 255, 0);"> 
<b> 
</b> 
</span> 
</h4> 
</td> 
<td> 
<h4 style="text-align: center;"> 
<i> 
<span style="color: rgb(102, 255, 0);"> 
<b> 
<b> 
&nbsp;Low Bandwidth Users 
</b> 
</b> 
</span> 
</i> 
<span style="color: rgb(102, 255, 0);"> 
<b> 
<br /> 
</b> 
</span> 
</h4> 
</td> 
</tr> 
<tr> 
<td> 
{auto width=&quot;235&quot; displayheight=&quot;0&quot; height=&quot;225&quot;} <?php echo $hqList; ?> {/auto} 
</td> 
<td> 
{auto width=&quot;235&quot; displayheight=&quot;0&quot; height=&quot;225&quot;}<?php echo $lqList; ?>{/auto} 
</td> 
</tr> 
</tbody> 
</table> 
<center> 
{loadposition user50} 
</center>'; 
+2

. Piace ? – codaddict

+0

Sono in codice - non dovresti ricevere alcun errore a meno che non ci sia un bug nella funzione 'mysql_escape_string', che dubito fortemente. –

+0

Puoi stampare sia l'input che l'output di 'mysql_escape_string' che causa questo errore? –

risposta

2

Well..Debugged esso .. Si scopre che il problema era, dopo tutto, non con la funzione di fuga ...

Partenza la query:

UPDATE $jos_content 
SET introtext = '$intro_code', 
     fulltext = '$article_code' 
WHERE id = '$article_id'"; 

si può vedere il campo 'full-text' ... a quanto pare, la parola "full-text" è una parola chiave mysql ... Per essere precisi, si tratta di un tipo di campo come testo, INT, ecc MEDIUMTEXT ...

ho cambiato la query per questo

"UPDATE $jos_content 
SET $jos_content.introtext = '$intro_code', 
     $jos_content.fulltext = '$article_code' 
WHERE $jos_content.id = '$article_id'"; 

E voilà ... !!!!

+0

assicurati che il tuo tipo di dati sia nvarchar – Nealv

+1

In realtà non è un tipo di campo. È un tipo di indice. Credo che tu abbia anche potuto evitare il problema racchiudendo il nome del campo in back ticks ('\' ') – JAL

7

Non dovrebbe essere necessario barre. L'unica cosa che causerà un problema durante i normali inserti è le virgolette, e mysql_escape_string() dovrebbe gestirlo eccetto i problemi di charset. Prova anche mysql_real_escape_string().

Inoltre, si noti che la memorizzazione di codice HTML fornito dall'utente grezzo nel database può causare problemi di sicurezza. Prendi in considerazione l'utilizzo di qualcosa come bbcode o markdown.

+0

BBcode non è un'opzione qui .. Perché joomle ottiene solo la voce nel campo e lo ha inserito nella pagina ... !! non fa alcun tipo di parsing ... – SpikETidE

1

solo per confermare, la query assomiglia a questo diritto:

$query = ' 
    UPDATE "'.mysql_real_escape_string ($jos_content).'" 
    SET introtext = "'.mysql_real_escape_string ($intro_code).'", 
      fulltext = "'.mysql_real_escape_string ($article_code).'" 
    WHERE id = "'.mysql_real_escape_string ($article_id).'" 
"; 
25

Preferisco convertire il codice in stringa ordinaria prima di inserirlo nel database. Penso che sia lo scenario più sicuro.Considerare l'utilizzo di questo codice:

$article_code = base64_encode($article_code); 
/* insert to database */ 

Così, quando si vuole utilizzare quel codice indietro, basta decodificare usando base64_decode. Ti suggerisco di usare il tipo di dati 'testo' per salvare $ article_code piuttosto che 'varchar'.

+0

Hai ragione, l'unico contro è che aggiungerei far crescere i tuoi dati per il 33% in più ma fino a qui è una soluzione KISS! –

+13

E ora hai reso impossibile la ricerca nel tuo database. –

1

fulltext è una parola chiave predefine mysql. Si prega di utilizzare acuta (`) o virgoletta singola (')

Qui è il codice -

UPDATE $jos_content 
SET `introtext` = '$intro_code', 
     `fulltext` = '$article_code' 
WHERE `id` = '$article_id'"; 
1

Ho avuto lo stesso problema, ho riparato con le espressioni regolari. Si può usare qualcosa di simile: $target = '{~p class={{q}}important-text{{q}}~}Some text here {~/p~}';

e quindi utilizzare la funzione preg_replace():

class handle 
{ 
    public static function makehtml($target) 
    { 
    $output = preg_replace("#{~#", "<", $target); 
    $output = preg_replace("#~}#", ">", $target); 
    $output = preg_replace("#{{q}}#", '"', $target); 
    return $output; 
} 
} 
echo handle::makehtml($target); 
// output : <p class="important-text">Some text here</p> 
0

se siete preoccupati per lo spazio e si utilizza il metodo di codifica base 64 postato qui è possibile utilizzare il comando gzdeflate in php per abbatterlo e poi codificarlo. Ecco un piccolo script di test su alcuni caratteri di esempio generati. Ovviamente ci sono diversi metodi di compressione da esaminare se si è interessati alle dimensioni, ma questo significherebbe che potresti metterlo in mysql riducendo al minimo le dimensioni sul db. Potresti quindi leggerlo di nuovo con gzinflate (base64_decode (...));

<?php 
//generate sample chars for the example 
function generateRandomString($length = 10000) { 
    $characters = '"56/789abcdefghij:klmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
    $randomString = ''; 
    for ($i = 0; $i < $length; $i++) { 
     $randomString .= $characters[rand(0, strlen($characters) - 1)]; 
    } 
    return $randomString; 
} 
// 

$string =generateRandomString(); 
echo 'string size:'.strlen($string); 
$b64html = base64_encode($string); 
echo '<br/>'.'Original base64 size:'.strlen($b64html); 
$compressed = gzdeflate($string, 9); 
echo '<br/>'.'compressed size:'.strlen($compressed); 
echo '<br/>'.'base64 compressed size:'.strlen(base64_encode($compressed)); 
/* insert into db the base64_encode($compressed); */ 
?> 
0

Chiamando la funzione mysql_escape_string() passando la variabile che contiene il testo HTML come:

mysql_escape_string ($ _ POST [ "testo"]);

assicurerà che i caratteri speciali come le virgolette nel testo non causeranno un errore php e il database verrà aggiornato correttamente.

3

Questo è il modo migliore che ho trovato "mysql_escape_string Ho provato() e ancora ottenere gli errori di sintassi" addslashes()

$article_code = addslashes($article_code); 

UPDATE $jos_content 
SET introtext = '$intro_code', 
     fulltext = '$article_code' 
WHERE id = '$article_id'"; 
+0

usa stripslash ogni volta che vuoi usare quel codice. –