使用php foreach来更新数据库

时间:2014-01-16 17:44:35

标签: php arrays foreach

我正在尝试在我的数据库中为每个用户添加占星术标志。我已经存储了他们的生日,我有一个while循环,将我需要的信息放入一个数组中,但我无法使foreach语句工作,以便我可以使用数组中的每个值来计算我需要的内容和将新信息插回数据库。这就是我到目前为止所做的:

while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
    $update = array(
        'user' => $row['username'],
        'day' => $row['bday'],
        'month' => $row['bmon']
    );
}
print_r($update);

print_r($ update)成功打印数据库中的最后一个条目,如下所示:

Array ( [user] => johnsmith [day] => 30 [month] => 6 )

我尝试过100种不同的东西,但它对我不起作用。这就是我想要做的事情:

foreach($update) {

    $astrology = "";
    if(($month==1 && $day>19)||($month==2 && $day<19)){
        $astrology = 'Aquarius';
    }else if(($month==2 && $day>18)||($month==3 && $day<21)){
        $astrology = 'Pisces';
    }else if(($month==3 && $day>20)||($month==4 && $day<20)){
        $astrology = 'Aries';
    }else if(($month==4 && $day>19)||($month==5 && $day<21)){
        $astrology = 'Taurus';
    }else if(($month==5 && $day>20)||($month==6 && $day<21)){
        $astrology = 'Gemini';
    }else if(($month==6 && $day>20)||($month==7 && $day<23)){
        $astrology = 'Cancer';
    }else if(($month==7 && $day>22)||($month==8 && $day<23)){
        $astrology = 'Leo';
    }else if(($month==8 && $day>22)||($month==9 && $day<23)){
        $astrology = 'Virgo';
    }else if(($month==9 && $day>22)||($month==10 && $day<23)){
        $astrology = 'Libra';
    }else if(($month==10 && $day>22)||($month==11 && $day<22)){
        $astrology = 'Scorpio';
    }else if(($month==11 && $day>21)||($month==12 && $day<22)){
        $astrology = 'Sagittarius';
    }else if(($month==12 && $day>21)||($month==1 && $day<20)){
        $astrology = 'Capricorn';
    }

    $query = "UPDATE useroptions SET astrology='$astrology' WHERE username='$user'" ;
    $db_conx->query($query);
    }

3 个答案:

答案 0 :(得分:1)

试试这个,

    while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {               
        $user = $row['username'];
        $day = $row['bday'];
        $month =  $row['bmon'];

                 $astrology = "";
                if(($month==1 && $day>19)||($month==2 && $day<19)){
                    $astrology = 'Aquarius';
                }else if(($month==2 && $day>18)||($month==3 && $day<21)){
                    $astrology = 'Pisces';
                }else if(($month==3 && $day>20)||($month==4 && $day<20)){
                    $astrology = 'Aries';
                }else if(($month==4 && $day>19)||($month==5 && $day<21)){
                    $astrology = 'Taurus';
                }else if(($month==5 && $day>20)||($month==6 && $day<21)){
                    $astrology = 'Gemini';
                }else if(($month==6 && $day>20)||($month==7 && $day<23)){
                    $astrology = 'Cancer';
                }else if(($month==7 && $day>22)||($month==8 && $day<23)){
                    $astrology = 'Leo';
                }else if(($month==8 && $day>22)||($month==9 && $day<23)){
                    $astrology = 'Virgo';
                }else if(($month==9 && $day>22)||($month==10 && $day<23)){
                    $astrology = 'Libra';
                }else if(($month==10 && $day>22)||($month==11 && $day<22)){
                    $astrology = 'Scorpio';
                }else if(($month==11 && $day>21)||($month==12 && $day<22)){
                    $astrology = 'Sagittarius';
                }else if(($month==12 && $day>21)||($month==1 && $day<20)){
                    $astrology = 'Capricorn';
                }

                $query = "UPDATE useroptions SET astrology='$astrology' WHERE username='$user'" ;
                $db_conx->query($query);
    }

答案 1 :(得分:1)

访问月和日时,您需要访问数组元素,并且不需要循环。我会像这样修改你的代码:

$astrology = "";
$month = $update['month'];
$day = $update['day'];

