0

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++;
    }
}
2
  • Any error message? Try replacing $insert_result=mysql_query($insert_instance); with $insert_result=mysql_query($insert_instance) or die(mysql_error()); Commented Jul 10, 2010 at 13:38
  • How do your database looks like? Maybe a column is unique. Is the column id in instances a string or number? Commented Jul 10, 2010 at 13:39

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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
0

Maybe it should be

$insert_instance="INSERT INTO instances (id,defeated,time)
VALUES ({$row['id']},0,{$time})";

2 Comments

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.
I have tried changing the insert as you suggested Codler but no joy. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.