PDO语法错误或访问冲突:插入时为1064

时间:2016-02-21 14:36:48

标签: php pdo

对PDO来说相当新鲜。无论如何,我有一个名为Usage的表,其中应包含使用量,添加日期和存储在名为$ loggedInUser的变量中的用户帐号

我有以下代码:

public function __construct()
    {
        $database = new Database();
        $db = $database->dbConnection();
        $this->dbconn = $db;
        $this->loggedInUser = $_SESSION['user_session'];
    }

public function enterGasReadings($gUsage)
{
    try
    {       
        $gstmt = $this->dbconn->prepare("
            INSERT INTO gas_readings 
            (GasUsage, DateAdded, AccountNumber) 
            VALUES
            (:gUsage, NOW(), :accNum)");
        $gstmt->bindparam(":gUsage", $gUsage);
        $gstmt->bindparam(":accNum", $loggedInUser);
        $gstmt->execute();  
        return $gstmt;
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }   
}

这是我的帖子部分:

if(isset($_POST['btn-submitGasUsage']))
{
    $gUsage = strip_tags($_POST['txtGasUsage']);

    if($gUsage=="") {
        $gasError[] = "Please Enter a Number";  
    }

    try {
        if($newReading->enterGasReadings($gUsage)){ 
            $gasNotif[] = "Reading successfully entered.";
        }
    } catch (Exception $e) {
        echo $e->getMessage();

    }
    }

但是,我一直收到SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'AccountNumber' cannot be null错误 我添加了反引号,但这似乎没什么区别。

有什么想法吗? 谢谢!

0 个答案:

没有答案