首页 文章

尝试用html表单填充sql表

提问于
浏览
0

我试图在表'Colaboradores'中创建一个新行,但它没有填充,但当我'回应''$ sql'与连接工作正常 . 我已经检查了sql表中列的名称 . 我使用MAMP作为服务器

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

       }

    ?>

1 回答

  • 1

    您可以轻松改进代码:

    • 在数据库中避免使用符号 ã+

    • 避免数据库中的空间(替换为 _

    • 告知自己OOP和PDO

    • 告知自己有关SQL注入,准备查询,...

    • 为变量名称使用约定,降低camelcase?上层来源?除了保持正常

    现在尝试使用此代码

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

    编辑

    我在我的计算机上构建你的项目,尝试添加

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

    查看您的请求期间发生了什么 .

    在我这边,我发现与 ordebordb 这个词不匹配

    例如: $stmt->bindValue('ordb', $ordeb, PDO::PARAM_INT);

    你也可以查看你的日期格式,它应该是“Y-m-d H:i:s”)

    Note : 表中的所有列都是 text 类型,文本只能用于长文本(如textarea),您应该使用 varchar ,它允许您最多保存255个字符(足够) .


相关问题