简体   繁体   English

使用 php 问题更新 MySQL 表

[英]Updating MySQL table with php issue

I'm sending some data through to a mysql server via android in an attempt to update some details.我正在通过 android 将一些数据发送到 mysql 服务器,以尝试更新一些细节。 Currently the php side looks something like this:目前 php 端看起来像这样:

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".我必须使用循环,因为我将使用“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.好的,关于当前迭代的几件事。

  1. Log/output your errors!记录/输出您的错误!
    `$res = mysql_query( $query ); `$res = mysql_query($query); if(;$res ) log( myslq_error() );/* or die or whatever */` if(;$res) log( myslq_error() );/* 或者死或其他 */`
  2. Do one update, not 100.进行一次更新,而不是 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. 100 次更新会使您的数据库/PHP生气,而您最不想要的就是生气的数据库。 Further, this only does one array lookup in REQUEST, whereas the code above does 100. (O(1) * 100 is still 100).此外,这仅在 REQUEST 中执行一次数组查找,而上面的代码执行 100。(O(1) * 100 仍然是 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.作为旁注:仅仅因为应该从 Android 发送一些东西,所以没有理由期望它不需要正确转义。 Remember the lessons of Bobby Tables!记住 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:至少,PHP 无法解析此行:

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'?查询实际上是否有效,生成的字段“data_X_X”是否确实存在于表“sometable”中?

My guess is that the generated field name does not exist in your table.我的猜测是生成的字段名称在您的表中不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM