Ensom Hodder
Ensom Hodder

Reputation: 1548

How to pass non-string values into a prepared statement in PHP

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?

Upvotes: 2

Views: 217

Answers (1)

xdazz
xdazz

Reputation: 160843

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

Upvotes: 1

Related Questions