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

时间:2018-01-12 17:16:10

标签: php mysql pdo

我正在使用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"));    
  }
?>

这是我无法处理的问题:

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>'  ;
}

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

1 个答案:

答案 0 :(得分:4)

您的错误条件顺序错误。错误在于发送邮件,但是您显示的数据库错误正如预期的那样是空的。适当和一致的缩进将有助于发现这些问题。

还值得注意的是,您不需要使用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.";
}