/    Sign up×
Community /Pin to ProfileBookmark

Hi,

I’m struggling with this piece of code although I know there is a simple solution, the loop only executes the code once where it should multiple times.

[code=php] $query = “SELECT * FROM practitioners WHERE approved = 1”;

$result = pg_query($query);
if (!$result) {
echo “Problem with query ” . $query . “<br/>”;
echo pg_last_error();

}

while($myrow = pg_fetch_assoc($result)) {

$idx = $myrow[‘practitioner_id’];
$form_results = $_POST[“$idx”];

if($form_results=’yes’)
{

$query = “UPDATE practitioners SET approved=’0′ where practitioner_id=’$idx'”;

$result = pg_exec($db, $query);

if (!$result) {
printf (“ERROR”);
$errormessage = pg_last_error();
echo $errormessage;
exit;
}

}

}[/code]

$idx are the values sent through the form

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@jpijperSep 09.2010 — You redefine $result. Did the same thing myself not an hour ago ?
Copy linkTweet thisAlerts:
@josephbm91Sep 09.2010 — Also, the condition

if($form_results='yes')

Will ALWAYS evaluate to true since you used the assignment operator (=) and not the equality operator (==). Make sure to change it to

if($form_results=='yes')
Copy linkTweet thisAlerts:
@RaffikkiauthorSep 09.2010 — Thanks! 100&#37; ?
Copy linkTweet thisAlerts:
@RaffikkiauthorSep 10.2010 — OMW, my brain wont work anymore.... Now I don't know why this part is only executing once while the other query executes multiple times like it shoud:[code=php] $query = "SELECT * FROM practitioners WHERE approved = 1";

$result = pg_query($query);
if (!$result) {
echo "Problem with query " . $query . "<br/>";
echo pg_last_error();
exit();
}
///////
while($myrow = pg_fetch_assoc($result)) {
///////

$id = $myrow['practitioner_id'];



$approvals = pg_escape_string($_POST['approve' . $id]);
$removals = pg_escape_string($_POST['remove' . $id]);

if ($approvals == approve) {


$query2 = "UPDATE practitioners SET approved='0' WHERE practitioner_id='$id'";

$result2 = pg_exec($db, $query2);

if (!$result2) {
printf ("ERROR");
$errormessage = pg_last_error();
echo $errormessage;

}

printf ("These values were updated in the database - %s ",$id);


///////////////Only loops once then returns SQL error///////////////
$query3 = "SELECT * FROM practitioners WHERE practitioner_id='$id'";

$result3 = pg_query($query3);
if (!$result3) {
echo "Problem with query " . $query3 . "<br/>";
echo pg_last_error();

}

while($myrow3 = pg_fetch_assoc($result3)) {
$email = $myrow3['email'];
}
printf ("Emails - %s ",$email);
////////////////////////////////

}

if ($removals == remove) {

$query3 = "DELETE FROM practitioners WHERE practitioner_id='$id'";

$result3 = pg_exec($db, $query3);

if (!$result3) {
printf ("ERROR");
$errormessage = pg_last_error();
echo $errormessage;
exit;
}

printf ("These values were removed in the database - %s ",$removals);
}
pg_close();
/////////

}[/code]
Copy linkTweet thisAlerts:
@josephbm91Sep 10.2010 — On the statements

$approvals == approve

and

$removals == remove,

what exactly are those things on the right side of the equality operators? Do you mean for them to be strings?
Copy linkTweet thisAlerts:
@RaffikkiauthorSep 10.2010 — If I understood you correctly, it's more like functions, when the form passes through a "remove", the statement checks it and then executes the code to remove the users from the database.

The same thing for the approval part, the thing is the approval database update sql works fine & loops fine but not where the email is retrieved???
Copy linkTweet thisAlerts:
@RaffikkiauthorSep 10.2010 — My brain started working again.....

the "pg_query(" should be "pg_exec("

I hate PostgreSQL.....
Copy linkTweet thisAlerts:
@josephbm91Sep 10.2010 — Glad you got it!
×

Success!

Help @Raffikki spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.14,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...