Visualizzazione risultati 1 fino 2 di 2

Discussione: Problema con mysql e php

  1. #1
    Guest

    Predefinito Problema con mysql e php

    Buongiorno,
    stavo creando un programma di inserimento dati e registrazione, io nel mio programma creo una tabella di nome creatab.php
    Codice PHP:
    <?php
    $cn
    =mysql_connect("localhost","fantascudettonew","kugfutekru28") or die("Connessione non riuscita: " . mysql_error());
    mysql_select_db("my_fantascudettonew");

    $query = "CREATE TABLE user (
    user_id INT(11) NOT NULL AUTO_INCREMENT,
    user_firstname VARCHAR (50) NOT NULL,
    user_lastname VARCHAR (50) NOT NULL,
    user_username VARCHAR (11) NOT NULL,
    user_password VARCHAR (32) NOT NULL,
    user_email VARCHAR (50) NOT NULL,
    user_reg TINYINT (1) NOT NULL default 0,
    PRIMARY KEY (user_id))"
    ;
    $users = mysql_query($query) or die(mysql_error());

    $qry = "CREATE TABLE confirm (
    confirm_id INT(11) NOT NULL AUTO_INCREMENT,
    confirm_validator VARCHAR (32) NOT NULL,
    confirm_userid INT (11) NOT NULL default 0,
    PRIMARY KEY (confirm_id))"
    ;
    $confirm = mysql_query($qry) or die(mysql_error());
    ?>
    poi creo un'altra tabella chiamata registrazione.php
    Codice PHP:
    <?php
    session_start
    ();
    if (
    $_SESSION['log'] == 1)
    {
    echo
    "<p><b>Sei già registrato!</b><br>Per effettuare una nuova registrazione" .
    "ritorna alla Home Page ed effettua il Logout.<br><br>";
    echo
    "<a href=\"index.php\"><<< Clicca qui per ritornare alla Home Page</a></p>";
    exit();
    }
    ?>
    <html>
    <head>
    <title>Registrazione</title>
    <SCRIPT LANGUAGE="JavaScript">
    function ver()
    {
    for (i=0; i<document.forms[0].elements.length; ++i)
    if(document.forms[0].elements[i].value == "")
    {
    alert("Riempire tutti i campi!");
    document.forms[0].elements[i].focus();
    return false;
    }
    return true;
    }
    </SCRIPT>
    <SCRIPT>
    function ControllaMail()
    {
    EmailAddr = document.modulo.email.value;
    Filtro = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-]{2,})+.)+([a-zA-Z0-9]{2,})+$/;
    else
    {
    alert("Controlla l'indirizzo e-mail inserito!");
    document.modulo.email.focus();
    return false;
    }
    }
    </SCRIPT>
    </head>
    <body>
    <p>


    <form name="modulo" action="data_insert.php?action=add&type=user" method="post" onSubmit="return (ver() && ControllaMail());">
    Nome<br>
    <input name="firstname" type="text" size="25" maxlength="30"><br><br>
    Cognome<br>
    <input name="lastname" type="text" size="25" maxlength="30"><br><br>
    Username<br>
    <input name="user" type="text" size="20" maxlength="11"><br><br>
    Password<br>
    <input name="pass" type="password" maxlength="8"><br><br>
    E-mail<br>
    <input name="email" type="text" size="50" maxlength="50"><br><br>
    <input type="submit" name="Submit" value="Conferma">
    </form>
    </p>
    </body>
    </html>
    e un'altra chiamata data_insert.php

    Codice PHP:
    <?php
    $cn
    =mysql_connect("localhost","fantascudettonew","kugfutekru28") or die("Connessione non riuscita: " . mysql_error());
    mysql_select_db("my_fantascudettonew");

    $user = mysql_real_escape_string($_POST['user']);
    $user_mail = $_POST['user'];
    $pass = $_POST['pass'];
    $pass_mail = $_POST['pass'];
    $firstname = mysql_real_escape_string($_POST['firstname']);
    $lastname = mysql_real_escape_string($_POST['lastname']);

    $query = "SELECT * FROM user " .
    "WHERE user_username = '$user' " .
    "OR user_email = '$pass' ";

    $result = mysql_query($query) or die (mysql_error());
    if (
    mysql_num_rows($result) != 0)
    {
    while (
    $row = mysql_fetch_array($result))
    {
    if (
    $row['user_username'] == $user)
    {
    echo
    "<p>";
    echo
    "L'username, <b>" . $row['user_username'] . "</b> è già in uso da un altro utente, scegliere uno diverso";
    echo
    "</p>";
    }
    //email già presente nel db
    if ($row['user_email'] == $_POST['email'])
    {
    echo
    "<p>";
    echo
    "La casella E-mail, <b>" . $row['user_email'] . "</b> è già presente nel Data Base, scegliere una diversa";
    echo
    "</p>";
    }
    }
    //rimandiamo l'utente alla pagina precedente
    echo "<p>";
    echo
    "<a href=\"registrazione.php\">";
    echo
    "<<< Clicca qui per ritornare alla pagina precedente";
    echo
    "</a>";
    echo
    "</p>";
    exit();
    }
    //Se invece username e email non sono presenti nel db procediamo con l'inserimanto
    else
    {
    switch (
    $_GET['action'])
    {
    case
    "add":
    switch (
    $_GET['type'])
    {
    case
    "user":
    $sql = "INSERT INTO user
    (user_firstname,
    user_lastname,
    user_username,
    user_password,
    user_email)
    VALUES
    ('
    $firstname',
    '
    $lastname',
    '
    $user',
    $pass',
    '"
    . $_POST['email'] . "')";
    $users = mysql_query($sql) or die(mysql_error());
    $id = mysql_insert_id(); // recuperiamo l'id (contenuto nel campo user_id) dell'utente appena inserito
    break;
    }
    break;
    }
    }

    $temp = gettimeofday();
    $msec = (int) $temp["usec"];
    $msgid = md5(time() . $msec);
    $ins = "INSERT INTO confirm
    (confirm_validator,
    confirm_userid)
    VALUES
    ('
    $msgid',
    '
    $id')";
    $res = mysql_query($ins) or die(mysql_error());
    ?>
    <html>
    <head>
    <title>Inserimento dati</title>
    </head>
    <body>
    <?php
    $to
    = $_POST["email"];
    $toname = $_POST["firstname"];
    $subject = "Completa la tua registrazione";

    $boundary = "==MP_Bound_xyccr948x==";
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: multipart/alternative; boundary=\"$boundary\"\r\n";
    $headers .= "From: [email]quellochevuoi@tuoserver.it[/email]\r\n"; //inseriamo l'indirizzo email del mittente, cioè il nostro
    $html_msg = "<center>";
    $html_msg .= "<table width=\"500\" border=0 cellpadding=\"4\">";
    $html_msg .= "<tr><td align=\"center\">&nbsp;";
    $html_msg .= "</td></tr>";
    $html_msg .= "<tr><td>Questi sono i dati della tua registrazione:";
    $html_msg .= "</td></tr><tr><td>Username: <font color=\"red\">" . $user_mail . "</font>";
    $html_msg .= "</td></tr><tr><td>Password: <font color=\"red\">" . $pass_mail . "</font>";
    $html_msg .= "</td></tr><tr><td align=\"center\">&nbsp;";
    $html_msg .= "</td></tr></table></center>";
    $confirmmessage = "Salve " . $toname . ",\n\n";
    $confirmmessage .= "per completare la tua registrazione devi cliccare sul link sottostante:\n\n";
    $confirmmessage .= $html_msg . "\n\n";
    $confirmmessage .= "<a href=\"http://localhost/confirm_reg.php" .
    "?id=$msgid\">Clicca qui per confermare la tua registrazione</a>";
    $message = "This is a Multipart Message in MIME format\n";
    $message .= "--$boundary\n";
    $message .= "Content-type: text/html; charset=iso-8859-1\n";
    $message .= "Content-Transfer-Encoding: 7bit\n\n";
    $message .= $confirmmessage . "\n";
    $message .= "--$boundary--";
    $mailsent = mail($to, $subject, $message, $headers);
    if (
    $mailsent)
    {
    echo
    "Salve" . $toname . ",<br>";
    echo
    "Un messaggio è stato inviato all'indirizzo <b>" . $to . "</b> da te fornito.<br><br>";
    echo
    "IMPORTANTE:<br>";
    echo
    "Per completare la registrazione al sito devi aprire la tua casella e-mail, leggere il messaggio di conferma e cliccare sul link che troverai all'interno.<br><br>";
    } else {
    echo
    "Errore durante l'invio dell'e-mail.";
    }
    ?>
    </body>
    </html>

    IL PROBLEMA è CHE QUANDO SCRIVO NEI MIEI CAMPI (NOME,PASSWORD,E-AMIL ECC) E CLICCO SUL CAMPO CONFERMA MI ESCE QUESTO ERRORE.

    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 '', 'a')' at line 11

    Da quello che ho capito sembra un errore di differenti versioni.
    Mi potete dire la soluzione per favore perchè ci sto perdendo tantissimo tempo

    [DS] Usa i tag di codice del forum quando ne riporti ampi stralci, si legge meglio... e non esce un post kilometrico ;)
    Ultima modifica di dementialsite : 05-09-2011 alle ore 17.16.58

  2. #2
    Guest

    Predefinito

    Se guardi bene in questo pezzo di codice

    Codice PHP:
    VALUES
    ('$firstname',
    '$lastname',
    '$user',
    $pass',
    '" .
    $_POST['email'] . "')";
    prima di $pass manca l'apice.
    Prova così

    Codice PHP:
    VALUES
    ('$firstname',
    '$lastname',
    '$user',
    '$pass',
    '" . $_POST['email'] . "')";

Regole di scrittura

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