Current location: Hot Scripts Forums » Programming Languages » PHP » Having a problem with a large while loop. . .


Having a problem with a large while loop. . .

Reply
  #1 (permalink)  
Old 04-30-05, 10:37 PM
Spreegem Spreegem is offline
Newbie Coder
 
Join Date: Jan 2004
Location: In front of my computer
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Having a problem with a large while loop. . .

I am having a problem with a large while loop I am using in my PHP page. I get the error unexpected $ on line 476 (Last line of the page) I've narrowed the problem down to my while loop, but am not sure what in teh while loop is causing the problem.

PHP Code:

while($weap == 0)

        {
        
$we_num 'we_'.$num;
        
$weapon $_POST['weapon']['$we_num'];
        
$row 0;
        
        if(
$num == 9)
            {
            
$weap 1;
            }
        else
            {
            if(
$weapon == $num)
                {                    
                while(
$row == 0)
                    {
                    if(
$num2 26)
                        {
                        
$row 1;
                        }
                    else
                        {
                        
$tech 'weap_tech'.$num2;
                        
$type 'weap_type'.$num2;
                        
$dmg 'weap_dmg'.$num2;
                        
                        
$tech2 'we_tech'.$num2;
                        
$type2 'we_type'.$num2;
                        
$dmg2 'we_dmg'.$num2;
                        
                        
$tech3 $_SESSION['$tech2'];
                        
$type3 $_SESSION['$type2'];
                        
$dmg3 $_SESSION['$dmg2'];
                        if(
$row2[$tech] == 0)
                            {
                            
mysql_query("INSERT INTO ships ($tech$type$dmg) VALUES('$tech3', '$type3', '$dmg3') WHERE ship_name = '$ship' AND owner = '$user'") or die (mysql_error());
    
                            
$row 1;
                            
$num $num 1;
                            
$num2 $num2 1;
                            }
                        else
                            {
                            
$num2 $num2 1;
                            }
                        }
                    } 
                }
            else
                {
                
$num $num 1;
                }
            }
        } 
Reply With Quote
  #2 (permalink)  
Old 05-01-05, 04:36 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
This loop is correct in syntax and all braces are matched.
since you said the error you had pointed the last line, it means that there is a missing brace(s)

however, I noticed this:
PHP Code:

$weapon $_POST['weapon']['$we_num']; 

'$we_num' will not evaluate $we because it's inside single quotes. use double quotes to evaluate it.
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #3 (permalink)  
Old 05-01-05, 04:44 AM
Acecool's Avatar
Acecool Acecool is offline
Aspiring Coder
 
Join Date: Nov 2003
Posts: 506
Thanks: 0
Thanked 0 Times in 0 Posts
Correct syntax:
PHP Code:

while (condition)

{
     
// Do the loop...

You do:
PHP Code:

while (condition)

          {
          
// Do the loop...
          

Edit: Also you do not need double quotes...

PHP Code:

$weapon $_POST['weapon'][$we_num]; 

__________________
Check Acecoolco.com for PHP Tutorials, and other tuts
If you plan on contacting me, please read this: Legal Terms & Conditions

Last edited by Acecool; 05-01-05 at 04:48 AM.
Reply With Quote
  #4 (permalink)  
Old 05-01-05, 01:15 PM
Spreegem Spreegem is offline
Newbie Coder
 
Join Date: Jan 2004
Location: In front of my computer
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Thank You both, that solved the error, however it is not inserting the stuff into the database. Here is the updated code.

PHP Code:

    while($weap == 0)

        {
        
$we_num 'we_'.$num;
        
$weapon $_POST['weapon']["$we_num"];
        
$row 0;
        
        if(
$num == 9)
            {
            
$weap 1;
            }
        else
            {
            if(
$weapon == $num)
                {                    
                while(
$row == 0)
                    {
                    if(
$num2 26)
                        {
                        
$row 1;
                        }
                    else
                        {
                        
$tech 'weap_tech'.$num2;
                        
$type 'weap_type'.$num2;
                        
$dmg 'weap_dmg'.$num2;
                        
                        
$tech2 'we_tech'.$num2;
                        
$type2 'we_type'.$num2;
                        
$dmg2 'we_dmg'.$num2;
                        
                        
$tech3 $_SESSION["$tech2"];
                        
$type3 $_SESSION["$type2"];
                        
$dmg3 $_SESSION["$dmg2"];
                        if(
$row2["$tech"] == 0)
                            {
                            
mysql_query("INSERT INTO ships ($tech$type$dmg) VALUES('$tech3', '$type3', '$dmg3') WHERE ship_name = '$ship' AND owner = '$user'") or die (mysql_error());
    
                            
$row 1;
                            
$num $num 1;
                            
$num2 $num2 1;
                            }
                        else
                            {
                            
$num2 $num2 1;
                            }
                        }
                    } 
                }
            else
                {
                
$num $num 1;
                }
            }
        } 
Reply With Quote
  #5 (permalink)  
Old 05-01-05, 01:51 PM
alabaster_lynch alabaster_lynch is offline
Wannabe Coder
 
Join Date: Mar 2005
Posts: 103
Thanks: 0
Thanked 0 Times in 0 Posts
mysql_query("INSERT INTO ships ($tech, $type, $dmg) VALUES('$tech3', '$type3', '$dmg3') WHERE ship_name = '$ship' AND owner = '$user'") or die (mysql_error());

are $tech,$type, $dmg valid names in your db?

and also is this written so the query will write over an already existant row?
Reply With Quote
  #6 (permalink)  
Old 05-01-05, 03:03 PM
Spreegem Spreegem is offline
Newbie Coder
 
Join Date: Jan 2004
Location: In front of my computer
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
There is no row to overwrite, but. . .

$tech = weap_tech1
weap_tech1, or whatever the number is, is a valid name in the database. Is it a problem with me using a variable for the name the values should be inserted into? I have bolded it below. . .

mysql_query("INSERT INTO ships ($tech, $type, $dmg) VALUES('$tech3', '$type3', '$dmg3') WHERE ship_name = '$ship' AND owner = '$user'") or die (mysql_error());
Reply With Quote
  #7 (permalink)  
Old 05-01-05, 04:09 PM
alabaster_lynch alabaster_lynch is offline
Wannabe Coder
 
Join Date: Mar 2005
Posts: 103
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Spreegem
There is no row to overwrite, but. . .

$tech = weap_tech1
weap_tech1, or whatever the number is, is a valid name in the database. Is it a problem with me using a variable for the name the values should be inserted into? I have bolded it below. . .

mysql_query("INSERT INTO ships ($tech, $type, $dmg) VALUES('$tech3', '$type3', '$dmg3') WHERE ship_name = '$ship' AND owner = '$user'") or die (mysql_error());

I don't think it would be a problem, except I would think you would need some single quotes around them....also one think that I have to ask is why do you have conditional statements in an insert?

what does fields does your ships table have?
Reply With Quote
  #8 (permalink)  
Old 05-01-05, 04:17 PM
Acecool's Avatar
Acecool Acecool is offline
Aspiring Coder
 
Join Date: Nov 2003
Posts: 506
Thanks: 0
Thanked 0 Times in 0 Posts
Single quotes in double quotes work fine..

Why do you need a WHERE ship_name='...'?
If you want to UPDATE a row, then use the UPDATE row syntax..

INSERT INTO will create a new row, and you do not need a WHERE...

Code:
UPDATE table SET column='value', another='another value' WHERE id='##'
__________________
Check Acecoolco.com for PHP Tutorials, and other tuts
If you plan on contacting me, please read this: Legal Terms & Conditions
Reply With Quote
  #9 (permalink)  
Old 05-01-05, 05:05 PM
alabaster_lynch alabaster_lynch is offline
Wannabe Coder
 
Join Date: Mar 2005
Posts: 103
Thanks: 0
Thanked 0 Times in 0 Posts
Well I am glad someone agrees with me.....
Reply With Quote
  #10 (permalink)  
Old 05-01-05, 05:57 PM
Spreegem Spreegem is offline
Newbie Coder
 
Join Date: Jan 2004
Location: In front of my computer
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Opps. . . Yeah, I meant update, can't believe I didn't notice that.

Still not working for some reason. . . I just tried echoing $weapon, $tech, $tech3, and the others, but nothing gets echoed, none of them have values it appears.

Here's the updated code for the loop
PHP Code:

$num = 1;

$num2 = 1;        
    while($weap == 0)
        {
        $we_num = 'we_'.$num;
        $weapon = $_POST['weapon']["$we_num"];
        $row = 0;
        
        if($num == 9)
            {
            $weap = 1;
            }
        else
            {
            if($weapon == $num)
                {                    
                while($row == 0)
                    {
                    if($num2 = 26)
                        {
                        $row = 1;
                        }
                    else
                        {
                        $tech = 'weap_tech'.$num2;
                        $type = 'weap_type'.$num2;
                        $dmg = 'weap_dmg'.$num2;
                        
                        $tech2 = 'we_tech'.$num2;
                        $type2 = 'we_type'.$num2;
                        $dmg2 = 'we_dmg'.$num2;
                        
                        $tech3 = $_SESSION["$tech2"];
                        $type3 = $_SESSION["$type2"];
                        $dmg3 = $_SESSION["$dmg2"];
                        ?>
                        <p>
                        <?
                        
echo $weapon;
                        echo 
$tech;
                        echo 
$type;
                        echo 
$dmg;
                        
                        echo 
$tech3;
                        echo 
$type3;
                        echo 
$dmg3;
                        
?>
                        </p>
                        <?
                        
if($row2["$tech"] == 0)
                            {
                            
mysql_query("UPDATE ships '$tech' = '$tech3', '$type' = '$type3', '$dmg' = '$dmg3' WHERE ship_name = '$ship' AND owner = '$user'") or die (mysql_error());
    
                            
$row 1;
                            
$num $num 1;
                            
$num2 $num2 1;
                            }
                        else
                            {
                            
$num2 $num2 1;
                            }
                        }
                    } 
                }
            else
                {
                
$num $num 1;
                }
            }
        }

Last edited by Spreegem; 05-01-05 at 06:16 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
While loop jaishalg PHP 1 11-23-04 03:36 PM
SQL infinite loop problem with VB application Syed Visual Basic 2 05-07-04 10:19 AM
problem with multiple file upload and loop sita12691 PHP 0 05-03-04 03:29 PM
form problem with loop sita12691 PHP 3 05-02-04 11:51 AM


All times are GMT -5. The time now is 06:43 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.