Jump to content

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


synstealth

Recommended Posts

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.


[code]

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.
?>
[/code]

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--]

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.