0

fairly new to PDO. Anyway i have a table called Usage which should contain the usage amount, the date it was added and the users account number which is stored in a variable called $loggedInUser

I have the following code:

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

and this is my post section:

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

    }
    }

However, i keep getting the SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'AccountNumber' cannot be null error Ive added the backticks ` in but that didnt seem to make a difference.

Any ideas? Thanks!

Mo A
  • 45
  • 1
  • 6
  • hope the syntax is correct inside the prepare statement ("accNum" or ":accNum") ? – Tushar Feb 21 '16 at 14:40
  • Edited the original post. I still get the same error it says: `SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'usage,DateAdded, AccountNumber) VALUES('6234', NOW()), NULL' at line 1` – Mo A Feb 21 '16 at 14:41
  • Don't you think that the number of opening braces in the query is not the quite the same as the number of closing ones? – Your Common Sense Feb 21 '16 at 14:43
  • Also, Usage is a reserved word, and thus adding the backticks ` would solve another syntax error in your SQL. – Your Common Sense Feb 21 '16 at 14:45
  • 2
    Added the backticks around Usage but now it says `SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2` – Mo A Feb 21 '16 at 14:46
  • I don't see any reference to `$loggedInUser`, where is this data coming from? – BugHunterUK Feb 21 '16 at 15:12
  • Ive just declared a variable above the function to hold the session. edited my original post – Mo A Feb 21 '16 at 15:39
  • 1
    `$loggedInUser = $_SESSION['user_session'];` should be `$this->loggedInUser = $_SESSION['user_session']`. You also may want to look into injecting those dependencies into the constructor as opposed to declaring them. What error are you getting exactly? I'm going to delete my answer as your post has had many edits since my answer was posted and the question has gone off topic. – BugHunterUK Feb 21 '16 at 15:56
  • @BugHunterUK The exact error im getting is `SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'AccountNumber' cannot be null` and what do you mean by injecting them into the constructor? – Mo A Feb 21 '16 at 16:06
  • This means `$loggedInUser;` is null. The database expects a value (not null) and you're passing in null. – BugHunterUK Feb 21 '16 at 16:07
  • Finally working with `$this->loggedInUser` Thanks @BugHunterUK – Mo A Feb 21 '16 at 16:08
  • No problem. In future though try not to take your origional question off topic. If you hit a new problem, post a new question :) – BugHunterUK Feb 21 '16 at 16:10
  • I will do! thank you. – Mo A Feb 21 '16 at 16:16

0 Answers0