UserPie, sicuro e "a passo coi tempi"?

Marco Bonanno

Utente Attivo
3 Lug 2012
32
0
6
Grazie mille ancora una volta, ricordo di aver visto già questa funziona in uno script, perfetto.
Mentre ci sono, ho aggiornato un po il codice riguardo l'update dei dati del profilo (Una volta riuscito con questo poi cercherò di fare lo stesso con la password, che sicuramente non cambierà molto.).

Ho fatto cosi:

Codice:
//submit
if(isset($_POST['submit'])) {

	$email = $_POST['email'];
	$gender = $_POST['gender'];
    $location = $_POST['location'];
	
    if($user->update($email,$gender,$location)) {
        redirect('account.php');
    }
}

e sempre nella pagina account.php

Codice:
<form action="" method="POST">
<table width="500" border="1" cellpadding="5">
<tbody>
<tr>
<th scope="row" width="154">Email</th>
<td width="314"><input type="text" name="email" size="30" value="<?php echo $_SESSION['email']; ?>" /></td>
</tr>
<tr>
<th scope="row">Gender</th>
<td><input type="text" name="gender" size="30" value="<?php echo $_SESSION['gender']; ?>" /></td>
</tr>
<tr>
<th scope="row">Location</th>
<td><textarea cols="20" name="location" rows="5"><?php echo $_SESSION['location']; ?></textarea></td>
</tr>
<tr>
<th scope="row"></th>
<td>
<input type="hidden" name="id" value="<?php echo $_SESSION['memberID']; ?>" />
<input type="submit" name="submit" value="Save" /></td>
</tr>
</tbody>
</table>
</form>

Invece, nella classe user ho inserito questo:

Codice:
// Update profile
    public function update($email,$gender,$location) {
        try {
        $stmt = $this->_db->prepare('UPDATE members SET email = ?, gender = ?, location = ? WHERE memberID = ? ');
        $stmt->execute(array($email,$gender,$location));
        return $stmt->fetch();
        } catch(PDOException $e) {
            echo '<p class="bg-danger">'.$e->getMessage().'</p>';
        }
    }

Sicuramente avrò sbagliato la parte finale, per quanto riguarda i "?" credo di aver seguito correttamente il tuo consiglio di sopra, penso.
 

flameseeker

Utente Attivo
27 Nov 2013
699
0
0
Per capire se c'è qualcosa che non va con la questione dei placeholders nelle query, ti basta contare il numero di punti di domanda che hai inserito..
Codice:
email = ?
gender = ? 
location = ? 
memberID = ?
..e paragonarli ai dati che stai effettivamente inviando:
PHP:
$stmt->execute(array($email,$gender,$location));

Direi che ti manca il memberID ;)


Altra cosa che noto è che il form che hai scritto ha il parametro action vuoto:
HTML:
<form action="" method="POST">

Dovrebbe contenere il nome della pagina che contiene il tuo codice di submit.
 

Marco Bonanno

Utente Attivo
3 Lug 2012
32
0
6
Ho appena provato, inserendo le variabili dell'ID utente dove di dovuto, almeno spero, ma uguale rilascia il seguente errore quando provo ad inviare i nuovi dati:

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
 

Marco Bonanno

Utente Attivo
3 Lug 2012
32
0
6
Ho eliminato per il momento il campo gender per semplicità, quindi la classe user ho fatto cosi:

Codice:
    // Update profile
    public function update($id,$email,$location) {
        try {
        $stmt = $this->_db->prepare('UPDATE members SET memberID = ?, email = ?, location = ? WHERE memberID = ? ');
        $stmt->execute(array($id,$email,$location));
        return $stmt->fetch();
        } catch(PDOException $e) {
            echo '<p class="bg-danger">'.$e->getMessage().'</p>';
        }
    }

Poi, nel file account.php

Codice:
//submit
if(isset($_POST['submit'])) {

	$email = $_POST['email'];
    $location = $_POST['location'];
	
    if($user->update($email,$location)) {
        redirect('changepw.php');
    }
}

Ho provato sia cosi che inserendo anche la variabile $id con POST id

ed il form:

