尝试用html表单填充sql表

Im trying to create a new row in the table 'Colaboradores' but it doesn't populate, but when i 'echo' the '$sql' with works fine along with the connection. I already check the name of the columns in the sql table. Im using MAMP as a sever

 <?php
       include("../../config.php");
       session_start();

       if($_SERVER["REQUEST_METHOD"] === "POST") {

    $nomeF = $_POST['nomeF']; 
    $nomeL = $_POST['nomeL'];
    $Prof = $_POST['Profissao'];
    $morada = $_POST['morada'];
    $cod = $_POST['cod'];
    $num = $_POST['num'];
    $mail = $_POST['mail'];
    $ordeb = $_POST['ordb'];
    $orde = $_POST['orde'];
    $dataI = $_POST['dataI'];
    $dataF = $_POST['dataF'];
    $notas1 = $_POST['notas1'];
    $notas2 = $_POST['notas2'];

    try
    {
        $db = new PDO('mysql:host=localhost;dbname=SCMMM;charset=utf8', 'root', 'root');
    }
    catch(Exception $e)
    {
        die('Error : '.$e->getMessage());
    }


    $sql = "INSERT INTO Colaboradores (NomeF, NomeL, Profissao, Morada, CodPostal, Telemovel, mail, precoh, precohmais, dataI, dataF, notas1, notas2) 
      VALUES (:nomeF, :nomeL, :Prof, :morada, :cod, :num, :mail, :ordeb, :orde, :dataI, :dataF, :notas1, :notas2)";

    $stmt = $db->prepare($sql);
    $stmt->bindValue('nomeF', $nomeF, PDO::PARAM_STR);
    $stmt->bindValue('nomeL', $nomeL, PDO::PARAM_STR);
    $stmt->bindValue('Prof', $Prof, PDO::PARAM_STR);
    $stmt->bindValue('morada', $morada, PDO::PARAM_STR);
    $stmt->bindValue('cod', $cod, PDO::PARAM_STR);
    $stmt->bindValue('num', $num, PDO::PARAM_INT);
    $stmt->bindValue('mail', $mail, PDO::PARAM_STR);
    $stmt->bindValue('ordb', $ordeb, PDO::PARAM_INT);
    $stmt->bindValue('orde', $orde, PDO::PARAM_INT);
    $stmt->bindValue('dataI', $dataI, PDO::PARAM_STR);
    $stmt->bindValue('dataF', $dataF, PDO::PARAM_STR);
    $stmt->bindValue('notas1', $notas1, PDO::PARAM_STR);
    $stmt->bindValue('notas2', $notas2, PDO::PARAM_STR);
    $stmt->execute();

       }

    ?>

You can easily improve your code :

  • Avoid symbol as ã or + in database
  • Avoid space in database (replace by _)
  • Inform yourself about OOP and PDO
  • Inform yourself about SQL injection, Prepare query, ...
  • Use a convention for your variables names, lower camelcase ? upper camelcase ? whatever but stay regular

Now try with this code

$nomeF = $_POST['nomeF']; 
$nomeL = $_POST['nomeL'];
$descP = $_POST['descP'];
$morada = $_POST['morada'];
$num = $_POST['num'];
$mail = $_POST['mail'];
$dataI = $_POST['dataI'];
$dataF = $_POST['dataF'];
$ordeb = $_POST['ordeb'];
$orde = $_POST['orde'];
$notas1 = $_POST['notas1'];
$notas2 = $_POST['notas2'];

try
{
    $db = new PDO('mysql:host=localhost;dbname=DBNAME;charset=utf8', 'USERNAME', 'PASSWORD');
}
catch(Exception $e)
{
    die('Erreur : '.$e->getMessage());
}


$sql = "INSERT INTO Colaboradores (nomeF, nomeL, descP, morada, mail, ordeb, orde, dataI, dataF, notas1, notas2) 
  VALUES (:nomeF, :nomeL, :descP, :morada, :num, :mail, :ordeb, :orde, :dataI, :dataF, :notas1, :notas2)";

$stmt = $db->prepare($sql);
$stmt->bindValue('nomeF', $nomeF, PDO::PARAM_STR);
$stmt->bindValue('nomeL', $nomeL, PDO::PARAM_STR);
$stmt->bindValue('descP', $descP, PDO::PARAM_STR);
$stmt->bindValue('morada', $morada, PDO::PARAM_STR);
$stmt->bindValue('num', $num, PDO::PARAM_INT);
$stmt->bindValue('mail', $mail, PDO::PARAM_STR);
$stmt->bindValue('ordeb', $ordeb, PDO::PARAM_STR);
$stmt->bindValue('dataI', $dataI, PDO::PARAM_STR);
$stmt->bindValue('dataF', $dataF, PDO::PARAM_STR);
$stmt->bindValue('notas1', $notas1, PDO::PARAM_STR);
$stmt->bindValue('notas2', $notas2, PDO::PARAM_STR);
$stmt->execute();

EDIT

I build your project on my computer, try to add

$error = $stmt->errorInfo();
print_r($error);

To see what's happen during your request.

On my side, I found a mismatch with the word ordeb and ordb

For example : $stmt->bindValue('ordb', $ordeb, PDO::PARAM_INT);

And can you check also the format of your date, it should be "Y-m-d H:i:s")

Note : All your columns in your table are of text type, text should be used only for long text (like in textarea), you should use varchar which allow you to save up to 255 characters (enough).