PHP无法将值插入数据库

时间:2018-06-11 12:49:29

标签: php wordpress

我正在为我的网站使用WordPress主题,但是定制它让我很头疼我们正在尝试使用自己编写的手而不是WordPress提供的手。

我编写了一个php脚本,应该将此表单中的值插入到wordpress数据库之外的自定义数据库中的自定义表中。然而,当我尝试运行它时,我没有收到任何错误消息,也没有数据插入到数据库中。

我的PHP代码,请注意,我已将$ user和$ pass更改为不在此处显示。我已经通过我的数据库终端测试了这个脚本中使用的登录信息,它运行正常。请在此处查看完整文件page.sign.php

if(isset($_POST['submit'])) {
    $lastname = $_POST["lname"];
    $firstname = $_POST["fname"];
    $email = $_POST["email"];
    $affiliation = $_POST["affiliation"]; 
    $country = $_POST["X"]; 
    $position = $_POST["position"]; 
    $hindex = $_POST["scholar"]; 
    $gender = $_POST["optionsRadios"]; 
    $city = $_POST["city"]; 
    $webpage = $_POST["webpage"]; 
    $newsletter = $_POST["newsletter"];



//Source https://gist.github.com/adrian-enspired/385c6830ba2932bc36a2
$host = "localhost";
$dbname = "petition";
$user = "<username>";
$pass = "<password>";
$charset = "UTF8MB4"; // if your db does not use CHARSET=UTF8MB4, you should probably be fixing that

$dsn = "mysql:host={$host};dbname={$dbname};charset={$charset}";
$options = [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, 
    PDO::ATTR_EMULATE_PREPARES => false 
  ];

  try {
    $pdo = new PDO($dsn, $user, $pass, $options);


  } catch (PDOException $e) {

    echo "<h1>Error connecting to the database </h1>";
  }


$stmt = $pdo->prepare("INSERT INTO petitioners (lastname, firstname, email, affiliation, country, position, hindex, gender, city, webpage, newsletter) 
VALUES 
(:lastname, :firstname, :email, :affiliation, :country, :position, :hindex, :gender, :city, :webpage, :newsletter)");
$stmt->bindParam(':lastname', $lastname); 
$stmt->bindParam(':firstname', $firsname);
$stmt->bindParam(':email',$email);
$stmt->bindParam(':affliation',$affiliation);
$stmt->bindParam(':country',$country);
$stmt->bindParam(':position',$position);
$stmt->bindParam(':hindex',$hindex);
$stmt->bindParam(':gender',$gender);
$stmt->bindParam(':city',$city);
$stmt->bindParam(':webpage',$webpage);
$stmt->bindParam(':newsletter',$newsletter);
$stmt->execute();


echo "<h1>Signatory succefully registered</h1>"; 

$stmt->close();
$conn->close();

}

0 个答案:

没有答案