Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从具有不同值的数组更新表,其中为空字段_Php_Mysql - Fatal编程技术网

Php 从具有不同值的数组更新表,其中为空字段

Php 从具有不同值的数组更新表,其中为空字段,php,mysql,Php,Mysql,我的代码有问题,问题是数据不更新还是空的 表A year | period | code | user_id 2013 | 4 | 1231 | 2013 | 4 | 1232 | 2013 | 4 | 1233 | 2013 | 4 | 1234 | 2013 | 4 | 1235 | 表B user_id | user_name | cash A1 | AB | 10 A2 | BC

我的代码有问题,问题是数据不更新还是空的

表A

year | period | code | user_id 
2013 |      4 | 1231 | 
2013 |      4 | 1232 |
2013 |      4 | 1233 |
2013 |      4 | 1234 |
2013 |      4 | 1235 |
表B

user_id | user_name | cash
A1      | AB        |   10
A2      | BC        |    5
A3      | CD        |    7
当现金>=7时,我将把表B用户id放入表A用户id

表格结果

year | period | code | user_id 
2013 |      4 | 1231 |      10
2013 |      4 | 1232 |       7
2013 |      4 | 1233 |
2013 |      4 | 1234 |
2013 |      4 | 1235 |
这是我的密码

    $arr = array();
    $query = mysql_query("select user_id from tableB where cash >= 7");
    while ($arrs = mysql_fetch_array($query)) {
            $arr[] = $arrs[0];
            }
    $count = count($arr);
    for ($i = 0; $i < $count; $i++) {
        $sql = mysql_query("UPDATE tableA SET user_id ='$arr[$i]' WHERE year = 2013 and period = 4 and user_id IS NULL");
    }
    if ($sql) {
        echo "success";
    }
$arr=array();
$query=mysql\u query(“从表B中选择用户id,其中cash>=7”);
while($arrs=mysql\u fetch\u数组($query)){
$arr[]=$arrs[0];
}
$count=计数($arr);
对于($i=0;$i<$count;$i++){
$sql=mysql_query(“UPDATE tableA SET user_id='$arr[$i]',其中年份=2013,期间=4,用户_id为空”);
}
如果($sql){
呼应“成功”;
}

查询中的运算符应该是
=
,而不是
=>

请检查以下内容:

$arr[] = $arrs[0];
现在保存结果集的第一条记录。预计只有一个记录的现金>=7,这可能还可以,但这是一个风险的假设

for ($i = 0; $i < $count; $i++) {
        $sql = mysql_query("UPDATE tableA SET user_id ='$arr[$i]' WHERE year = 2013 and 
                            period = 4 and user_id IS NULL");
    }
($i=0;$i<$count;$i++)的
{
$sql=mysql\u query(“UPDATE tableA SET user\u id='$arr[$i]'其中year=2013和
句点=4且用户_id为空“);
}

在这里,
$arr[$i]
迭代第一条记录的字段,按顺序为您提供值“A3”、“CD”和7,您正在表上运行三个无用的更新。之后,表A中的列
user\u id
的值为7,而不是'A3',因为这是循环中的最后一个值。

我有代码的解决方案

$arr = array();
$query = mysql_query("select user_id from tableB where cash >= 7");
while ($arrs = mysql_fetch_array($query)) {
        $arr[] = $arrs[0];
        }
$q = mysql_query("select code from tableA order by code");

    $index = 0;
    while($codes = mysql_fetch_row($q)){
    $sql = mysql_query("UPDATE tableA SET user_id ='".$arr[$index++]."' WHERE code='".$codes[0]."'");
}
结果完美


谢谢大家

,您的问题到底是什么?表未更新,仍然为空是,类型不匹配(=>应为>=)。但问题仍然是一样的。我从数组中打印_r(),结果正常。我搞不懂为什么不更新呢