1

I am setting up a cron job to execute every hour. I need the script below to pull a value from each user in the database based on a field ID.

Field 18 is a dropdown with values from 0-10. In my PHP below I want to update $val based on Field 18's value. Then pass $val to Field 45 in the database for each user.

Everything works. However, the value being inserted to EVERY user is only based on the LAST user's information. The last user in the database has field 18 set to 4, so 2920 is sent to Field 45 of every user. What am I doing wrong?

Also...I realize this won't look good for potential SQL injections etc. I am just trying to get the functionality set before working on that.

<?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");

}   
}

 ?> 
4

1 回答 1

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']");
于 2013-07-17T21:37:33.547 回答