While is making me go WTF?!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bla5e
Forum Contributor
Posts: 234
Joined: Tue May 25, 2004 4:28 pm

While is making me go WTF?!

Post by bla5e »

Code: Select all

if (isset($_POST['edit'])){
    $insertpoll = pg_query($db,"UPDATE {$_POST['pub']}.polls SET question='{$_POST['question']}' WHERE id={$_POST['pollid']}");

    $option[1] = $_POST['option1'];
    $option[2] = $_POST['option2'];
    $option[3] = $_POST['option3'];
    $option[4] = $_POST['option4'];
    $option[5] = $_POST['option5'];
    $votes[1] = $_POST['votes1'];
    $votes[2] = $_POST['votes2'];
    $votes[3] = $_POST['votes3'];
    $votes[4] = $_POST['votes4'];
    $votes[5] = $_POST['votes5'];
    $i=1;
    while($i < 6) {
      echo 'while hit<br>';
      if(strlen($option[$i]) > 3){
        echo 'if hit<br>';
        $query = "UPDATE {$_POST['pub']}.polls_results SET optionid={$i}, votes={$votes[$i]}, option='{$option[$i]}' WHERE pollid={$_POST['pollid']}";
        $insert = pg_query($db,$query);
        if (!$insert){
          $smarty->assign('error', 'There was an error updating the poll.<br>'.pg_last_error());
          die();
        }
      }
      $i++;
    }
  }
it will print out both echo statements but the table is never updated and doesnt fail... WTF? :banghead:

i am using the same code to ADD / INSERT and it works fine
bla5e
Forum Contributor
Posts: 234
Joined: Tue May 25, 2004 4:28 pm

Re: While is making me go WTF?!

Post by bla5e »

If i echo out the $query
UPDATE eac.polls_results SET optionid=1, votes=555, option='testsdgsdg' WHERE pollid=8
UPDATE eac.polls_results SET optionid=2, votes=555, option='tstgdgsdg' WHERE pollid=8
bla5e
Forum Contributor
Posts: 234
Joined: Tue May 25, 2004 4:28 pm

Re: While is making me go WTF?!

Post by bla5e »

hmm so i checked and it updated.. but it makes every record the same data.. 8O
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: While is making me go WTF?!

Post by John Cartwright »

bla5e wrote:hmm so i checked and it updated.. but it makes every record the same data.. 8O
Look again at your WHERE clause. Your UPDATE statement will update ALL the results that are satisfied by pollid = 8, which I assume you will have multiple "results" per poll. Basically, you need to UPDATE based on the "results" id, and not the poll id.
bla5e
Forum Contributor
Posts: 234
Joined: Tue May 25, 2004 4:28 pm

Re: While is making me go WTF?!

Post by bla5e »

Thanks!

change of $query to

Code: Select all

$query = "UPDATE {$_POST['pub']}.polls_results SET votes={$votes[$i]}, option='{$option[$i]}' WHERE pollid={$_POST['pollid']} AND optionid={$i}";
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: While is making me go WTF?!

Post by John Cartwright »

Let me be a bit more clear. You probably want your queries to look like

Code: Select all

UPDATE eac.polls_results SET votes=555, option='testsdgsdg' WHERE pollid=8 AND optionid=1
EDIT | Yep.
bla5e
Forum Contributor
Posts: 234
Joined: Tue May 25, 2004 4:28 pm

Re: While is making me go WTF?!

Post by bla5e »

John Cartwright wrote:Let me be a bit more clear. You probably want your queries to look like

Code: Select all

UPDATE eac.polls_results SET votes=555, option='testsdgsdg' WHERE pollid=8 AND optionid=1
yah thanks
Post Reply