Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 18

Discussione: Help: php e apostrofi

  1. #1

    Help: php e apostrofi

    Ciao a tutti,
    sono nuova del forum e quindi chiedo scusa se ho sbagliato sezione e soprattutto se il mio linguaggio non è prettamente tecnico.

    sto facendo volontariato in una organizzazione e come compito ho quello di gestire il loro sito, visto che un pochino (ma proprio poco) smanetto con la creazione di siti.

    Dalla scorsa settimana nell'inserimento delle news, (senza che venisse, apparentemente, toccati i form) quando inseriamo una parola preceduta con l'apostrofo si blocca l'inserimento e anche la modifica.

    Il messaggio di errore che da è questo "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 'allarme da mesi; fin dallo scorso ottobre, in occasione della tournée ' at line 1" davanti alla parola allarme ci sarebbe un apostrofo.

    Se al posto dell'apostrofo fatto copia incolla da word usiamo il codice ' questo problema non succede. Il problema è che le news si inseriscono spesso e da persone diverse alle quali non posso chiedere di sostituire manualmente l'apostrofo con il suo codice.
    Inoltre quello che non capiamo è che fino a settimana scorsa non dava problemi

    Forse non sono stata particolarmente chiara ma spero che qualcuno mi possa aiutare

    grazie

    Giulia

  2. #2
    Moderatore di PHP L'avatar di Alhazred
    Registrato dal
    Oct 2003
    Messaggi
    12,505
    Il problema è che prima di effettuare la query che inserisce/modifica la news bisogna passare il testo alla funzione
    mysql_real_escape_string()

    Il problema ti succede perché l'apostrofo secondo MySQL chiude la stringa, quindi ciò che segue l'apostrofo cerca di interpretarlo come un comando MySQL, che ovviamente non è e ti da errore.

  3. #3
    Grazie Alhazred per la risposta, esiste un punto preciso dove la devo inserire?

    Ti lascio il codice puoi dirmi come devo modificare la stringa?

    Posso chiederti perché (se c'è una spiegazione) da un giorno all'altro ha creato il problema?

    Grazie ancora!

    Codice PHP:
    <?php require_once("../Connections/cmswiz.php"); ?>
    <?php 
    require_once("common.php"); ?>
    <?php 
    require_once("rss.php"); ?>
    <?php

    getConn
    ();
    // in realtà questo file processa sia l'insert che il modify, ma è stata
    // una decisione presa dopo e non avevo
    // voglia di rinominarlo
    // TODO: cancellare l'eventuale file ruscato
    // TODO: verificare magic quotes
    // TODO: gestione categorie
    // -----------------------------------------
    // insert or modify?
    $myError '';
    $mydoing $_POST['mydoing'];// values: insert, modify
    if($mydoing == 'modify')
    {
        
    $idr $_POST['idr'];
    }

    $fileok true;

    // determiniamo il folder per l'upload:
    $tipo_th $_GET['id'];
    switch(
    $tipo_th)
    {
    case 
    '1':
        
    $myfolder 'notizie';
        break;
    case 
    '2':
        
    $myfolder 'corsi';
        break;
    case 
    '3':
        
    $myfolder 'comunicati';
        break;
    case 
    '4':
        
    $myfolder 'cospeflash';
        break;
    case 
    5:
        
    $myfolder 'cospenews';
        break;
    case 
    '6':
        
    $myfolder 'documenti';
        break;
    default:
        echo 
    'an error has occurred';
        break;
    }

    $queryz '';

    /* ----------------------- */
    $status_th = isset($_GET['status']) ? $_GET['status'] : 0;
    $categoria_th = isset($_POST['categoria_th']) ? $_POST['categoria_th'] : '';
    $titolo_th = isset($_POST['titolo_th']) ? str_replace("'""''"$_POST['titolo_th']) : '';
    $sottotitolo_th = isset($_POST['sottotitolo_th']) ? str_replace("'""''"$_POST['sottotitolo_th']) : '';
    $edizione_th = isset($_POST['edizione_th']) ? str_replace("'""''"$_POST['edizione_th']) : '';
    $home_th = (isset($_POST['home_th']) And ($_POST['home_th'] == false))? 1;
    $testoBreve_th = isset($_POST['testoBreve_th']) ? str_replace("'""''"$_POST['testoBreve_th']) : '';
    $testoLungo_th = isset($_POST['testoLungo_th']) ? str_replace("'""''"$_POST['testoLungo_th']) : '';
    if (isset(
    $_POST['ordine_th']))
    {
        
    $ordine_th $_POST['ordine_th'];
        if (!
    is_numeric($ordine_th))
            
    $ordine_th 0;
        
    }
    else
    {
        
    $ordine_th 0;
        
    // Faccio scorrere in giù tutti gli altri elementi
        
    $q "UPDATE varie SET ordine_th = ordine_th + 1 WHERE tipo_th = $tipo_th AND status_th = $status_th";
        
    mysql_query($q)or die(mysql_error());
    }
    $dataScadenzaNew_th = (isset($_POST['dataScadenzaNew_th']) && (strlen($_POST['dataScadenzaNew_th']) > 0)) ? "'" GiraData($_POST['dataScadenzaNew_th']) . "'""NULL";
    $dataScadenza_th = (isset($_POST['dataScadenza_th']) && (strlen($_POST['dataScadenza_th']) > 0)) ? "'" GiraData($_POST['dataScadenza_th']) . "'""NULL";
    /* ----------------------- */


    switch($tipo_th)
    {
    case 
    '1':
        
    // ***notizie***
        // edizione_th
        // ordine_th
        // testoLungo_th
        
    if($mydoing == 'insert'){
            
    $queryz "insert into varie (testoLungo_th, tipo_th, titolo_th, edizione_th, ordine_th, dataScadenzaNew_th, dataScadenza_th) values ('$testoLungo_th', $tipo_th, '$titolo_th','$edizione_th',$ordine_th$dataScadenzaNew_th$dataScadenza_th)";
        } else {
            
    $queryz "update varie set tipo_th = $tipo_th, titolo_th = '$titolo_th', edizione_th = '$edizione_th', testoLungo_th = '$testoLungo_th', ordine_th = $ordine_th, dataScadenzaNew_th = $dataScadenzaNew_th, dataScadenza_th = $dataScadenza_th, dataIns_th = now() WHERE id_th = $idr";
        }
        break;
    case 
    '2':
        echo 
    "insert corsi";
        
    // ***corsi***
        // categoria_th
        // titolo_th
        //testoBreve_th
        // testoLungo_th
        // ordine_th
        // home_th
        
    if($mydoing == 'insert'){
            
    $queryz "insert into varie (tipo_th, categoria_th, titolo_th, testoBreve_th, testoLungo_th, ordine_th, dataScadenzaNew_th, dataScadenza_th, home_th) values ($tipo_th$categoria_th, '$titolo_th', '$testoBreve_th', '$testoLungo_th', $ordine_th$dataScadenzaNew_th$dataScadenza_th$home_th)";
        } else {
            
    $queryz "update varie set tipo_th = $tipo_th, categoria_th = $categoria_th, titolo_th = '$titolo_th', testoBreve_th = '$testoBreve_th', testoLungo_th = '$testoLungo_th', ordine_th = $ordine_th, dataScadenzaNew_th = $dataScadenzaNew_th, dataScadenza_th = $dataScadenza_th, home_th=$home_th, dataIns_th = now() WHERE id_th = $idr";
        }
        break;
    case 
    '3':
        
    // comunicatistampa
        // titolo_th
        // testoLungo_th
        // edizione_th
        // ordine_th
        
    if($mydoing == 'insert'){
            
    $queryz "insert into varie (tipo_th, titolo_th, testoLungo_th, edizione_th, ordine_th, dataScadenzaNew_th, dataScadenza_th) values ($tipo_th, '$titolo_th', '$testoLungo_th', '$edizione_th', $ordine_th$dataScadenzaNew_th$dataScadenza_th)";
        } else {
            
    $queryz "update varie set tipo_th = $tipo_th, titolo_th = '$titolo_th', edizione_th = '$edizione_th', testoLungo_th = '$testoLungo_th', ordine_th = $ordine_th, dataScadenzaNew_th = $dataScadenzaNew_th, dataScadenza_th = $dataScadenza_th, dataIns_th = now() WHERE id_th = $idr";
        }
        break;
    case 
    '4':
        
    // cospeflash
        // titolo_th
        // edizione_th
        // ordine_th
        
    if($mydoing == 'insert'){
            
    $queryz "insert into varie (tipo_th, titolo_th, edizione_th, ordine_th, dataScadenzaNew_th, dataScadenza_th) values ($tipo_th, '$titolo_th', '$edizione_th', $ordine_th$dataScadenzaNew_th$dataScadenza_th)";
        } else {
            
    $queryz "update varie set tipo_th = $tipo_th, titolo_th = '$titolo_th', edizione_th = '$edizione_th', ordine_th = $ordine_th, dataScadenzaNew_th = $dataScadenzaNew_th, dataScadenza_th = $dataScadenza_th, dataIns_th = now() WHERE id_th = $idr";
        }
        break;
    case 
    5:
        
    // cospenews
        // titolo_th
        // edizione_th
        // testoLungo_th
        // ordine_th
        
    if($mydoing == 'insert'){
            
    $queryz "insert into varie (tipo_th, titolo_th, edizione_th, testoLungo_th, ordine_th, dataScadenzaNew_th, dataScadenza_th) values ($tipo_th, '$titolo_th', '$edizione_th', '$testoLungo_th', $ordine_th$dataScadenzaNew_th$dataScadenza_th)";
        } else {
            
    $queryz "update varie set tipo_th = $tipo_th, titolo_th = '$titolo_th', edizione_th = '$edizione_th', testoLungo_th = '$testoLungo_th', ordine_th = $ordine_th, dataScadenzaNew_th = $dataScadenzaNew_th, dataScadenza_th = $dataScadenza_th, dataIns_th = now() WHERE id_th = $idr";
        }
        break;
    case 
    '6':
        
    // documenti
        // categoria_th
        // titolo_th
        // sottotitolo_th
        // edizione_th
        // testoLungo_th
        // ordine_th
        
    if($mydoing == 'insert'){
            
    $queryz "insert into varie (tipo_th, categoria_th, titolo_th, sottotitolo_th, edizione_th, testoLungo_th, ordine_th, dataScadenzaNew_th, dataScadenza_th) values ($tipo_th, '$categoria_th', '$titolo_th', '$sottotitolo_th', '$edizione_th', '$testoLungo_th', $ordine_th$dataScadenzaNew_th$dataScadenza_th)";
        } else {
            
    $queryz "update varie set tipo_th = $tipo_th, categoria_th = $categoria_th, titolo_th = '$titolo_th', sottotitolo_th = '$sottotitolo_th', edizione_th = '$edizione_th', testoLungo_th = '$testoLungo_th', ordine_th = $ordine_th, dataScadenzaNew_th = $dataScadenzaNew_th, dataScadenza_th = $dataScadenza_th, dataIns_th = now() WHERE id_th = $idr";
        }
        break;
    default:
        
    $myError+='C\'è qualcosa che non va...
    '
    ;
        break;
    }
    //echo $queryz;
    // ouf! eseguiamo la query
    mysql_query($queryz)or die(mysql_error());

    // Devo aggiornare l'RSS ?
    if (($tipo_th == '1') And ($mydoing != 'insert'))
    {
        
    // Sto cambiando una notizia visibile ?
        
    $q "SELECT status_th FROM varie WHERE id_th = $idr";
        
    $rs mysql_query($q);
        
    $row_rs mysql_fetch_assoc($rs);
        
    $Visibile = ($row_rs["status_th"] == 1) ? true false;
        
    mysql_free_result($rs);

        if (
    $Visibile)
        {
            
    UpdateRSSNews();
        }
    }

    if(
    $myError=='')
    {
        
    // è andato tutto bene
        // header("Location: list.php?id=$tipo_th&status=$status_th");
        
    print '<script>window.location="list.php?id='.$tipo_th.'&status='.$status_th.'"</script>';
    }
    else
    {
        echo 
    "Errore: " $myError;
    }

    ?>

  4. #4
    Avranno cambiato qualche configurazione sul server dov'è ospitato il sito. C'è infatti un parametro di configurazione di php.ini che inserisce uno slash in automatico ai parametri passati con php.

    Però è un parametro opzionale, quindi può essere abilitato e disabilitato risolvendo o meno il problema

  5. #5
    Originariamente inviato da TheBomber999
    Avranno cambiato qualche configurazione sul server dov'è ospitato il sito. C'è infatti un parametro di configurazione di php.ini che inserisce uno slash in automatico ai parametri passati con php.

    Però è un parametro opzionale, quindi può essere abilitato e disabilitato risolvendo o meno il problema
    Ma, e perdona l'ignoranza, questo file php.ini lo trovo da qualche parte e lo posso modificare oppure non si può fare nulla!

    Grazie ancora!

  6. #6
    Dipende da chi ti fornisce il server! E' il file di configurazione di php, quindi non a tutti è dato toccarlo :-)

  7. #7
    Grazie,
    mentre per quello che diceva Alhazred è possibile come dice lui?

    Giulia

  8. #8
    Si certo,
    lui ti ha detto un modo corretto per risolvere.
    Impostare php.ini è un'altra possibile soluzione allo stesso problema

  9. #9
    Originariamente inviato da TheBomber999
    Si certo,
    lui ti ha detto un modo corretto per risolvere.
    Impostare php.ini è un'altra possibile soluzione allo stesso problema
    Ok allora magari aspetto lui se mi aiuta con il suo metodo visto che ho visto che questo file php.ini nel server non lo trovo.


  10. #10
    Ok, ti consiglio comunque ti informarti sul "MySQL Injection" perchè sei molto soggetta al rischio.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.