douhui8163 2018-07-03 08:45
浏览 100
已采纳

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

       }

    ?>
  • 写回答

1条回答 默认 最新

  • douxie4583 2018-07-03 09:15
    关注

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


    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了