Visualizzazione risultati 1 fino 6 di 6

Discussione: aiuto guestbook

  1. #1
    Guest

    Predefinito aiuto guestbook

    Sto' provando questo guestbook php/MySQL, funziona tutto pero' quando clicco 'View Guestbook' mostra solo ID e Date/Time, i valori Name, Email e Comment rimangono vuoti.. non capisco cosa puo' essere.
    riporto dettagli codice e sql:


    CREATE TABLE `guestbook` (
    `id` int(4) NOT NULL auto_increment,
    `name` varchar(65) NOT NULL default '',
    `email` varchar(65) NOT NULL default '',
    `comment` longtext NOT NULL,
    `datetime` varchar(65) NOT NULL default '',
    PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


    guestbook.php
    Codice PHP:
    <table width="400" border="0" align="center" cellpadding="3" cellspacing="0">

    <
    tr>
    <
    td><strong>Test Sign Guestbook </strong></td>
    </
    tr>

    </
    table>

    <
    table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">

    <
    tr>
    <
    form id="form1" name="form1" method="post" action="addguestbook.php">
    <
    td>
    <
    table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
    <
    tr>
    <
    td width="117">Name</td>
    <
    td width="14">:</td>
    <
    td width="357">
    <
    input name="name" type="text" id="name" size="40"/>

    </
    td>
    </
    tr>
    <
    tr>
    <
    td>Email</td>
    <
    td>:</td>
    <
    td><input name="email" type="text" id="email" size="40" />
    </
    td>
    </
    tr>
    <
    tr>
    <
    td valign="top">Comment</td>
    <
    td valign="top">:</td>
    <
    td><textarea name="comment" cols="40" rows="3" id="comment">
    </
    textarea></td>
    </
    tr>
    <
    tr>
    <
    td>&nbsp;</td>
    <
    td>&nbsp;</td>
    <
    td>
    <
    input type="submit" name="Submit" value="Submit" />
    <
    input type="reset" name="Submit2" value="Reset" />
    </
    td>
    </
    tr>
    </
    table>
    </
    td>
    </
    form>
    </
    tr>
    </
    table>
    <
    table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
    <
    tr>
    <
    td><strong><a href="viewguestbook.php">View Guestbook</a> </strong></td>
    </
    tr>
    </
    table>

    addguestbook.php
    Codice PHP:
    <?php
    $host
    ="localhost"; // Host name
    $username="pippo"; // Mysql username
    $password=""; // Mysql password
    $db_name="my_pippo"; // Database name
    $tbl_name="guestbook"; // Table name

    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
    mysql_select_db("$db_name")or die("cannot select DB");

    $datetime=date("y-m-d h:i:s"); //date time

    $sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
    $result=mysql_query($sql);

    //check if query successful
    if($result){
    echo
    "Successful";
    echo
    "<BR>";

    // link to view guestbook page
    echo "<a href='viewguestbook.php'>View guestbook</a>";
    }

    else {
    echo
    "ERROR";
    }

    mysql_close();
    ?>

    viewguestbook.php

    Codice PHP:
    <table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
    <tr>
    <td><strong>View Guestbook | <a href="guestbook.php">Sign Guestbook</a> </strong></td>
    </tr>
    </table>
    <br>

    <?php
    $host
    ="localhost"; // Host name
    $username="pippo"; // Mysql username
    $password=""; // Mysql password
    $db_name="my_pippo"; // Database name
    $tbl_name="guestbook"; // Table name


    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
    mysql_select_db("$db_name")or die("cannot select DB");

    $sql="SELECT * FROM $tbl_name";
    $result=mysql_query($sql);

    while(
    $rows=mysql_fetch_array($result)){
    ?>

    <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
    <tr>
    <td>ID</td>
    <td>:</td>
    <td>
    <? echo $rows['id']; ?></td>
    </tr>
    <tr>
    <td width="117">Name</td>
    <td width="14">:</td>
    <td width="357"><? echo $rows['name']; ?>
    </td>
    </tr>
    <tr>
    <td>Email</td>
    <td>:</td>
    <td><? echo $rows['email']; ?></td>
    </tr>
    <tr>
    <td valign="top">Comment</td>
    <td valign="top">:</td>
    <td><? echo $rows['comment']; ?></td>
    </tr>
    <tr>
    <td valign="top">Date/Time </td>
    <td valign="top">:</td>
    <td><? echo $rows['datetime']; ?></td>
    </tr>
    </table></td>
    </tr>
    </table>

    <?php
    }
    mysql_close(); //close database
    ?>


    grazie

  2. #2
    mzanella non è connesso AlterGuru
    Data registrazione
    29-12-2015
    Messaggi
    1,954

    Predefinito

    funziona tutto pero' quando clicco 'View Guestbook' mostra solo ID e Date/Time, i valori Name, Email e Comment rimangono vuoti
    Allora direi che non "funziona" proprio "tutto" !
    Hai controllato quali sono i valori delle colonne name, email e comment nella base di dati? Guardando il codice in addguestbook.php ho il forte sospetto che vengano inserite delle stringhe vuote:
    Codice PHP:
    $datetime=date("y-m-d h:i:s"); //date time

    $sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
    L'inserimento avviene interpolando (pessima idea!) sulle variabili $name, $email, $comment, le quali non sono inizializzate, quindi hanno "valore vuoto".

    Altra considerazione di carattere generale: le funzioni mysql_* sono deprecate, dovresti usare mysqli o PDO.

  3. #3
    Guest

    Predefinito

    ok, scusa ma sono agli inizi con MySQL, come potrei modificare le variabili?

  4. #4
    mzanella non è connesso AlterGuru
    Data registrazione
    29-12-2015
    Messaggi
    1,954

    Predefinito

    Devi leggere i dati passati dal form (in guestbook.php) alla pagina tramite POST, quindi:
    Codice PHP:
    $name = $_POST['name'];
    $email = $_POST['email'];
    $comment = $_POST['comment'];
    $_POST è un vettore "speciale", inizializzato automaticamente, che contiene tutti i dati passati tramite POST, ai quali puoi accedere usando il nome dei campi come indice.

    I passi successivi normalmente sono i controlli di sicurezza sull'input, ad esempio evitare che $name contenga del codice SQL pericoloso (SQL injection), vedi tu se vuoi approfondire ora l'argomento o meno.

  5. #5
    jackpare non è connesso Neofita
    Data registrazione
    20-01-2016
    Messaggi
    6

    Predefinito

    Ho Rinnovato il servizio HTTPS doveva essere attivati in circa 15 minuti sono 04 giorni che aspetto.
    http://jackpare.altervista.org/

  6. #6
    darbula non è connesso AlterGuru 2500
    Data registrazione
    24-04-2011
    Messaggi
    2,894

    Predefinito

    Salve il tuo sito sembra essere fruibile. https://jackpare.ssl.altervista.org/ .. Qui sei fuori tema,puoi aprire una nuova discussione.

Regole di scrittura

  • Non puoi creare nuove discussioni
  • Non puoi rispondere ai messaggi
  • Non puoi inserire allegati.
  • Non puoi modificare i tuoi messaggi
  •