用PHP表单填充MySQL数据库

时间:2018-11-17 22:04:47

标签: php mysql database

我正在尝试通过php表单填充mySQL数据库。我可以访问我的数据库并使用phpmyadmin手动插入数据,但是我无法提交表单并将该数据放入我的数据库中。

require 'database.php';

 if ( !empty($_POST)) {
    // keep track validation error
    $firstNameError = null;
    $lastNameError = null;
    $teamNameError = null; 
  $photoError = null;
    // keep track post values
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $teamName = $_POST['teamName'];
   $photo = $_POST['photo'];
    // validate input
    $valid = true;


    if (empty($firstName)) {
        $firstNameError = 'Please enter First Name';
        $valid = false;
    }


    if (empty($lastName)) {
        $lastNameError = 'Please enter Last Name';
        $valid = false;
    }


    if (empty($teamName)) {
        $teamNameError = 'Please enter Team Name';
        $valid = false;

    }
   if (empty($photo)) {
        $photoError = 'Ree';
        $valid = false;
    }

    // insert data
    if ($valid) {
        $pdo = Database::connect();
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "INSERT INTO `photodatabase` (`id`, `firstName`, `lastName`, `teamName`, `photo`) VALUES ( ?, ?, ?, ?, ?)";
        $q = $pdo->prepare($sql);
        $q->execute(array($firstName, $lastName, $teamName, $photo));
        Database::disconnect();

  }

0 个答案:

没有答案