I'm having a slight issue with prepared statements for PHP
and MySQLi
. My code takes POST
data, prepares a statement and then binds the POST data to the query. After this it executes, seemingly without error, but the statement clearly doesn't ever get executed correctly since no database changes occur.
A quick overview of my problem:
- I'm using MySQLi and prepared statements.
- I've updated some previous PHP/MySQLi code that was not using prepared statements.
- Since updating, a problem has occured with
$query->execute()
.
Code:
if($query = $this->mysqli->prepare("UPDATE article SET copy = ?, title = ?, alias = ?, description = ?, category = ?, category_id = ?, author = ?, permission = ?, mode = ?, comments = ?, revised = ?, frequency = ?, priority = ? WHERE id = ?"))
{
if
(
$query->bind_param
(
"sssssdsssssssd",
$_POST['edit_article'],
$_POST['edit_title'],
$_POST['edit_alias'],
$_POST['edit_description'],
$_POST['edit_category'],
$result['id'],
$_POST['edit_author'],
$_POST['edit_permission'],
$_POST['edit_mode'],
$_POST['edit_comments'],
$date_time,
$_POST['edit_frequency'],
$_POST['edit_priority'],
$id
)
)
{
if($query->execute())
{
echo $this->mysqli->error; // does nothing.
// These statements print to the screen successfully with the correct values.
echo $_POST['edit_frequency'] . "<br />";
echo $_POST['edit_priority'] . "<br />";
//$_SESSION['article_edited'] = true;
// die;
//exit(header("Location: " . __MANAGER__ . $_POST['edit_alias']));
}
else
{
$_SESSION['article_edited'] = false;
exit(header("Location: " . __MANAGER__ . $_POST['edit_alias']));
}
}
}
else
{
$_SESSION['article_edited'] = false;
exit(header("Location: " . __MANAGER__ . $_POST['edit_alias']));
}
Could anybody perhaps explain a better way to debug this code? It makes no sense to me since I check whether the execute()
function works with if($query->execute())
and it's successful. Assuming of course the problem does not lie with the execute function, but elsewhere. The problem can't be the POST data since it validates fine with:
if
(
$_POST['foo'] && isset($_POST['foo']) &&
$_POST['bar'] && isset($_POST['bar'])
)
{
// Statements.
}
I am clueless.
EDIT
I would like to note I echo the values of 2 particluar POST vars since those were the ones I added in when updating the code. It has since stopped working. Also note I have used print_r($_POST)
and all POST vars are set and contain values.