tags:

views:

33

answers:

2

I have the following code which should be inserting into the database x amount of times depending on the value given $row['max'] however only 1 is being inserted. Can someone please show me the error of my ways! Thanks.

$query = "SELECT * FROM challenges WHERE rate='fixed'";
$query_result = mysql_query($query);

while ($row = mysql_fetch_array($query_result)) {
    $spawn_time = preg_split('/,/', $row['time']);
    $spawn_time_results = count($spawn_time);
    $limitno = $row['max'];
    $spawn_counter = 0;

    while ($spawn_counter <= $spawn_time_results) {
        if ($spawn_time[$spawn_counter] == date("i")) {
            $time = time();
            $insert_instance = "INSERT INTO instances (id,defeated,time)
                                VALUES ('{$row['id']}',0,{$time})";
            $insert_result = mysql_query($insert_instance);
        }
        $spawn_counter++;
    }
}
A: 

Maybe it should be

$insert_instance="INSERT INTO instances (id,defeated,time)
VALUES ({$row['id']},0,{$time})";
Codler
dont think there is anything wrong with the actual insert as 1 record is being entered correctly it just aint looping.For example if the value for $limitno is 8 then it should loop round inserting 8 times but it only does one.
anadin
I have tried changing the insert as you suggested Codler but no joy. Thanks.
anadin
+1  A: 

You never actually use $limitno, so it has no effect...

PS: please use explode instead of preg_split and for instead of this while loop.

nikic
changed to a for as suggested. Took me awhile as never used that before but was pretty simple after reading up.Works as it should now with a further adjustment. Thanks
anadin