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.

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

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大