pdo在执行时返回未知错误消息

tp5buhyn  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(304)

我´我用php开发了一个网站,我试图实现pdo,但它一直在返回

PDO::errorInfo():Array([0] => 00000 [1] => [2] => )

它´这是非常重要的,我可以纠正这些错误,因为这是一个重要的项目,我和我´我已经尽力了。这个连接很好,因为我有什么想法?这是连接(如果您需要):

<?php 
  $redirect ="503.php";
  $config = parse_ini_file('config.ini');
  $basehost = $config['host'];
  $basecon = $config['table'];
  $seccon = $config['sectable'];
  $basechar = $config['char'];
  $smhost = $config['SMTPhost'];
  $smauth = $config['SMTPAuth'];
  $smuser = $config['SMTPUser'];
  $smpass = $config['SMTPPass'];
  $smsec = $config['SMTPSecure'];
  $cartab = $config['cardtable'];
  $con = "mysql:host=$basehost;dbname=$basecon;charset=$basechar";
  $options = [PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,];
  try {
    $pdo = new PDO($con,$config['username'],$config['password'], $options);
  } catch (Exception $e) {
    exit(header("location:$redirect"));    
  }
?>

这是我能解决的问题´t形手柄:

if($mail->send()) { 
    $stmt = $pdo->prepare("INSERT INTO $basecon.$seccon (C_Nome,C_email,C_User,C_Pass,Card_Number,N_fiscal,D_Nasc,C_Morada,N_tel,N_tel_emer,N_cid,N_saude,Tipo_Sangue,C_Hist,Reg_Code) VALUES (:name, :email,:user,:pass,:cardnum,:fisnum,:birth,:adressnum,:telf,:emertelf,:citcard,:healthcard,:bloodstring,:histstring,:value)");
    $stmt->bindParam(array(':name', $_POST['name']), PDO::PARAM_STR);
    $stmt->bindParam(array(':email', $_POST['email']), PDO::PARAM_STR);
    $stmt->bindParam(array(':user', $_POST['username']), PDO::PARAM_STR);
    $stmt->bindParam(array(':pass', md5($_POST['password'])), PDO::PARAM_STR);
    $stmt->bindParam(array(':cardnum', $_POST['cardnumber']), PDO::PARAM_STR);
    $stmt->bindParam(array(':fisnum', $_POST['fiscalnum']), PDO::PARAM_STR);
    $stmt->bindParam(array(':birth', $_POST['birthdate']), PDO::PARAM_STR);
    $stmt->bindParam(array(':adressnum', $_POST['address']), PDO::PARAM_STR);
    $stmt->bindParam(array(':telf', $_POST['telnum']), PDO::PARAM_STR);
    $stmt->bindParam(array(':emertelf', $_POST['emertelnum']), PDO::PARAM_STR);
    $stmt->bindParam(array(':citcard', $_POST['citnumber']));
    $stmt->bindParam(array(':healthcard', $_POST['healthnumber']), PDO::PARAM_STR);
    $stmt->bindParam(array(':bloodstring', $_POST['bloodtype']), PDO::PARAM_STR);
    $stmt->bindParam(array(':histstring','Conta criada a'), PDO::PARAM_STR);
    $stmt->bindParam(array(':value', $regcode), PDO::PARAM_STR);
    if($stmt->execute()) {
        $successmsg = "Your registration was successful! <a href='login.php'>Clique aqui para efetuar login</a><br>";
    } else {
        $errormsg = "We couldn´t send you the confirmation E-mail, please check if you provided us with the correct E-mail, if so, please try again later.";
    }       
} else {
    echo "\nPDO::errorInfo():\n";
    print_r($pdo->errorInfo());
    $errormsg = '<div class="alert alert-danger" role="alert">Something went wrong, please try again later." </div>'  ;
}

电子邮件的工作原理与问题无关。谢谢你:)

2ekbmq32

2ekbmq321#

您的错误条件顺序错误。发送邮件时出错,但显示的数据库错误如预期的那样为空。适当和一致的缩进将有助于发现这类问题。
同样值得注意的是,您不需要用pdo绑定参数,并且可以使用 ? 占位符。
最后一次编辑,在数据库初始化期间启用异常,但以后不要在代码中使用它。如果数据库查询中有问题,它不会返回false,而是会引发异常。

if($mail->send()) { 
    try {
        $stmt = $pdo->prepare("INSERT INTO $basecon.$seccon (C_Nome,C_email,C_User,C_Pass,Card_Number,N_fiscal,D_Nasc,C_Morada,N_tel,N_tel_emer,N_cid,N_saude,Tipo_Sangue,C_Hist,Reg_Code) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
        $stmt->execute([
            $_POST['name'],
            $_POST['email'],
            $_POST['username'],
            password_hash($_POST['password']),
            $_POST['cardnumber'],
            $_POST['fiscalnum'],
            $_POST['birthdate'],
            $_POST['address'],
            $_POST['telnum'],
            $_POST['emertelnum'],
            $_POST['citnumber'],
            $_POST['healthnumber'],
            $_POST['bloodtype'],
            'Conta criada a',
            $regcode,
        ]) {
        $successmsg = "Your registration was successful! <a href='login.php'>Clique aqui para efetuar login</a><br>";
    } catch (\Exception $e) {
        // of course you should never catch errors just to display them, this is just a demo
        echo $e->getMessage();
        print_r($pdo->errorInfo());
        $errormsg = '<div class="alert alert-danger" role="alert">Something went wrong, please try again later." </div>';
    }
} else {
    $errormsg = "We couldn´t send you the confirmation E-mail, please check if you provided us with the correct E-mail, if so, please try again later.";
}

相关问题