feelay Posted February 3, 2008 Share Posted February 3, 2008 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}\""); ?> Quote Link to comment https://forums.phpfreaks.com/topic/89189-updating-in-a-strange-way/ Share on other sites More sharing options...
AndyB Posted February 3, 2008 Share Posted February 3, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/89189-updating-in-a-strange-way/#findComment-456695 Share on other sites More sharing options...
trq Posted February 3, 2008 Share Posted February 3, 2008 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(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/89189-updating-in-a-strange-way/#findComment-456696 Share on other sites More sharing options...
alecks Posted February 3, 2008 Share Posted February 3, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/89189-updating-in-a-strange-way/#findComment-456698 Share on other sites More sharing options...
kenrbnsn Posted February 3, 2008 Share Posted February 3, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/89189-updating-in-a-strange-way/#findComment-456700 Share on other sites More sharing options...
AndyB Posted February 3, 2008 Share Posted February 3, 2008 Table and row names should be wrapped in `s 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. Quote Link to comment https://forums.phpfreaks.com/topic/89189-updating-in-a-strange-way/#findComment-456702 Share on other sites More sharing options...
feelay Posted February 3, 2008 Author Share Posted February 3, 2008 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"> Quote Link to comment https://forums.phpfreaks.com/topic/89189-updating-in-a-strange-way/#findComment-456704 Share on other sites More sharing options...
alecks Posted February 3, 2008 Share Posted February 3, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/89189-updating-in-a-strange-way/#findComment-456705 Share on other sites More sharing options...
AndyB Posted February 3, 2008 Share Posted February 3, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/89189-updating-in-a-strange-way/#findComment-456709 Share on other sites More sharing options...
PFMaBiSmAd Posted February 3, 2008 Share Posted February 3, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/89189-updating-in-a-strange-way/#findComment-456796 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.