Jump to content


Photo

foreach acting weird, will not print out all but last row.


  • Please log in to reply
2 replies to this topic

#1 synstealth

synstealth
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 29 May 2006 - 10:26 PM

I created two sql tables (tours and cabinrates) and insert data to tours, then get tourid, and use it to insert the cabinrates data ..

while it inserts the cabinrates, it inserted names and tourid. but the prices will not work. it only inserted the last row and im struggling and trying to find out why and how to fix this. I might be blind. please help.



if(isset($_POST['add_tour_final_button'])){
    $cn = $_POST['tnam'];
    $ch = $_POST['thig'];
    $cm = $_POST['tmap'];
    $ci = $_POST['tinf'];
    $cc = $_POST['tcod'];
    $cd = $_POST['tdur'];
    $cs = $_POST['tsta'];
    $ce = $_POST['tend'];
    $ca = $_POST['taut'];
                        
    $cabins = array('Inside'     => $_POST['Inside Cabin'],
                    'Ocean'     => $_POST['Ocean View'],
                    'Junior'     => $_POST['Junior Suite'],
                    'Penth'        => $_POST['Penthouse']);
    
// INSERT INTO tours
    connect();                                // this is a fucntion to connect to sql
    $check = sql("SELECT * FROM tours WHERE tourcode='$cc'");   // another function to run mysql_query
    if($tchk = @mysql_fetch_assoc($check)){
        echo 'tour info already inserted!<br />';
        $inserted = true;
    }else{    
        $query = "INSERT INTO tours (tourname, tourhighlights, tourmap, tourinfo, tourcode, duration, startdate, enddate, tourauth) 
                  VALUES ('$cn','$ch','$cm','$ci','$cc','$cd','$cs','$ce','$ca')";
        $addtour = sql($query);
        @mysql_affected_rows($addtour);
        $inserted = true;
    }
    if($inserted){
        $gettourid = sql("SELECT tourid FROM tours WHERE tourcode='$cc'");
        while($trow = @mysql_fetch_array($gettourid))    {$tourid = $trow['tourid'];}
    }
    echo 'your tourID is: '.$tourid.'<br />';
    
// INSERT INTO cabinrates                 <--- this section below is where Im having the problem. 
    $num = 0;
    foreach($cabins as $size => $cost){
        $query2 = "INSERT INTO cabinrates (cabinname, cabinprice, tourid) VALUES ('$size', '$cost', '$tourid')";
        $addcabins = sql($query2);
        @mysql_affected_rows($addcabins);
        $num = $num + 1;
    }
    echo 'you have added '.$num.' rows of cabinrates<br />';
    mysql_close();

blah. nothing here.
?>



#2 ldsmike88

ldsmike88
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts
  • LocationFlorida

Posted 30 May 2006 - 03:17 AM

What do you mean it only inserts the last row? Point out in what part of your script you think the error is in.

#3 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 30 May 2006 - 06:32 AM

Here's some markup
that may help
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
// INSERT INTO cabinrates
[!--coloro:#FF9966--][span style=\"color:#FF9966\"][!--/coloro--]$num = 0; //You don't need a counter for a FOREACH[!--colorc--][/span][!--/colorc--]
foreach($cabins as $size => $cost){
$query2 = "INSERT INTO cabinrates (cabinname, cabinprice, tourid) VALUES ('$size', '$cost', '$tourid')";
$addcabins = sql($query2);
[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]//I don't see the point of this next line especially if you're supressing the errors, just take it out
@mysql_affected_rows($addcabins);[!--colorc--][/span][!--/colorc--]
//Try echo out every SQL statement that's made just to check you're doing all of them
echo $query2 . "<br />\n";
[!--coloro:#FF9966--][span style=\"color:#FF9966\"][!--/coloro--]$num = $num + 1;[!--colorc--][/span][!--/colorc--]
}
[/quote]

another idea would be to put curly braces around you're variables when you use them in the query
[!--coloro:#3333FF--][span style=\"color:#3333FF\"][!--/coloro--]$query2 = "INSERT INTO cabinrates (cabinname, cabinprice, tourid) VALUES ('{$size}', '{$cost}', '{$tourid}')";[!--colorc--][/span][!--/colorc--]

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users