繁体   English   中英

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

[英]Insert form data into database with Php and PDO

使用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());

    }
    ?>

任何人都知道这里出了什么问题吗? 它像往常一样将我发送回该页面,但未向数据库添加任何内容。

这会给你你的“鲍勃”

编辑测试:将其作为单独的网址运行,忽略表格等

例如:

将文件另存为bob.php,请访问http://myurl.com/bob.php,确保includes/database-inc.php include是“ findable”的,然后发布结果

<?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-> cont-> prepare();

<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>

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());

}
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM