doujie7497 2015-08-11 08:52
浏览 35

数据库未使用PDO进行更新

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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 个人网站被恶意大量访问,怎么办
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)