query works once but dies 2nd time

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

query works once but dies 2nd time

Post 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);
        }

    }
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

try escaping your '$val' with mysql_real_escape_string()
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

echo your sql out to the screen on the failure. So you can see the actual query.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

A print_r() of $_POST['worint'] might help too.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
Post Reply