简体   繁体   中英

Insert form data into database with Php and PDO

I have some trouble with inserting data into a mysql database using pdo and php. 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:

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

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

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

}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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