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]

Link to comment
Share on other sites

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--]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.