Jump to content

Recommended Posts

Hey!

 

I only have one question.. Shouldnt the following code work? Because it don't update the value in the database..

 

<?php
$SeUs = $_SESSION['user'];
$PoUS = $_POST['atkuser'];
mysql_query ("UPDATE characters SET temphealth =\"{$currentHealthYou}\" WHERE user =\"{$SeUs}\"");
mysql_query ("UPDATE characters SET temphealth = \"{$currentHealthEnemy}\" WHERE user =\"{$PoUs}\"");
?>

Link to comment
https://forums.phpfreaks.com/topic/89189-updating-in-a-strange-way/
Share on other sites

Assuming that you have already made a connection to MySQL and selected the database both without error, then your queries might work.

 

Better technique would enable you to see exactly what's happening and display errors (if any). Fx:

 

$query = "...." ; // define your query string
$result = mysql_query($query) or die("Error ". mysql_error(). " with query ". $query); // error display

Where do you define $currentHealthYou and $currentHealthEnemy ?

 

You might also try actually testing your code works.

 

<?php

$SeUs = $_SESSION['user'];
$PoUS = $_POST['atkuser'];
if (mysql_query ("UPDATE characters SET temphealth =\"{$currentHealthYou}\" WHERE user =\"{$SeUs}\"")) {
 echo "success";
} else {
 echo mysql_error();
}
if (mysql_query ("UPDATE characters SET temphealth = \"{$currentHealthEnemy}\" WHERE user =\"{$PoUs}\"")) {
 echo "success";
} else {
 echo mysql_error();
}

?>

Use single quotes in the query and wrapping the new values in brackets {} isnt really necessary. Table and row names should be wrapped in `s

$SeUs = $_SESSION['user'];
$PoUS = $_POST['atkuser'];
mysql_query ("UPDATE `characters` SET `temphealth` = '$currentHealthYou' WHERE `user` = '$SeUs'");
mysql_query ("UPDATE `characters` SET `temphealth` = '$currentHealthEnemy' WHERE `user` = '$PoUs'");

 

Test the queries in phpmyadmin or something... we cannot really help if we do not know what the table's structure is or what the values of $SeUs and $PoUs are supposed to be

Strings in MySQL are delimited by single quotes not double quotes. Try

<?php
$SeUs = $_SESSION['user'];
$PoUS = mysql_real_escape_string($_POST['atkuser']);
$q = "UPDATE characters SET temphealth ='$currentHealthYou' WHERE user ='$SeUs'";
mysql_query ($q) or die ("Problem with the query: $q<br>" . mysql_error());
$q = "UPDATE characters SET temphealth = '$currentHealthEnemy' WHERE user ='$PoUs'";
mysql_query ($q) or die ("Problem with the query: $q<br>" . mysql_error());
?>

 

Ken

Guys, the mysql error don't work here, because there is'nt any errors =(

 

thorpe , I tryed your Idea, and this is the result:

 

You won! The enemy now have 92 HP.successsuccess.

But still, the values didn't update.

If it helps to show u the code, here it is:

 

<?php
session_start();

mysql_connect('localhost','name','pass');
mysql_select_db('db_name');

############################################################


    function attack(){ 	
        #Ge $dbQueryHealth ett värde 
        $atkuser=$_POST['atkuser'];
$queryth =("SELECT temphealth FROM characters WHERE user='$atkuser'")or die(mysql_error());    
        #Sätt $currentHealth med värdet från databas-resursen $dbQuery 
        $currentHealthEnemy = mysql_result(mysql_query($queryth),0)or die(mysql_error());

	      #Ge $dbQueryHealth ett värde 
        $puser=$_SESSION['user'];
$queryth ="SELECT temphealth FROM characters WHERE user='$puser'"or die(mysql_error());    
        #Sätt $currentHealth med värdet från databas-resursen $dbQuery 
        $currentHealthYou = mysql_result(mysql_query($queryth),0)or die(mysql_error());  

        /*/ Ge $dbQueryExp ett värde 
        $puser=$_SESSION['user'];
   $queryexp ="SELECT exp FROM characters WHERE user='$puser'" or die(mysql_error());
        // Sätt $currentHealth med värdet från databas-resursen $dbQuery 
        $currentexp = mysql_result(mysql_query($queryexp),0)or die (mysql_error());*/ 
       
   $puser=$_SESSION['user'];
   $querydmg ="SELECT attack FROM characters WHERE user='$puser'" or die(mysql_error());
        // Sätt $currentHealth med värdet från databas-resursen $dbQuery 
        $currentdmg = mysql_result(mysql_query($querydmg),0)or die (mysql_error()); 
   
          
        $minHealth = 0;
	$maxHealth = 100; 
        $expAward = mt_rand (1, 300);   
        $healthAward = mt_rand (1, 30); 
        $winner= mt_rand (0, 1);  
         
        if($winner) { 
            if(($currentHealthEnemy - $healthAward) > $minHealth) {   
                $currentHealthEnemy -= $healthAward;
                $currentExp + expAward;
                echo "You won! The enemy now have {$currentHealthEnemy} HP.";  
            } else { 
                echo "You won! You Killed The Enemy"; 
                $currentHealthEnemy = $maxHealth;
                $currentExp + expAward; 
            } 
        } else { 
            if(($currentHealthYou - $healthAward) <= 0) { 
                echo "You Lost. The Enemy killed you."; 
                $currentHealthYou = 100; 
            } else { 
                $currentHealthYou -= $healthAward; 
                echo "You lost! You've lost {$healthAward} HP. You now have {$currentHealthYou} HP."; 
            }  
        }

        
$SeUs = $_SESSION['user'];
$PoUS = $_POST['atkuser'];
if (mysql_query ("UPDATE characters SET temphealth =\"{$currentHealthYou}\" WHERE user =\"{$SeUs}\"")) {
  echo "success";
} else {
  echo mysql_error();
}
if (mysql_query ("UPDATE characters SET temphealth = \"{$currentHealthEnemy}\" WHERE user =\"{$PoUs}\"")) {
  echo "success";
} else {
  echo mysql_error();
}

	#mysql_query ("UPDATE characters SET exp = \"{$currentExp}\" WHERE user =".$_SESSION['user']."");   
    } 
echo attack();
mysql_close();
?>
<form action="index.php" method="post">
<input type="submit" name="home" value="Back to home">

Table and column (field) names only need to be enclosed with `backticks` if you have adopted the bad habit of naming anything with a MySQL reserved word.

 

or for coding style :S

 

Sure, some of my coding style includes bad habits ... but I'm trying to improve :)

The back-tick ` is mysql specific syntax. It is not standard sql. Should you need to migrate your database to a different sql server or mysql chooses to more closely follow sql standards in the future, any thing that is relying on a back-tick to work, will need to be rewritten.

 

Short answer - don't rely on back-ticks to make bad choices in table and column names work.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.