I'm sending some data through to a mysql ser开发者_Python百科ver via android in an attempt to update some details. Currently the php side looks something like this:
for($i=0; $i<(10); $i++){
for($k=0; $k<(10); $k++){
mysql_query("UPDATE sometable SET data_".$i."_".$k." = '10'
WHERE id = '".$_REQUEST['id']."'");
}
}
I have to use a loop becuase I'll be building up lots of generic types of data with the style "data_x". Unfortunately, this layout doesn't seem to update any fields in the database.
Does this method create some type of space, or just simply disrupt a complete variable when read in a statement?
Thanks
Ok, couple of things about current iteration.
- Log/output your errors!
`$res = mysql_query( $query ); if( !$res ) log( myslq_error() );/* or die or whatever */` - Do one update, not 100.
$query = "UPDATE sometable SET";
for($i=0; $i<(10); $i++){
for($k=0; $k<(10); $k++){
$query .= ' data_'.$i.'_'.$k.' = \'10\''
if( !( $k == $i && $i == 10 ) ) $query .= ',';
}
}
//see side note below
$query .= 'WHERE id = ' . mysql_real_escape_string( $_REQUEST['id'] );
$res = mysql_query( $query );
if( !$res ) do_something_with_error( mysql_error() );
100 updates can make your database/PHP angry and the last thing you want is an angry database. Further, this only does one array lookup in REQUEST, whereas the code above does 100. (O(1) * 100 is still 100).
As a side note: just because something is supposed to be sent from Android, that is no reason to expect that it does not need to be properly escaped. Remember the lessons of Bobby Tables!
I also cannot suggest strongly enough that you reconsider your schema. That may seem to be the easiest way to handle things right now, but later developers (including yourself) will wonder what the heck was supposed to be stored there. I've worked on projects like that and they were not fun. (On the other hand, I don't know your specifics, so I could be completely wrong).
This was addressing an initial copy paste error:
At a bare minimum, PHP can't parse this line:
for(ik=0; $i<(10); $i++)
Rewrite it with the dollar sign:
for($i=0; $i<(10); $i++)
Did you debug the code at all?
Is the query actually valid and does the generated field 'data_X_X' definitely exist in the table 'sometable'?
My guess is that the generated field name does not exist in your table.
精彩评论