Reputation: 182
I know its something stupid that im missing here, but everytime else is executed in below code, 0 is added to my column grupa_id (its under comment #prikaz plana) when it should in fact select the max id from my other table called grupa. When 'If' occures everything is fine, the correct data is inserted in database, even in Else correct data is inserted when i put '(select MAX(id) FROM grupa)' instead of variable $dataId. Can anyone see what am i missing here?
$dataId;
$Data = $ks->getQuery("SELECT id, naziv from grupa WHERE naziv='$postNazivGrupe'");
if($Data->num_rows>0)
{
while($Row = $Data->fetch_assoc())
{
$dataId = $Row['id'];
}
}
else
{
$Data = $ks->getQuery("INSERT into grupa(oznaka_grupe, naziv, nadgrupa) VALUES ('$postOznaka_grupe', '$postNazivGrupe', '$postNazivNadgrupe')");
$dataId ="(select MAX(id) FROM grupa)";
}
##upis plana
$Data = $ks->getQuery("UPDATE plan SET grupa_id='$dataId' WHERE id=$postID");
Upvotes: 2
Views: 107
Reputation: 46900
In your else case you are generating an invalid dataId
$dataId ="(select MAX(id) FROM grupa)";
That is not a valid dataId, the result of that query will be. The previous answer was correct, don't know why it was deleted.
This makes your query to become this
"UPDATE plan SET grupa_id='(select MAX(id) FROM grupa)' WHERE id=$postID"
If that is how you intend it to be, remove the single quotes around that variable then. With your single quotes does not execute that subquery and tries to put that whole query as the value but since the field is numeric it gets converted to 0. Remove the single quotes around $dataId
in your update query.
This version is fine
"UPDATE plan SET grupa_id=(select MAX(id) FROM grupa) WHERE id=$postID"
Upvotes: 1