dovs36921 2014-10-18 23:26
浏览 84
已采纳

PHP PDO插入语句不通过

I have a web application that I am building where a user is able to create a new project.

I have been dealing with this for a couple days and I am totally stumped!
I have a "New Project" button and when you click it, it calls a newProject function (and passes through the id for the folder the new project will be in. But that does not matter.)
This is what that javascript function looks like.

function newProject(parentFolder) {
    //this is here because the data type stored in the database is varchar and if the value is 'none' then there the project being created will bi in no folder.
    if(parentFolder != 'none') { //convert to nonstring
        var PF = parentFolder;
    }else {
        var PF = 'none';
    }

    //create the new project
    $.post('../../php/new_project.php', { parentFolder: PF, name: 'New Project' }, function(data) {
        alert(data);
    });

}

The connection to the new_project.php file is fine and all the variables pass through.

Here is the new_project.php file

session_start();

    if(isset($_SESSION['username'])){
        $username = $_SESSION['username'];
        if(isset($_SESSION['name'])){
            $name = $_SESSION['name'];
        }
    }

if(isset($_POST['name'])) {

    $parentFolder = $_POST['parentFolder'];
    $name = $_POST['name'];
    $date = date('Y/m/d');
    $openId;
    for ($i=0;$i<10;++$i)
            $openId.= ($r=mt_rand(0,35))<26?chr(ord('a')+$r):chr(ord('0')+$r-26);

    $conn = new PDO('mysql:host=localhost;dbname=projects', "******", "*******");

    $sql = "INSERT INTO projects (id, ProjectName, creatorOfProject, public, lastEdited, openId, deleted, parentfolder, favorite) VALUES (:id, :ProjectName, :creatorOfProject, :public, :lastEdited, :openId, :deleted, :parentfolder, :favorite)";

    $query = $conn -> prepare($sql);
    $query -> execute(array(
        ":id"                               => '',
        ":ProjectName"          => $name,
        ":creatorOfProject" => $username,
        ":puclic"                       => '',
        ":lastEdited"               => $date,
        ":openId"                       => $openId,
        ":deleted"                  => '',
        ":parentfolder"         => $parentFolder,
        ":favorite"                 => ''
    ));

}else { 
    echo 'something went wront';
}

Because I am alerting the data passed from this file it just pops up a blank alert box. There are no javascript error either.

  • 写回答

1条回答 默认 最新

  • duanpi7107 2014-10-18 23:30
    关注

    You have a typo, but it also looks like you are passing empty strings which aren't needed:

    $sql = "INSERT INTO projects (id, ProjectName, creatorOfProject, public, lastEdited, openId, deleted, parentfolder, favorite) 
            VALUES (null, :ProjectName, :creatorOfProject, null, :lastEdited, :openId, null, :parentfolder, null)";
    
    $query = $conn -> prepare($sql);
    $query -> execute(array(
        ":ProjectName"          => $name,
        ":creatorOfProject" => $username,
        //":public"                       => '',
        //    ^ Typo was there.
        ":lastEdited"               => $date,
        ":openId"                       => $openId,
        ":deleted"                  => '',
        ":parentfolder"         => $parentFolder,
        ":favorite"                 => ''
    ));
    

    I deleted the other empty strings from the array/placeholders you are using and replaced them with a null in the insert statement.

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