2014-10-18 91 views
0

我有一個Web應用程序,我正在建立一個用戶能夠創建一個新項目。PHP PDO插入語句不通過

我已經處理了幾天,我完全難住!
我有一個「新建項目」按鈕,當你點擊它時,它會調用一個newProject函數(並通過新項目所在的文件夾的id。但這並不重要。)
這就是那個javascript函數看起來像。

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); 
    }); 

} 

與new_project.php文件的連接很好,所有變量都通過。

這裏是new_project.php文件

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'; 
} 

因爲我警報從這個文件,它只是彈出一個空白的警告框傳遞的數據。 也沒有javascript錯誤。

+0

':public'和'「:puclic」' - 錯誤異常沒有設置,所以你沒有收到通知。 'setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION)' - 不要使用這個'echo'事情發生了;' - 使用try/catch。 – 2014-10-18 23:31:02

回答

0

你有一個錯字,但它也像你逝去的一些並不需要的空字符串:

$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"     => '' 
)); 

我刪除了其他空字符串從數組/您使用的佔位符,並與他們取代插入語句中的null

+0

我應該澄清,因爲數據庫中已經有一個默認設置,所以我傳遞了空白字符串。 – Kevin 2014-10-18 23:35:22

+0

傳遞'null'仍將使用默認值,並且在調試時可能更易於閱讀。 – Fluffeh 2014-10-18 23:36:37

+0

@Kevin即使你爲某些傳遞NULL值,如果你的一個綁定有一個錯字,你的代碼也會因爲它而失敗。另外,你不檢查錯誤。 – 2014-10-18 23:37:58