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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。