使用Php和PDO将表单数据插入数据库

时间:2018-05-08 09:07:52

标签: php mysql pdo crud

使用pdo和php将数据插入mysql数据库时遇到了一些麻烦。我在互联网上尝试过很多东西,但似乎没什么用。

这就是我最接近工作的地方:

数据库连接:

<?php
    class Database
 {

private static $dbName = 'gildeuitleen' ;
private static $dbHost = 'localhost' ;
private static $dbUsername = 'root';
private static $dbUserPassword = 'root';

private static $cont  = null;

public function __construct() {
    die('Init function is not allowed');
}

public static function connect()
{

   if ( null == self::$cont )
   {     
    try
    {
      //Connectie maken
      self::$cont =  new PDO( "mysql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$dbUsername, self::$dbUserPassword); 
    }
    catch(PDOException $e)
    {
      die($e->getMessage()); 
    }
   }
   return self::$cont;
}

public static function disconnect()
{
    self::$cont = null;
}
  }
  ?>

表格:

<form method="post" action="add.php">
                <input type="text" name="Barcode" placeholder="Barcode" required><br>
                <select name="Product">
                  <option value="laptop">Laptop</option>
                  <option value="beamer">Beamer</option>
                </select><br>
                <select name="Vestiging">
                  <option value="venlo">Venlo</option>
                  <option value="Venray">Venray</option>
                  <option value="Roermond">Roermond</option>
                  <option value="Weert">Weert</option>
                </select> <br>
                <input type="text" name="Datumuitgave" placeholder="JJJJ-MM-DD" required><br>
                <input type="text" name="Datumteruggave" placeholder="JJJJ-MM-DD" required><br>
                <input type="text" name="Persoonlijknummer" placeholder="Persoonlijk nummer" required><br>
                <input type="submit" value="Toevoegen">


            </form>

add.php文件:

 <?php

        include 'includes/database-inc.php';

        $pdo = Database::connect();

        try{

            $query = "INSERT INTO leningen (Barcode, Product, Vestiging, Datum uitgave, Datum teruggave, Persoonlijk nummer huurder) VALUES (?, ?, ?, ?, ?, ?)"; 

            $stmt = $pdo->prepare($query);   

            $stmt->execute();

            header('Location: OverzichtProducten.php?succes');
            exit();

            }


        catch(PDOException $exception){

            die('ERROR: ' . $exception->getMessage());

    }
    ?>

任何人都知道这里出了什么问题?它正常地将我发送回页面,但不向数据库添加任何内容。

2 个答案:

答案 0 :(得分:1)

这会给你你的&#34; bobs&#34;

修改 测试:将此作为单独的URL运行,忽略表单等

EG:

将文件另存为bob.php 访问http://myurl.com/bob.php,确保includes/database-inc.php包含&#34;可查找&#34;然后发布结果

<?php

    include 'includes/database-inc.php';

    $pdo = Database::connect();

    try{

        $query = "INSERT INTO leningen ";
        $query += " (`Barcode`, `Product`, `Vestiging`, `Datum uitgave`, `Datum teruggave`, `Persoonlijk nummer huurder`) ";
        $query += "VALUES (:barcode, :product, :vestiging, :datum_uitgave, :datum_teruggave, :persoonlijk_nummer)"; 

        $stmt = $pdo->prepare($query);   

        $stmt->execute(array(
          "barcode" => "Bob",
          "product" => "Bob",
          "vestiging" => "Bob",
          "datum_uitgave" => "Bob",
          "datum_teruggave" => "Bob",
          "persoonlijk_nummer" => "Bob"
        ));

        echo "Success";
    }
    catch(PDOException $exception){
        die('ERROR: ' . $exception->getMessage());
    }
    catch (Exception $exception) {
       die('General Error: '.$exception->getMessage());
    }
?>

还要$cont static public并拨打$ ​​pdo-&gt; cont-&gt; prepare();

答案 1 :(得分:1)

The add.php file:

$Barcode= $_POST['Barcode']; $Vestiging= $_POST['Vestiging']; $Datumuitgave= $_POST['Datumuitgave']; $Datumteruggave= $_POST['Datumteruggave']; $Persoonlijknummer= $_POST['Persoonlijknummer']; include 'includes/database-inc.php'; $pdo = Database::connect(); try{ $query = "INSERT INTO leningen (Barcode, Product, Vestiging, Datum uitgave, Datum teruggave, Persoonlijk nummer huurder) VALUES (:Barcode,Vestiging,:Datumuitgave,:Datumteruggave,:Persoonlijknummer)"; $stmt = $pdo->prepare($query); $stmt->bindparam(":Barcode",$Barcode); $stmt->bindparam(":Vestiging",$Vestiging); $stmt->bindparam(":Datumuitgave",$Datumuitgave); $stmt->bindparam(":Datumteruggave",$Datumteruggave); $stmt->bindparam(":Persoonlijknummer",$Persoonlijknummer); $stmt->execute(); header('Location: OverzichtProducten.php?succes'); exit(); } catch(PDOException $exception){ die('ERROR: ' . $exception->getMessage()); } ?>

db.apple.aggregate([{

    "$match": {
        "color": "red"
    },
},
{   
    "$lookup": {
        "from": "peach",
        "localField": "id",
        "foreignField": "id",
        "as": "peach"
    }
},
{   
    "$unwind": {
        "path": "$peach",
        "includeArrayIndex": "arrayIndex",
        "preserveNullAndEmptyArrays": false
    }
},
{
  "$match": {
        "peach.name": "sweet"
    }
},
{
    "$project": {
        id: 1,
         peach_name: "$peach.id"
     }
}]);