Author Topic: Only some data is being inserted  (Read 592 times)

Offline pixlepix

  • Level 12
  • *
  • Posts: 90
  • Reputation: +0/-0
    • View Profile
Only some data is being inserted
« on: November 03, 2010, 06:07:17 PM »
Code: [Select]
<?php

include('Config.php');
for($a=0;$a<=7;$a++){
for($b=0;$b<=7;$b++){ $c $a*$b;
$vquery "INSERT INTO Map(Type, id, player, X, Y) VALUES ('wood', $c, 4, $a$b)";
$aresult mysql_query($vquery);



}
}


?>



This is supposed to fill the table with 49 rows of data, with X1-7 and Y 1-7
Code: [Select]
Type id player village X Y Cont
0 49 4 NULL 7 7 NULL
0 42 4 NULL 6 7 NULL
0 36 4 NULL 6 6 NULL
0 35 4 NULL 5 7 NULL
0 30 4 NULL 5 6 NULL
0 25 4 NULL 5 5 NULL
0 28 4 NULL 4 7 NULL
0 24 4 NULL 4 6 NULL
0 20 4 NULL 4 5 NULL
0 16 4 NULL 4 4 NULL
0 21 4 NULL 3 7 NULL
0 18 4 NULL 3 6 NULL
0 15 4 NULL 3 5 NULL
0 9 4 NULL 3 3 NULL
0 14 4 NULL 2 7 NULL
0 12 4 NULL 2 6 NULL
0 10 4 NULL 2 5 NULL
0 8 4 NULL 2 4 NULL
0 7 4 NULL 1 7 NULL
0 6 4 NULL 1 6 NULL
0 5 4 NULL 1 5 NULL
0 4 4 NULL 1 4 NULL
0 3 4 NULL 1 3 NULL
0 2 4 NULL 1 2 NULL
0 1 4 NULL 1 1 NULL
0 0 4 NULL 0 0 NULL


This is what is in the databse instead

Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: Only some data is being inserted
« Reply #1 on: November 03, 2010, 06:18:42 PM »
1) According to fors it should be 64, because it's 0-7... it should be $a=1 / $b=1
2) Query inside cycle = ugh... you have "INSERT INTO table (COLUMNS) VALUES (VALUES1), (VALUES2), (VALUES3)..." syntax, just concatenate the string and send 1 query instead of 49
3) Seems default for Type is 0 and you're inserting "wood", I'd say the Type column is set to be of a numeric type of some sort...
4) Looking at the list.... I would guess you have some sort of unique constrain set up on x+y... seems like it's working both ways which is weird, but I dunno what to do with it right now
which reminds me of:
5) No detection whether query was successful or not

BTW I would suggest to have a consistent caps style in column names (Type, Cont, X, Y versus id, player, village)
« Last Edit: November 03, 2010, 06:21:47 PM by Nox »
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Only some data is being inserted
« Reply #2 on: November 03, 2010, 07:54:52 PM »
Code: [Select]
$c = $a*8+$b;You made id auto increment, right? Then id is unique. $a*$b, which is wrong mapping of 2D to 1D, caused several ids to be the same which was disallowed by SQL.

Offline CygnusX

  • Level 24
  • *
  • Posts: 303
  • Reputation: +3/-2
    • View Profile
    • Lords of Midnight
Re: Only some data is being inserted
« Reply #3 on: November 04, 2010, 06:37:41 AM »
I'm not sure he made the field ID auto-incrementing (I would expect the ID's to print in order and not have any gaps if he did), but more than likely, the field ID has the unique property enabled.  This will prevent your insert statement if a row already exists with the matching ID.

Also, just because I'm picky, I'd recommend renaming ID to MapID and player to PlayerID and indexing both fields.  I believe this is what your intent is at least.

Offline pixlepix

  • Level 12
  • *
  • Posts: 90
  • Reputation: +0/-0
    • View Profile
Re: Only some data is being inserted
« Reply #4 on: November 04, 2010, 08:00:04 PM »
Ah, thanks. I forgot to set id as auto increment (facepalm) Thanks

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal