0

The concept of my game relies on the fact that if you do not log in to take care of your pet, the pet will 'die'. Currently this is my code to measure the time past and to apply the negative modifiers to the pet's mood, hunger, and cleanliness levels.

$mod=mysqli_query($con,"select * from users where user_name='$user_name'");
while($row=mysqli_fetch_array($mod)){
      $last_login=$row['last_login'];
      $stored_login=$row['stored_login'];
      $unixtime = (strtotime($last_login)-strtotime($stored_login));
      //echo $unixtime;
      $days = ($unixtime/60/12/24);
      //echo $days;
      $time_mod = mysqli_query($con,"update users set time_mod='$days' where user_name='$user_name'");   
       }
 $pet=mysqli_query($con,"select time_mod from users where user_name='$user_name'");
 $day1=1;
 $day2=2;
 $day3=3;
 if($pet=$day1){
 $pet_mod = mysqli_query($con,"select * from user_pet where user_name='$user_name'");  
 while($row=mysqli_fetch_array($pet_mod)){
    $m1=$row['mood_level'];
    $m2=$row['hunger_level']; 
    $m3=$row['cleanliness_level']; 
    $mm1=($m1-20);  
    $mm2=($m2-20);
    $mm3=($m3-20); 
    $modm1="UPDATE user_pet SET mood_level='$mm1' WHERE user_name = '$user_name'";
    $q5=mysqli_query($con,$modm1);  
    $modm2="UPDATE user_pet SET hunger_level='$mm2' WHERE user_name = '$user_name'";
    $q5=mysqli_query($con,$modm2);
    $modm3="UPDATE user_pet SET cleanliness_level='$mm3' WHERE user_name = '$user_name'";
    $q5=mysqli_query($con,$modm3);          
             }
     }
 if($pet=$day2){
 $pet_mod = mysqli_query($con,"select * from user_pet where user_name='$user_name'");  
 while($row=mysqli_fetch_array($pet_mod)){
     $m1=$row['mood_level'];
     $m2=$row['hunger_level']; 
     $m3=$row['cleanliness_level']; 
     $mm1=($m1-40);  
     $mm2=($m2-40);
     $mm3=($m3-40); 
     $modm1="UPDATE user_pet SET mood_level='$mm1' WHERE user_name = '$user_name'";
     $q5=mysqli_query($con,$modm1);  
     $modm2="UPDATE user_pet SET hunger_level='$mm2' WHERE user_name = '$user_name'";
     $q5=mysqli_query($con,$modm2);
     $modm3="UPDATE user_pet SET cleanliness_level='$mm3' WHERE user_name = '$user_name'";
     $q5=mysqli_query($con,$modm3);          
     }
  }
  if($pet>=$day3){
  $pet_mod = mysqli_query($con,"select * from user_pet where user_name='$user_name'");  
  while($row=mysqli_fetch_array($pet_mod)){
     $m1=$row['mood_level'];
     $m2=$row['hunger_level']; 
     $m3=$row['cleanliness_level']; 
     $modm1="UPDATE user_pet SET mood_level=0 WHERE user_name = '$user_name'";
     $q5=mysqli_query($con,$modm1);  
     $modm2="UPDATE user_pet SET hunger_level=0 WHERE user_name = '$user_name'";
     $q5=mysqli_query($con,$modm2);
     $modm3="UPDATE user_pet SET cleanliness_level=0 WHERE user_name = '$user_name'";
     $q5=mysqli_query($con,$modm3);          
      }
  }

I also have a picture that is supposed to change if your pet is 'dead' or not.

<?php 
$account_r2 = mysqli_query($con,"SELECT * FROM user_pet WHERE user_name='$user_name'");
while($row = mysqli_fetch_array($account_r2)) {
          $m1=$row['mood_level'];
          $m2=$row['hunger_level']; 
          $m3=$row['cleanliness_level']; 
if($m1=0){
          echo "<img src='My Pet Monster-02.png' alt='Your Monster'>";
         }
if($m2=0){
          echo "<img src='My Pet Monster-02.png' alt='Your Monster'>"; 
         }
if($m3=0){
          echo "<img src='My Pet Monster-02.png' alt='Your Monster'>";  
         }
else{
         echo "<img src='My Pet Monster-03.png' alt='Your Monster'>";  
         }
}
     ?>

However, if the time past is greateror equal to 1 my code is making the pet's mood equal to zero(which should only happen if it's greater than or equal to 3), and if any level is equal to 0 it is still not changing the picture. The time is storing correctly- any idea why the other code isn't working? Any advice or help is greatly appreciated!

  • 1
    look up `=` vs `==` in if statements, for a start. Also `else if` rather than `if` after `if` after `if` – Daniel Jul 26 '14 at 16:02

1 Answers1

0

You're presently assigning instead of comparing with

if($m1=0) and if($m2=0) and if($m3=0)

change all your single equals signs to doubles == or === for exact matching.

Same thing for if($pet=$day1) and if($pet=$day2)


Sidenotes:

Your present code is open to SQL injection. Use prepared statements, or PDO with prepared statements.

Edit: Plus, I quote Daniel in his comment:

"Also else if rather than if after if after if"

Nota: I did post my answer before seeing his comment, but quoted him as an edit.


Add error reporting to the top of your file(s) which will help during production testing.

error_reporting(E_ALL);
ini_set('display_errors', 1);
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • I made the changes, but now even if the total is less than $day1 it's subtracting from the levels. I think it is doing this because technically the field existing is = 1 (I ran into the same issue with login validation. The solution then was ->numrows, but that was checking for the existence of a row. – Gabrielle Taylor Jul 26 '14 at 17:31