简体   繁体   English

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

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

I have some trouble with inserting data into a mysql database using pdo and php. 使用pdo和php将数据插入mysql数据库时遇到一些麻烦。 I've tried a lot of things on the internet but nothing seems to work. 我在互联网上尝试了很多东西,但似乎没有任何效果。

This is what I've got that's the closest to working: 这是我最接近工作的内容:

The database connection: 数据库连接:

<?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;
}
  }
  ?>

The form: 表格:

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

    }
    ?>

Anyone got any idea what's going wrong here? 任何人都知道这里出了什么问题吗? It sends me back to the page as normal, but adds nothing to the database. 它像往常一样将我发送回该页面,但未向数据库添加任何内容。

this will give you your "bobs" 这会给你你的“鲍勃”

EDIT to test : run this as a separate url ignore the form etc 编辑测试:将其作为单独的网址运行,忽略表格等

EG: 例如:

save file as bob.php visit http://myurl.com/bob.php make sure the includes/database-inc.php include is "findable" and then post the result 将文件另存为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());
    }
?>

also make $cont static public and call $pdo->cont->prepare(); 还使$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