在PostgresSQL数据库中插入PHP表单

时间:2019-04-03 08:51:02

标签: php html postgresql

因此,我正在建立一个可以购买票等的网站。因此,我想拥有一个登录系统,我开始构建该网站,并开始使用PHP代码进行登录,但始终会收到错误Array?当我只想插入可变电子邮件和其余纯文本时,它确实起作用。

我花了整整一周的时间尝试其他方法,等等。但是我不明白为什么它不起作用。

当我使用常量而不是POST变量时,我什至会得到相同的错误...

CREATE TABLE Users(
  userId SERIAL,
  email VARCHAR(40) NOT NULL,
  password VARCHAR(30) NOT NULL,
  firstName VARCHAR(20) NOT NULL,
  lastName VARCHAR(20) NOT NULL,
  age INT NOT NULL,
  organizer BOOLEAN NOT NULL,
  region VARCHAR(30),
  favouriteGenre VARCHAR(15),
  description VARCHAR(200),

  PRIMARY KEY(userId)
);
<?php

  require 'globals.php';

  try {
    $db_conn = new PDO("pgsql:host=$db_host;dbname=$db_name", $db_user, $db_password);
  } catch (PDOException $e) {
    die("Error: ".$e->getMessage()."\n");
  }

  $email = $_POST['email'];
  $password = $_POST['password'];
  $pwdConfirm = $_POST['confirm'];
  $firsName = $_POST['firstName'];
  $lastName = $_POST['lastName'];
  $age = $_POST['age'];
  $rol = $_POST['rol'];
  $region = $_POST['region'];
  $favGenre = $_POST['favGenre'];
  $description = $_POST['description'];

  //TODO inputChecks

  $query = $db_conn->prepare('INSERT INTO users (email, password, firstName, lastName, age, organizer)
                              VALUES (:email, :password, :firstName, :lastName, :age, :organizer)');
  $query->bindParam(':email', $email, PDO::PARAM_STR, 40);
  $query->bindParam(':password', $password, PDO::PARAM_STR, 30);
  $query->bindParam(':firstName', $firstName, PDO::PARAM_STR, 20);
  $query->bindParam(':lastName', $lastName, PDO::PARAM_STR, 20);
  $query->bindParam(':age', $age, PDO::PARAM_INT);
  $query->bindParam(':organizer', $firstName, PDO::PARAM_BOOL);

  if ($query->execute()) {
    echo "success!";
  } else {
    die("Execute query error: ".$db_conn->errorInfo());
  }

  $db_conn = NULL;

我希望它可以将其插入数据库,并且不再发出错误。

1 个答案:

答案 0 :(得分:-1)

尝试一下

$query = $db_conn->prepare('INSERT INTO users (email, password, firstName, lastName, age, organizer,region, favouriteGenre, description)
                              VALUES (:email, :password, :firstName, :lastName, :age, :organizer, :region, :favouriteGenre, :description)');
  $query->bindParam(':email', $email, PDO::PARAM_STR, 40);
  $query->bindParam(':password', $pwd, PDO::PARAM_STR, 30);
  $query->bindParam(':firstName', $firstName, PDO::PARAM_STR, 20);
  $query->bindParam(':lastName', $lastName, PDO::PARAM_STR, 20);
  $query->bindParam(':age', $age, PDO::PARAM_INT);
  $query->bindParam(':organizer', $firstName, PDO::PARAM_BOOL);
  $query->bindParam(':region', $region, PDO::PARAM_STR);
  $query->bindParam(':favouriteGenre', $favGenre, PDO::PARAM_STR);
  $query->bindParam(':description',  $description, PDO::PARAM_STR);

导致错误的可能原因之一是您试图在具有9个字段的表中插入6个值。另一个导致该错误的原因是,您已将password变量定义为$ pwd,但使用了绑定参数时,$ password变量。