Codice:
<form action="changepw.php" method="POST">
<table width="500" border="1" cellpadding="5">
<tbody>
<tr>
<th scope="row" width="154">Email</th>
<td width="314"><input type="text" name="email" size="30" value="<?php echo $_SESSION['email']; ?>" /></td>
</tr>
<tr>
<th scope="row">Location</th>
<td><textarea cols="20" name="location" rows="5"><?php echo $_SESSION['location']; ?></textarea></td>
</tr>
<tr>
<th scope="row"></th>
<td>
<input type="hidden" name="memberID" value="<?php echo $_SESSION['memberID']; ?>" />
<input type="submit" name="submit" value="Save" /></td>
</tr>
</tbody>
</table>
</form>

L'errore che mi restituisce è questo:

[05-Apr-2015 20:03:34 Europe/London] PHP Warning: Missing argument 3 for User::update(), called in /home/marcobon/public_html/last/account.php on line 15 and defined in /home/marcobon/public_html/last/classes/user.php on line 59

Quindi a quanto pare è il richiamo della funzione update che non è corretto.
 

flameseeker

Utente Attivo
27 Nov 2013
699
0
0
Perdonami, sono stato poco chiaro: quando intendevo che ti manca il memberID intendevo che nell'execute non passavi il riferimento di sessione che identifica il tuo utente.

Riporta il codice a com'era prima e modifica l'execute della query in questo modo:
PHP:
$stmt->execute(array($email,$gender,$location,$_SESSION['member_id']));

(assumendo che il valore di sessione member_id è presente da quando ti ho scritto questa replica)
 

Marco Bonanno

Utente Attivo
3 Lug 2012
32
0
6
Ciao, ritorno su questa discussione per lo stesso problema che ancora non sono riuscito a risolvere.
Ho provato in tanti modi in questi giorni, è quello che sto per incollare è l'ultimo che ho preso da esempio da un altro script.

*** Preciso che per il motivo appena detto, i nomi assegnati alle variabili indicano altro, ad esempio su $lastname io ho impostato sotto la location, fa confusione lo so.

Nella classe user:

Codice:
    // update user
    public function update_user($firstname, $lastname, $gender, $user_id){
        
        $this->username = $firstname;
        $this->location  = $lastname;
        $this->gender     = $gender;
        $this->memberID     = $user_id;
        
        $query = $this->_db->prepare("UPDATE `members` SET
        
        `username`	   = ?,
        `location`	   = ?,
        `gender`          = ?
        WHERE `memberID` 	   = ?");
        
        $query->bindValue(1, $this->username, PDO::PARAM_STR);
        $query->bindValue(2, $this->location, PDO::PARAM_STR);
        $query->bindValue(3, $this->gender, PDO::PARAM_STR);
        $query->bindValue(4, $this->memberID, PDO::PARAM_INT);
        
        try{
            $query->execute();
        }catch(PDOException $e){
            die($e->getMessage());
        }
    }

Mentre nell'ipotetica pagina account.php

Codice:
if(isset($_POST['update-submit'])){		
    require 'classes/Validation.php';
    $val = new Validation();
    $val->addSource($_POST);
    $rules_array = array(
        'first-name'=>array('type'=>'string',  'required'=>true, 'min'=>0, 'max'=>32, 'trim'=>true),
        'last-name'=>array('type'=>'string',  'required'=>true, 'min'=>0, 'max'=>32, 'trim'=>true),
		'gender'=>array('type'=>'string',  'required'=>true, 'min'=>0, 'max'=>1, 'trim'=>true));
   $val->addRules($rules_array);
   $val->run();
   $san[] = $val->sanitized;
   if(sizeof($val->errors) > 0){
      $val_errors[] = $val->errors;
   }else{
   try{  
      $firstname = $san[0]['first-name'];
	  $lastname = $san[0]['last-name'];
	  $gender = $san[0]['gender'];
       $user_id = $_SESSION['memberID'];
    
	   $user->update_user($firstname, $lastname, $gender, $user_id);
       $_SESSION['message'] = "Your profile has been updated!";
       header("Location: account.php");
       exit();   
    }catch(PDOException $e){
        $errors[] = $e->getMessage();
    }
   }
 }

Ed relativo form:

Codice:
<form action="" method="post" id="settings-form" enctype="multipart/form-data">
<div class="personal-info">
<h3>Change Profile Information</h3>

<?php if(!empty($errors)){
     echo '<p class="error">' . implode('</p><p>', $errors) . '</p>';
 } if(!empty($val_errors)){ 
     echo '<p class="error">' . implode('</p><p>', $val_errors[0]) . '</p>';
 }
    ?>
        
    <label for="first-name">Username:</label><br />
    <input type="text" name="first-name" id="first-name" value="<?php if(isset($_POST['first-name'])){
        echo htmlspecialchars($_POST['first-name'], ENT_QUOTES); } 
