dovs36921
2014-10-18 23:26
浏览 83

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.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题