2

I have a few lines of code below to execute a prepared statement with PHP on my PostgreSQL database. However, it fails because some values are automatically converted to string while in my case, the uid and edu_year should be integers:

$query = "INSERT INTO education(uid, year, diploma, school, major) 
   VALUES ($1, $2, $3, $4, $5)";
pg_prepare($conn, "my_query", $query);
pg_execute($conn, "my_query", array($_SESSION['uid'], $_POST['edu_year'][$i],
   $_POST['edu_diploma'][$i], $_POST['edu_school'][$i], $_POST['edu_major'][$i]));

Does anyone know how to make this work, or is there another, more preferred way of achieving this?

Priidu Neemre
  • 2,813
  • 2
  • 39
  • 40
Ensom Hodder
  • 1,522
  • 5
  • 18
  • 35

1 Answers1

1

You just need to case them into integer:

pg_execute($conn, "my_query", array((int)$_SESSION['uid'], (int)$_POST['edu_year'][$i], $_POST['edu_diploma'][$i], $_POST['edu_school'][$i], $_POST['edu_major'][$i]));
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • Is it possible to echo the complete query statement for the execution ? I mean the $query replaced with correct parameters. – Ensom Hodder Apr 28 '13 at 15:37