0

I'm new to PDO and i'm developing a small e-commerce website. What i want to do with tis code is update user information. I have a login.php page from which i pass id of a user to update and i retrieve that variable with $_GET['id]. But if i'm outside of the if cycle that check the information submitted the variable is visible otherwise no.. Please can you tell me where i'm wrong with the code? The query was executed because i'm redirected to "aggiorna.php?aggiorna=ok" but the database isn't updated.

   function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);

   return $data;
   }

   //inserisci dati in tabella utenti
    if(isset($_GET['id']))
    $id = intval($_GET['id']);

     if ($_SERVER["REQUEST_METHOD"] == "POST") {

     if(isset($_POST['nome'])) {
     $nome = test_input($_POST['nome']);
     } else {
     $error_nome = "Devi inserire il nome";
     }

     if(isset($_POST['cognome'])) {
      $cognome = test_input($_POST['cognome']);
      } else {
      $error_cognome = "Devi inserire il cognome";
      }

      if(isset($_POST['cf'])) {
      $cf = test_input($_POST['cf']);
      } else {
      $error_cf = "Devi inserire il codice fiscale";
      }

     if(isset($_POST['paese'])) {
     $paese = test_input($_POST['paese']);
     } else {
     $error_paese = "Devi inserire il paese";
     }

     if(isset($_POST['telefono'])) {
      $telefono = test_input($_POST['telefono']);
      } else {
      $error_telefono = "Devi inserire il telefono";
      }

      if(isset($_POST['indirizzo'])) {
      $indirizzo = test_input($_POST['indirizzo']);
      } else {
      $error_indirizzo = "Devi inserire l'indirizzo";
      }

     if(isset($_POST['cap'])) {
     $cap = test_input($_POST['cap']);
     } else {
     $error_cap = "Devi inserire il cap";
     }

    if(isset($_POST['citta'])) {
    $citta = test_input($_POST['citta']);
    } else {
    $error_citta = "Devi inserire il citta";
    }

    if(isset($_POST['provincia'])) {
     $provincia = test_input($_POST['provincia']);
    } else {
    $error_provincia = "Devi inserire la provincia";
    }

    $aggiorno = $db->prepare("UPDATE utenti SET
                        nome = :nome,
                        cognome = :cognome,
                        cf = :cf, 
                        paese = :paese,
                        telefono = :telefono,
                        indirizzo = :indirizzo,
                        cap = :cap,
                        citta = :citta,
                        provincia = :provincia
                        WHERE id = :id");

  $aggiorno->bindParam(':nome',$nome);
  $aggiorno->bindParam(':cognome',$cognome);
  $aggiorno->bindParam(':cf',$cf);
  $aggiorno->bindParam(':paese',$paese);
  $aggiorno->bindParam(':telefono',$telefono);
  $aggiorno->bindParam(':indirizzo',$indirizzo);
  $aggiorno->bindParam(':cap',$cap);
  $aggiorno->bindParam(':citta',$citta);
  $aggiorno->bindParam(':provincia',$provincia);
 $aggiorno->bindParam(':id',$id);


 $results = $aggiorno->execute();
 if($results)
 header("Location:aggiorna.php?aggiorna=ok");
else echo "Aggiornamento non eseguito";
} 

Config file

require_once("config.php");

 try {
$db = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME .";port=" .   DB_PORT,DB_USER,DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'utf8'");
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
 } catch (Exception $e) {
echo "Could not connect to the database.";
echo $e->getMessage();
exit;
}

0 Answers0