News:

Wondering if this will always be free?  See why free is better.

Main Menu

Database query running twice

Started by Alanar, February 25, 2016, 01:35:20 PM

Previous topic - Next topic

Alanar

Hey, guys. I've got a simple comment system that I'm working on and I've run into a bit of trouble getting this particular query to fire only once. I know it has something to do with having multiple queries in the same page, but I'm not sure exactly which is causing the problem or how to solve it.

I have three queries in different locations in the code to handle different parts of the process the script handles.

From what I can tell, the second query that selects old comments is also pulling the third query that updates the comment. However, I can't figure out how to combine the two and make the variables work in the order I need. I'm probably just overlooking something simple. Any help is appreciated!


//Handles an update script for a different row than the following queries. It changes the query depending on two preset variables controlled by a switch. This switch will also control what will be stored in the new comment in the third query. (That part works fine)
$smcFunc['db_query']('', $query);

//Pulls the old comments and stores them in the $old_comment variable
$result = $smcFunc['db_query']('', '
        SELECT comments
        FROM {db_prefix}member_data
        WHERE id_member = ' . $id_member);

        $row = mysql_fetch_array($result);
        $old_comment = $row['comments'];

//The value from the switch above is combined with this old comment to form the value of the new comment, then I add the $updated_comment into the row.

$smcFunc['db_query']('',
        'UPDATE {db_prefix}member_data
        SET comments ="' . $updated_comment .
        '" WHERE id_member = ' . $id_member);

Alanar

Apologies. Kept at it and found the issue. The PHP was fine, but the click binding on Ajax was being duplicated when it refreshed that part of the page.

Kindred

That is one reason why you should never put database queries in the template files.

Put database queries in a file in Sources... THEN load the template -- that's how the smf templating system works...
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Advertisement: