Page 1 of 1

query works once but dies 2nd time

Posted: Tue Jul 19, 2005 8:02 am
by shiznatix
ok i have a loop and it runs this query

Code: Select all

$query = '
                UPDATE
                    mdl_work_interest
                SET
                    active = "1"
                WHERE
                    fk_user_id = "'.$_SESSION['userid'].'"
                AND
                    name = "'.$val.'"
            ';
in the loop it works perfectly fine the first time but on the second time through the loop i get this error

Check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE fk_user_id = "3"' at line 19

i dont understand cause session userid is "3" and $val is "Commercial and TFP"

here is the whole loop

Code: Select all

$query = '
        UPDATE
            mdl_work_interest
        SET
            active = "0"
        WHERE
            fk_user_id = "'.$_SESSION['userid'].'"
    ';

    $do_query = $db->query($query);

foreach ($_POST['worint'] as $key => $val)
    {
        $query = '
            SELECT
                *
            FROM
                mdl_work_interest
            WHERE
                name = "'.$val.'"
            AND
                fk_user_id = "'.$_SESSION['userid'].'"
        ';

        $do_query = $db->query($query);        

        if (mysql_num_rows($do_query))
        {
            $query = '
                UPDATE
                    mdl_work_interest
                SET
                    active = "1"
                WHERE
                    fk_user_id = "'.$_SESSION['userid'].'"
                AND
                    name = "'.$val.'"
            ';

            $do_query = $db->query($query);
        }
        else
        {
            $query = '
                INSERT INTO
                    mdl_work_interest
                        (
                         fk_user_id,
                         name,
                         active
                        )
                VALUES
                        (
                         "'.$_SESSION['userid'].'",
                         "'.$val.'",
                         "1"
                        )
            ';

            $do_query = $db->query($query);
        }

    }

Posted: Tue Jul 19, 2005 8:42 am
by Burrito
try escaping your '$val' with mysql_real_escape_string()

Posted: Tue Jul 19, 2005 9:03 am
by neophyte
echo your sql out to the screen on the failure. So you can see the actual query.

Posted: Tue Jul 19, 2005 9:28 am
by onion2k
A print_r() of $_POST['worint'] might help too.

Posted: Tue Jul 19, 2005 9:59 am
by shiznatix
if i had a gun right now i would shoot myself in the side to teach me a lesson. it was the query AFTER those that was messing it up and it was all because of a freakin , . obviously it is monday