Jump to content

[SOLVED] Help with a while loop


Darkmatter5

Recommended Posts

Here's the code

 

$querysys="SELECT system_id FROM systems";
$result=mysql_query($querysys) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
  $sysid="sys$row[system_id]";
  $querycnt="SELECT COUNT(gs_id) AS amount FROM game_systems WHERE game_id=$rowcon[game_id] AND system_id=$row[system_id]";
  $resultcnt=mysql_query($querycnt) or die(mysql_error());
  $amt=mysql_fetch_array($resultcnt);
  if(isset($_POST[$sysid])) {
    if($amt['amount']==0) { $quesys="INSERT INTO game_systems (game_id, system_id) VALUES ($rowcon[game_id], $row[system_id])"; }
  }
  else {
    if($amt['amount']==1) { $quesys="DELETE FROM game_systems WHERE game_id=$rowcon[game_id] AND system_id=$row[system_id]"; }
  }
  if(isset($quesys)) { $result=mysql_query($quesys) or die(mysql_error()); $success='yes'; }
  unset($quesys);
}

 

If I don't put the unset($quesys) line $quesys will stay set even through other elements of $row in the while loop. My condition on running a query is if $quesys is set.  How can I make $quesys only be set if ($_POST[$sysid] is set and $amt['amount'] is 0) or if ($_POST[$sysid] is not set and $amt['amount'] is 1)?

Link to comment
https://forums.phpfreaks.com/topic/153043-solved-help-with-a-while-loop/
Share on other sites

How can I make $quesys only be set if ($_POST[$sysid] is set and $amt['amount'] is 0)

if(isset($_POST[$sysid]) && $amt['amount'] == 0)

 

or if ($_POST[$sysid] is not set and $amt['amount'] is 1)?

if(isset($_POST[$sysid]) == false && $amt['amount'] == 1)

Okay here's some code I came up with to help debug.

 

$querysys="SELECT system_id FROM systems";
$result=mysql_query($querysys) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
  $sysid='sys' .$row['system_id'];
  $querycnt="SELECT COUNT(gs_id) AS amount FROM game_systems WHERE game_id=$rowcon[game_id] AND system_id=$row[system_id]";
  $resultcnt=mysql_query($querycnt) or die(mysql_error());
  $amt=mysql_fetch_array($resultcnt);
  if(isset($_POST[$sysid]) && $amt['amount']==0) { echo "$sysid is checked<br>"; $quesys="INSERT INTO game_systems (game_id, system_id) VALUES ($rowcon[game_id], $row[system_id])"; }
  if(isset($_POST[$sysid])==false && $amt['amount']==1) { echo "$sysid is not checked<br>"; $quesys="DELETE FROM game_systems WHERE game_id=$rowcon[game_id] AND system_id=$row[system_id]"; }
  echo "QUESYS: $quesys<br>QUERYCNT: $querycnt<br>AMT:$amt[amount]<p>";
  if(isset($quesys)) { $result=mysql_query($quesys) or die(mysql_error()); $success='yes'; }
  //unset($quesys);
  echo "$row[system_id] $sysid : $amt[amount]<br>";
}
echo "<p>";

 

Here's my output

QUESYS:

QUERYCNT: SELECT COUNT(gs_id) AS amount FROM game_systems WHERE game_id=4 AND system_id=1

AMT:0

 

1 sys1 : 0

QUESYS:

QUERYCNT: SELECT COUNT(gs_id) AS amount FROM game_systems WHERE game_id=4 AND system_id=2

AMT:0

 

2 sys2 : 0

QUESYS:

QUERYCNT: SELECT COUNT(gs_id) AS amount FROM game_systems WHERE game_id=4 AND system_id=3

AMT:0

 

3 sys3 : 0

QUESYS:

QUERYCNT: SELECT COUNT(gs_id) AS amount FROM game_systems WHERE game_id=4 AND system_id=4

AMT:0

 

4 sys4 : 0

QUESYS:

QUERYCNT: SELECT COUNT(gs_id) AS amount FROM game_systems WHERE game_id=4 AND system_id=5

AMT:1

 

5 sys5 : 1

QUESYS:

QUERYCNT: SELECT COUNT(gs_id) AS amount FROM game_systems WHERE game_id=4 AND system_id=6

AMT:1

 

6 sys6 : 1

sys7 is not checked

QUESYS: DELETE FROM game_systems WHERE game_id=4 AND system_id=7

QUERYCNT: SELECT COUNT(gs_id) AS amount FROM game_systems WHERE game_id=4 AND system_id=7

AMT:1

 

7 sys7 : 1

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home2/thealien/public_html/veintrade/edittitle.php on line 30

 

Line 30 in my code is the while loop.  What can be causing this error? $_POST[$sysid] are just check boxes.

It turns out the error was being caused here:

 

if(isset($quesys)) { $result=mysql_query($quesys) or die(mysql_error()); $success='yes'; }

 

$result is used before the while statement.  So I renamed $result within the while and all is right in the universe and all the peasants rejoice throughout the land!!!!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.