<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Problema inserimento dati in mysql]]></title><description><![CDATA[<p dir="auto">buongiorno a tutti, è il primo post in questo forum.<br />
premetto che non sono espertissimo di php e ancora meno di interazione con database sql<br />
ho un problema con un form di registrazione,<br />
la base del codice è stata presa da un articolo su un sito che parla di php.<br />
nella versione originale il codice permette di inserire un nuovo record in un db sql, contente username email e password.<br />
la pagina in php ha i diversi input di testo e vengono passati a uno script php che li elabora e li inserisce nel db</p>
<p dir="auto">io ho la necessità di aggiungere altri campi, ma se li aggiungo alla query lo script non funziona, anzi mi blocca l'apertura anche della pagina di inserimento dati, trattandosi di un "include"</p>
<p dir="auto">il codice originale dello script che scrive nel db è il seguente:</p>
<p dir="auto">codice:</p>
<p dir="auto">&lt;?php<br />
include_once 'db_connect.php';<br />
include_once 'psl-config.php';</p>
<p dir="auto">$error_msg ="";</p>
<p dir="auto"><strong>if</strong>(isset($_POST['username'], $_POST['email'], $_POST['p'])){<br />
// Sanitize and validate the data passed in<br />
$username = filter_input(INPUT_POST,'username', FILTER_SANITIZE_STRING);<br />
$email = filter_input(INPUT_POST,'email', FILTER_SANITIZE_EMAIL);<br />
$email = filter_var($email, FILTER_VALIDATE_EMAIL);<br />
<strong>if</strong>(!filter_var($email, FILTER_VALIDATE_EMAIL)){<br />
// Not a valid email<br />
$error_msg .='&lt;p class="error"&gt;Indirizzo email non valido&lt;/p&gt;';<br />
}</p>
<pre><code>$password = filter_input(INPUT_POST,'p', FILTER_SANITIZE_STRING);
**if**(strlen($password)!=128){
    // The hashed pwd should be 128 characters long.
    // If it's not, something really odd has happened
    $error_msg .='&lt;p class="error"&gt;Configurazione password errata&lt;/p&gt;';
}

// Username validity and password validity have been checked client side.
// This should should be adequate as nobody gains any advantage from
// breaking these rules.
//

$prep_stmt ="SELECT id FROM members WHERE email = ? LIMIT 1";
$stmt = $mysqli-&gt;prepare($prep_stmt);
</code></pre>
<p dir="auto">// check existing email<br />
<strong>if</strong>($stmt){<br />
$stmt-&gt;bind_param('s', $email);<br />
$stmt-&gt;execute();<br />
$stmt-&gt;store_result();</p>
<pre><code>    **if**($stmt-&gt;num_rows ==1){
        // A user with this email address already exists
        $error_msg .='&lt;p class="error"&gt;L indirizzo email inserinto risulta registarto&lt;/p&gt;';
                    $stmt-&gt;close();
    }
            $stmt-&gt;close();
}**else**{
    $error_msg .='&lt;p class="error"&gt;Database error Line 39&lt;/p&gt;';
            $stmt-&gt;close();
}

// check existing username
$prep_stmt ="SELECT id FROM members WHERE username = ? LIMIT 1";
$stmt = $mysqli-&gt;prepare($prep_stmt);

**if**($stmt){
    $stmt-&gt;bind_param('s', $username);
    $stmt-&gt;execute();
    $stmt-&gt;store_result();

            **if**($stmt-&gt;num_rows ==1){
                    // A user with this username already exists
                    $error_msg .='&lt;p class="error"&gt;Username non disponibile &lt;/p&gt;';
</code></pre>
<p dir="auto">$stmt-&gt;close();<br />
}<br />
$stmt-&gt;close();<br />
}<strong>else</strong>{<br />
$error_msg .='&lt;p class="error"&gt;Database error line 55&lt;/p&gt;';<br />
$stmt-&gt;close();<br />
}</p>
<pre><code>// TODO: 
// We'll also have to account for the situation where the user doesn't have
// rights to do registration, by checking what type of user is attempting to
// perform the operation.

**if**(empty($error_msg)){
    // Create a random salt
    //$random_salt = hash('sha512', uniqid(openssl_random_pseudo_bytes(16), TRUE)); // Did not work
    $random_salt = hash('sha512', uniqid(mt_rand(1, mt_getrandmax()),**true**));

    // Create salted password 
    $password = hash('sha512', $password . $random_salt);

    // Insert the new user into the database 
    **if**($insert_stmt = $mysqli-&gt;prepare("INSERT INTO members (username, email, password, salt) VALUES (?, ?, ?, ?)")){
        $insert_stmt-&gt;bind_param('ssss', $username, $email, $password, $random_salt);
        // Execute the prepared query.
        **if**(! $insert_stmt-&gt;execute()){
            header('Location: ../error.php?err=Registration failure: INSERT');
        }
    }
    header('Location: ./regSuccRed.php');
}
</code></pre>
<p dir="auto">}</p>
<p dir="auto">Questo è il codice modificato con l'aggiunta degli altri dati (Carattere piu grande e sottolineato)</p>
<p dir="auto">codice:</p>
<p dir="auto">&lt;?php<br />
include_once 'db_connect.php';<br />
include_once 'psl-config.php';</p>
<p dir="auto">$error_msg ="";</p>
<p dir="auto"><strong>if</strong>(isset($_POST['username'], $_POST['email'], $_POST['p'])){<br />
// Sanitize and validate the data passed in<br />
$name = filter_input(INPUT_POST,'name', FILTER_SANITIZE_STRING);<br />
$surname = filter_input(INPUT_POST,'surname', FILTER_SANITIZE_STRING);<br />
$username = filter_input(INPUT_POST,'username', FILTER_SANITIZE_STRING);<br />
$email = filter_input(INPUT_POST,'email', FILTER_SANITIZE_EMAIL);<br />
$email = filter_var($email, FILTER_VALIDATE_EMAIL);<br />
<strong>if</strong>(!filter_var($email, FILTER_VALIDATE_EMAIL)){<br />
// Not a valid email<br />
$error_msg .='&lt;p class="error"&gt;Indirizzo email non valido&lt;/p&gt;';<br />
}</p>
<pre><code>$password = filter_input(INPUT_POST,'p', FILTER_SANITIZE_STRING);
**if**(strlen($password)!=128){
    // The hashed pwd should be 128 characters long.
    // If it's not, something really odd has happened
    $error_msg .='&lt;p class="error"&gt;Configurazione password errata&lt;/p&gt;';
}

// Username validity and password validity have been checked client side.
// This should should be adequate as nobody gains any advantage from
// breaking these rules.
//

$prep_stmt ="SELECT id FROM members WHERE email = ? LIMIT 1";
$stmt = $mysqli-&gt;prepare($prep_stmt);
</code></pre>
<p dir="auto">// check existing email<br />
<strong>if</strong>($stmt){<br />
$stmt-&gt;bind_param('s', $email);<br />
$stmt-&gt;execute();<br />
$stmt-&gt;store_result();</p>
<pre><code>    **if**($stmt-&gt;num_rows ==1){
        // A user with this email address already exists
        $error_msg .='&lt;p class="error"&gt;L indirizzo email inserinto risulta registarto&lt;/p&gt;';
                    $stmt-&gt;close();
    }
            $stmt-&gt;close();
}**else**{
    $error_msg .='&lt;p class="error"&gt;Database error Line 39&lt;/p&gt;';
            $stmt-&gt;close();
}

// check existing username
$prep_stmt ="SELECT id FROM members WHERE username = ? LIMIT 1";
$stmt = $mysqli-&gt;prepare($prep_stmt);

**if**($stmt){
    $stmt-&gt;bind_param('s', $username);
    $stmt-&gt;execute();
    $stmt-&gt;store_result();

            **if**($stmt-&gt;num_rows ==1){
                    // A user with this username already exists
                    $error_msg .='&lt;p class="error"&gt;Username non disponibile &lt;/p&gt;';
</code></pre>
<p dir="auto">$stmt-&gt;close();<br />
}<br />
$stmt-&gt;close();<br />
}<strong>else</strong>{<br />
$error_msg .='&lt;p class="error"&gt;Database error line 55&lt;/p&gt;';<br />
$stmt-&gt;close();<br />
}</p>
<pre><code>// TODO: 
// We'll also have to account for the situation where the user doesn't have
// rights to do registration, by checking what type of user is attempting to
// perform the operation.

**if**(empty($error_msg)){
    // Create a random salt
    //$random_salt = hash('sha512', uniqid(openssl_random_pseudo_bytes(16), TRUE)); // Did not work
    $random_salt = hash('sha512', uniqid(mt_rand(1, mt_getrandmax()),**true**));

    // Create salted password 
    $password = hash('sha512', $password . $random_salt);

    // Insert the new user into the database 
    **if**($insert_stmt = $mysqli-&gt;prepare("INSERT INTO members (name, surname, username, email, password, salt) VALUES (?, ?, ?, ?, ?, ?)")){
        $insert_stmt-&gt;bind_param('ssss',$name, $surname, $username, $email, $password, $random_salt);
        // Execute the prepared query.
        **if**(! $insert_stmt-&gt;execute()){
            header('Location: ../error.php?err=Registration failure: INSERT');
        }
    }
    header('Location: ./regSuccRed.php');
}
</code></pre>
<p dir="auto">}</p>
<p dir="auto">Naturalmene ho provveduto a modificare la struttura della tabella in questione aggiungendo i campi in pù, è ho chiamato i campi di input della pagina di registrazione nello stesso modo in cui vengono richiamati da questo script</p>
<p dir="auto">ho provato anche a fargli eseguire una query di update ma niente da fare, non ne vuole sapere.<br />
ne sto uscendo pazzo!!! non riesco a capire perchè se aggiungo altri dati non funziona.</p>
<p dir="auto">Grazie a tutti<br />
Pozzo</p>
]]></description><link>https://dev.connect.gt/topic/216025/problema-inserimento-dati-in-mysql</link><generator>RSS for Node</generator><lastBuildDate>Fri, 27 Nov 2026 02:57:10 GMT</lastBuildDate><atom:link href="https://dev.connect.gt/topic/216025.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 24 Jul 2015 19:39:59 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problema inserimento dati in mysql on Mon, 03 Aug 2015 03:36:14 GMT]]></title><description><![CDATA[<p dir="auto">Quando si modifica uno script di solito si inseriscono i campi aggiuntivi alla fine della tabella non all'inizio. Perchè questo può causare errori nel richiamo della posizione dell'array contenente i dati, prova semplicemente a metterli alla fine e modificare lo script di conseguenza magari il tuo problema è solo questo dato che nel codice non vedo nulla di strano apparte una 20ina di passaggi inutili. La migliore cosa è creare script self-made che usare queste magagne della rete.</p>
<p dir="auto">Ciao</p>
]]></description><link>https://dev.connect.gt/post/1204313</link><guid isPermaLink="true">https://dev.connect.gt/post/1204313</guid><dc:creator><![CDATA[miwebdesign]]></dc:creator><pubDate>Mon, 03 Aug 2015 03:36:14 GMT</pubDate></item></channel></rss>