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
bla5e
Forum Contributor
Posts: 234 Joined: Tue May 25, 2004 4:28 pm
Post
by bla5e » Tue Jul 06, 2010 4:42 pm
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?
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
Post
by bla5e » Tue Jul 06, 2010 4:45 pm
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
Post
by bla5e » Tue Jul 06, 2010 5:00 pm
hmm so i checked and it updated.. but it makes every record the same data..
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Tue Jul 06, 2010 5:05 pm
bla5e wrote: hmm so i checked and it updated.. but it makes every record the same data..
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
Post
by bla5e » Tue Jul 06, 2010 5:08 pm
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}";
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Tue Jul 06, 2010 5:13 pm
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
Post
by bla5e » Tue Jul 06, 2010 5:14 pm
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