Page 1 of 1

While is making me go WTF?!

Posted: Tue Jul 06, 2010 4:42 pm
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

Re: While is making me go WTF?!

Posted: Tue Jul 06, 2010 4:45 pm
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

Re: While is making me go WTF?!

Posted: Tue Jul 06, 2010 5:00 pm
by bla5e
hmm so i checked and it updated.. but it makes every record the same data.. 8O

Re: While is making me go WTF?!

Posted: Tue Jul 06, 2010 5:05 pm
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.

Re: While is making me go WTF?!

Posted: Tue Jul 06, 2010 5:08 pm
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}";

Re: While is making me go WTF?!

Posted: Tue Jul 06, 2010 5:13 pm
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.

Re: While is making me go WTF?!

Posted: Tue Jul 06, 2010 5:14 pm
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