my related table is: slm_slmetiketler
: it's for tags and related post ids.
Columns are:slm_etiket
: tag (primary key
)slm_postids
: post ids matched with the tagslm_idadet
: quantity of post ids matched with the tag
example from current state of my table:
slm_etiket slm_postids slm_idadet
tag1 1 1
tag2 1,3 1
tag3 1,2,3 1
MY ISSUE & QUESTION
Issue is: slm_idadet
values are always updating with count of last tags' count of post ids. As you see true values must be 1
,2
and 3
.
For example the tag is : a6
in the newest post. And a6
was already tagged for 2 posts and with the new one, total posts are 3 for the tag a6
. So whole tags table's count column value is updated as 3
!
My code below gives no error. I am using store_result()
before fetch.
What am I doing wrong? (I suspect I overwrite everything with the last foreach
passage but I couldn't figure out the reason!)
MY FINDINGS
echo $a["tagpostids"].' - '.$adet.'<br>';
prints exactly what I require. (e.g 1 - 1 ; 1,3 - 2 ; 1,2,3 - 3. Correct post ids and correct count)- I've no error_log (I also used
E_USER_ERROR
outputs. Again I got no warning/error.) - within the 1st statement, I passed my output to an array; I freed and closed the 1st statement and then worked with 2nd one.
Best regards
/* code below tries to update the quantity of the post ids matches*/
// update of the count of post ids for each tag (slm_idadet)
$query = "SELECT slm_postids FROM slm_slmetiketler";
$stmt = $mysqli->prepare($query);
$result = $stmt->execute();
$res = $stmt->store_result();
$bind = $stmt->bind_result($tagpostids);
$arr = array();
while ($stmt->fetch())
{
$arr[] = array('tagpostids' => $tagpostids);
}
$stmt->free_result();
$stmt->close();
$query = "UPDATE slm_slmetiketler SET slm_idadet = ?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("i", $adet);
$b = array();
foreach ($arr as $a)
{
$b = explode(",",$a["tagpostids"]);
$adet = count($b);
$stmt->execute();
echo $a["tagpostids"].' - '.$adet.'<br>';
}
$stmt->close();