else { echo htmlspecialchars($_SESSION['username'], ENT_QUOTES); 
     }?>">
    
    <br />
	<label for="last-name" >Location:</label><br />
	<input type="text" name="last-name" id="last-name" value="<?php if(isset($_POST['last-name'])){
	                                                                   echo htmlspecialchars($_POST['last-name'], ENT_QUOTES);
	                                                                }else{
																	   echo htmlspecialchars($_SESSION['location'], ENT_QUOTES);
																	}?>">
    <br />
	<label for="gender">Gender:</label><br />
    <?php
       $gender 	= $_SESSION['gender'];
       $options 	= array("","M", "F");
       echo '<select name="gender" id="gender">';
       foreach($options as $option){
          if($gender == $option){
              $sel = 'selected="selected"';
          }else{
              $sel='';
          }
       echo '<option '. $sel .'>' . $option . '</option>';
        }
    ?>
       </select>
	    
  </div>
  <div class="clear"></div>
  <hr />
  <span>Update Changes:</span>
  <input type="submit" name="update-submit" value="Update">  
  </form>


Non genera nessun errore al file error_log, ma non va ugualmente :/
 
Discussioni simili
Autore Titolo Forum Risposte Data
Z Upload protetto e sicuro PHP 1
MarcoGrazia Simple cross-site requests ovvero processare richieste in modo sicuro Snippet PHP 5
W [PHP] Login sicuro al web Service SOAP PHP 20
lucofol Sono y sicuro sanno! Webdesign e Grafica 1
max_400 Come si fa a controllare se un sito è sicuro? Sicurezza e Virus 2
A Come mettere al sicuro un Cloud Server? Cloud Computing e Cloud Server 1
A problema form login sicuro PHP 0
xone Come ncludere file in modo sicuro per prevenire attacchi PHP 0
G Io ho trovato il metodo per guadagnare onesto e sicuro.entrate. Guadagnare col Sito 2
V Login sicuro al 100%, Come? PHP 6
A guadagno sicuro Guadagnare col Sito 0
max_400 Inviare Foto nello spazio web in modo sicuro PHP 3
W Streaming video Sicuro !!! Ajax 4
max_400 il login in php è sicuro? PHP 14
P info su sistema di pagamento sicuro in Joomla Joomla 0
P Pagamento sicuro Online PHP PHP 3
I login sicuro PHP 4
D guadagnare mentre si e' collegati....e' possibile e anche sicuro!!!!!!!!!!! Altri Annunci 1
S Aiuto Web Service Sicuro Programmazione 0
4 dettagli sul mio circuito banner "sicuro" Presenta il tuo Sito 0
peppoweb Il Mare sicuro è on line Discussioni Varie 0
peppoweb ICQ, AIM, MSN Messenger... al sicuro Sicurezza e Virus 0
WebmasterFioriniAndrea SQL, PHP Come passo i dati da una tabella? PHP 1
epicbrozo [Javascript] Mi potreste spiegare passo passo i passaggi di due funzioni? Javascript 0
M Consigli prima di fare qualsiasi passo Domini 8
G Come analizzare passo a passo un codice PHP ad oggetti mediante un software? PHP 6
A Creare un sito,1° passo: acquisire spazio web Hosting 7
V cm creare un semplicissimo forum cn publisher??aiutoo!spiegate passo passo PHP 2
G Facile guida passo passo per creare una formmail? HTML e CSS 1
peppoweb Proteggere PHP: guida passo passo PHP 0

Discussioni simili