if(($month==1 && $day>19)||($month==2 && $day<19)){
    $astrology = 'Aquarius';
}elsif(($month==2 && $day>18)||($month==3 && $day<21)){
    $astrology = 'Pisces';
}elsif(($month==3 && $day>20)||($month==4 && $day<20)){
    $astrology = 'Aries';
}elsif(($month==4 && $day>19)||($month==5 && $day<21)){
    $astrology = 'Taurus';
}elsif(($month==5 && $day>20)||($month==6 && $day<21)){
    $astrology = 'Gemini';
}elsif(($month==6 && $day>20)||($month==7 && $day<23)){
    $astrology = 'Cancer';
}elsif(($month==7 && $day>22)||($month==8 && $day<23)){
    $astrology = 'Leo';
}elsif(($month==8 && $day>22)||($month==9 && $day<23)){
    $astrology = 'Virgo';
}elsif(($month==9 && $day>22)||($month==10 && $day<23)){
    $astrology = 'Libra';
}elsif(($month==10 && $day>22)||($month==11 && $day<22)){
    $astrology = 'Scorpio';
}elsif(($month==11 && $day>21)||($month==12 && $day<22)){
    $astrology = 'Sagittarius';
}elsif(($month==12 && $day>21)||($month==1 && $day<20)){
    $astrology = 'Capricorn';
}

$user = $update['user'];

$query = "UPDATE useroptions SET astrology='$astrology' WHERE username='$user'" ;
$db_conx->query($query);
}

顺便说一句,更灵活的方法是在数据库中使用本机日期戳格式,在PHP中使用日期/时间对象,而不是按照自己的方式处理月/日。

答案 2 :(得分:0)

不要使用foreach,只需像在Krish-R中那样在while循环中执行,但不要覆盖$query进行更新。

更好的方法是使用预准备语句进行更新。

<?php
// prepare the update statement
if (!($stmt = $db_conx->prepare("UPDATE useroptions SET astrology = ? WHERE username = ?")) {
    die('Error preparing statement.');
}
// create vars and bind them
$username = '';
$astrology = '';
if(!($stmt->bind_param('ss', $astrology, $username))) {
    die('Error binding variables');
}
// loop over table
while($row...) {
    $update = ...;

    /*weird long ifelses to set $astrology*/

    $username = $update['user'];

    // execute the statement for each row
    if(!($stmt->execute()) {
        error_log("Update failed for username:'{$username}' with astrology:'{$astrology}' due to " . mysql_error($db_conx));
    }
}
// close the prepared statement
$stmt->close();

?>

最快的方法可能是使用MySQL函数动态计算占星术,这样你就可以只用一个简单的查询来更新所有数据,例如:http://sqlfiddle.com/#!2/0c6fa/1

基本上是

UPDATE users SET astrology = 
 IF ((bmonth = 1 && bday > 19)||(bmonth = 2 && bday < 19),
 'Aquarius',
 IF ((bmonth = 2 && bday > 18)||(bmonth = 3 && bday < 21),
 'Pisces',
 IF ((bmonth = 3 && bday > 20)||(bmonth = 4 && bday < 20),
 'Aries',
 IF ((bmonth = 4 && bday > 19)||(bmonth = 5 && bday < 21),
 'Taurus',
 IF ((bmonth = 5 && bday > 20)||(bmonth = 6 && bday < 21),
 'Gemini',
 IF ((bmonth = 6 && bday > 20)||(bmonth = 7 && bday < 23),
 'Cancer',
 IF ((bmonth = 7 && bday > 22)||(bmonth = 8 && bday < 23),
 'Leo',
 IF ((bmonth = 8 && bday > 22)||(bmonth = 9 && bday < 23),
 'Virgo',
 IF ((bmonth = 9 && bday > 22)||(bmonth = 10 && bday < 23),
 'Libra',
 IF ((bmonth = 10 && bday > 22)||(bmonth = 11 && bday < 22),
 'Scorpio',
 IF ((bmonth = 11 && bday > 21)||(bmonth = 12 && bday < 22),
 'Sagittarius',
 IF ((bmonth = 12 && bday > 21)||(bmonth = 1 && bday < 20),
 'Capricorn',
 'Alien'
 ))))))))))));