我正在设置一个每小时执行一次的cron作业。我需要下面的脚本根据字段ID从数据库中的每个用户中提取一个值。
字段18是下拉值,值为0-10。在我的PHP下面,我想根据Field 18的值更新$ val。然后将$ val传递给每个用户的数据库中的Field 45。
一切正常。但是,插入到每个用户的值仅基于最新用户的信息。数据库中的最后一个用户将字段18设置为4,因此2920被发送到每个用户的字段45。我做错了什么?
另外......我意识到这对于潜在的SQL注入等不太好。我只是想在设置之前设置功能。</ p>
<?php
// Create connection
$con=mysqli_connect("host","username","password","database");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$getvalue = mysqli_query($con,"SELECT value FROM mydatabase WHERE thefieldid=18");
while ($row = mysqli_fetch_array($getvalue)) {
foreach($row as $rows) {
$num = $rows['value'];
if ($num == 0) {
$val = '660'; }
if ($num == 1) {
$val = '990'; }
if ($num == 2) {
$val = '1590'; }
if ($num == 3) {
$val = '1955'; }
if ($num == 4) {
$val = '2920'; }
if ($num == 5) {
$val = '3885'; }
if ($num == 6) {
$val = '4850'; }
if ($num == 7) {
$val = '5400'; }
if ($num == 8) {
$val = '3885'; }
if ($num == 9) {
$val = '5400'; }
if ($num == 10) {
$val = '7200'; }
echo $num;
mysqli_query($con, "UPDATE mydatabase SET value = $val WHERE thefieldid = 45");
}
}
?>
答案 0 :(得分:0)
您是否需要过滤更新以定位循环当前行中的特定用户?
e.g $getvalue = mysqli_query($con,"SELECT value, userid FROM
mydatabase WHERE thefieldid=18");
...
mysqli_query($con, "UPDATE mydatabase SET value = $val
WHERE thefieldid = 45 AND userid= $row['userid']");