1

This is driving me crazy... I'm trying to insert using prepared statement and the thing fails silently. Here's the code:

$sql = 'INSERT INTO payments (id_fix, name, date, comment, timestamp) VALUES (:id_fix, :name, :date, :comment, :timestamp)';
$q = $this->PDO->prepare($sql);

$a = array(
'id_fix' => $r['id'],
'name' => $r['name'],
'date' => $r['evt_date'],
'comment' => $this->comment,
'timestamp' => $r['timestamp']);

$q->execute($a) or die ('NAPAKA');

$r is from another query. I know I should do simple subquery but I'd like to solve this thing.

If I change

'id_fix' => 0

it works! If I echo $r['id'] it's a valid number.

'id_fix' => intval($r['id'])

won't work.

Any ideas? :-S

May11
  • 143
  • 9
  • 1
    Could you replace `die ('NAPAKA')` with `die ($q->errorInfo())` and report the error back here? – Mureinik Dec 27 '14 at 16:08
  • 1
    From [http://php.net/manual/en/function.intval.php] - `The integer value of var on success, or 0 on failure. Empty arrays return 0, non-empty arrays return 1.` What does it return if you print the output of the conversion? – Elliot Rodriguez Dec 27 '14 at 16:10
  • 1
    Try turning on errors, what do you get? – ykykykykyk Dec 27 '14 at 16:10

3 Answers3

1
$sql = 'INSERT INTO payments (id_fix, name, date, comment, timestamp) VALUES (:id_fix, :name, :date, :comment, :timestamp)';
$q = $this->PDO->prepare($sql);

$q->bindParam(':id_fix',$r['id_fix']);
$q->bindParam(':name',$r['name']);
$q->bindParam(':date',$r['date']);
$q->bindParam(':comment',$r['comment']);
$q->bindParam(':timestamp',$r['timestamp']);

$q->execute() or die ('NAPAKA');
  • No explanation, what did you change? I hope its not just semi colon cuz that wont help – meda Dec 27 '14 at 17:50
  • it can't be id_fix in the db table and you get $r['id'], that is why it is failing i think. I have not seen all script regarding $r –  Dec 27 '14 at 18:42
  • Can you try the above code once more. This is correct, i think it fails because you use $r['id'] and not $r['id_fix'] –  Jan 05 '15 at 14:14
0

the thing fails silently - OP

You need to check for errors, read up On Errors and error handling

try {
    $this->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = '
    INSERT INTO payments 
                (id_fix, 
                 NAME, 
                 date, 
                 comment, 
                 timestamp) 
    VALUES      (:id_fix, 
                 :name, 
                 :date, 
                 :comment, 
                 :timestamp) 
    ';
    $q = $this->PDO->prepare($sql);

    $a = array(
    'id_fix' => $r['id'],
    'name' => $r['name'],
    'date' => $r['evt_date'],
    'comment' => $this->comment,
    'timestamp' => $r['timestamp']);

    $q->execute($a);

} catch (PDOException $e) {
    echo 'Query failed: ' . $e->getMessage();
}
meda
  • 45,103
  • 14
  • 92
  • 122
0

If I change

 'id_fix' => 0

it works! If I echo $r['id'] it's a valid number.

 'id_fix' => intval($r['id'])

won't work.

A simple explanation that would account for these symptoms is this: the table you are inserting in has a PRIMARY INDEX AUTO_INCREMENT on id_fix. Sending 0 has the effect of nulling the parameter, which means the a new ID is generated automatically. And it works.

The other ids you are trying to insert are already existing, and cause an error.

I'm not saying - I can't guarantee - that this is the explanation, but it is an explanation, and without a more detailed error message, I'm afraid that's the best that can be done.

You need to activate error reporting, use PDO exceptions, and surround your queries with try/catch.

Community
  • 1
  • 1
LSerni
  • 55,617
  • 10
  • 65
  • 107