Twister1004 Posted November 16, 2008 Share Posted November 16, 2008 Hi everyone! Yes, I'm getting more advanced than I was before, and I'm happy. =), I have you all to thank =). However, I'm here with another problem. This is something I wanted to play around with. I wanted to make a calculator (Somewhat) and I'm using my database to do this. Objective: I'm trying to make it so that when submit is clicked, it will take the value from the database and subtract it by the number that was entered and the number that was altered back into the database. (Confusing I know, but hey, you're talking to me ). Ex. Database has a value of 58. take 58 from the database and subtract it by a variable entered. Lets say 8. Now take 50 - 8 and make it as another variable. Now Update the database with the new number. But I guess I'm kinda stumped. <?php error_reporting(E_ALL); mysql_connect("localhost", "root", "root"); mysql_select_db("fable"); ?> <html> <head> </head> <body> <?php $strength = $_POST['strength']; $magic = $_POST['magic']; $skill = $_POST['skill']; $sql = mysql_query("SELECT `strength`, `magic`, `skill`, `general` FROM `exp` WHERE `ID` = '1'"); $get = mysql_fetch_row($sql) ?> You currently need this much EXP in these stats:<br/> Strength: <?php echo $get[0] ?><br/> Skill: <?php echo $get[2] ?><br/> Magic: <?php echo $get[1] ?><br/> <form method = "POST" action=""> <table> <tr> <td>Strength: </td> <td> <input type ="text" name = "strength" value = "0" id = "strength" /></td> </tr> <tr> <td>Magic: </td> <td><input type ="text" name = "magic" value = "0" id = "magic" /></td> </tr> <tr> <td>Skill:</td> <td><input type ="text" name = "skill" value = "0" id = "skill" /></td> </tr> <tr> <td><input type = "submit" name = "submit" value = "Submit skill stats" /></td> </tr> </table> <?php if (!isset($_POST['submit'])){ //This isnt finished. It was just as a test. However it doesnt want to work. $replace1 = $strength - $get[0]; $replace2 = $magic - $get[1]; $replace3 = $skill - $get[2]; $update = mysql_query("UPDATE `exp` SET '.$replace1.' = `strength`, '.$replace2.' = `magic`, '.$replace3.' = `skill` WHERE `ID` = '1' "); } ?> </form> </body> </html> After I wrote this I though, "what if negative was sent into database.... Would it subtract it then?" Thanks for reading, posting, helping, hinting, etc. -Twister Quote Link to comment Share on other sites More sharing options...
ratcateme Posted November 16, 2008 Share Posted November 16, 2008 try changing your query to this `strength` = '.$replace1.', `magic` = '.$replace2.', `skill` = '.$replace3.' Scott. Quote Link to comment Share on other sites More sharing options...
Twister1004 Posted November 16, 2008 Author Share Posted November 16, 2008 Nothing happend, However, when I did change it to that, the rest of my code after that query went all screwy like. It acted like it was all still in PHP. What i mean by screwy was the ending with </html> was not highlighed like it was supposed to be. <?php error_reporting(E_ALL); mysql_connect("localhost", "root", "root"); mysql_select_db("fable"); ?> <html> <head> </head> <body> <?php $strength = $_POST['strength']; $magic = $_POST['magic']; $skill = $_POST['skill']; $sql = mysql_query("SELECT `strength`, `magic`, `skill`, `general` FROM `exp` WHERE `ID` = '1'"); $get = mysql_fetch_row($sql) ?> You currently need this much EXP in these stats:<br/> Strength: <?php echo $get[0] ?><br/> Skill: <?php echo $get[2] ?><br/> Magic: <?php echo $get[1] ?><br/> <form method = "POST" action=""> <table> <tr> <td>Strength: </td> <td> <input type ="text" name = "strength" value = "0" id = "strength" /></td> </tr> <tr> <td>Magic: </td> <td><input type ="text" name = "magic" value = "0" id = "magic" /></td> </tr> <tr> <td>Skill:</td> <td><input type ="text" name = "skill" value = "0" id = "skill" /></td> </tr> <tr> <td><input type = "submit" name = "submit" value = "Submit skill stats" /></td> </tr> </table> <?php if (!isset($_POST['submit'])){ $replace1 = $strength - $get[0]; $replace2 = $magic - $get[1]; $replace3 = $skill - $get[2]; $update = mysql_query("UPDATE `exp` SET `strength` = '.$replace1.', `magic` = '.$replace2.', `skill` = '.$replace3.' WHERE `ID` = '1' "); } ?> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Twister1004 Posted November 16, 2008 Author Share Posted November 16, 2008 Figured out something. No matter what I've been typing in, it will not go into the database at all. It acts like it doesnt do anything. Quote Link to comment Share on other sites More sharing options...
gaza165 Posted November 16, 2008 Share Posted November 16, 2008 try echoing out your replace variables and see what values they give, if they are correct... then we can sort out the update statement... are the replace variables giving the correct values?? Quote Link to comment Share on other sites More sharing options...
Twister1004 Posted November 16, 2008 Author Share Posted November 16, 2008 Its not even echoing. I think it might not be going through the if statement then O_o. Give me a min. Ill try to re write it. Quote Link to comment Share on other sites More sharing options...
.josh Posted November 16, 2008 Share Posted November 16, 2008 $update = mysql_query("UPDATE `exp` SET `strength` = $replace1, `magic` = $replace2, `skill` = $replace3 WHERE `ID` = '1' "); Quote Link to comment Share on other sites More sharing options...
.josh Posted November 16, 2008 Share Posted November 16, 2008 if (!isset($_POST['submit'])){ remove the ! you're telling it to update it if it's NOT set (as in, you haven't clicked submit yet). Quote Link to comment Share on other sites More sharing options...
gaza165 Posted November 16, 2008 Share Posted November 16, 2008 <?php if (isset($_POST['submit'])){ $replace1 = $strength - $get[0]; $replace2 = $magic - $get[1]; $replace3 = $skill - $get[2]; $update = mysql_query("UPDATE `exp` SET `strength` = $replace1, `magic` = $replace2, `skill` = $replace3 WHERE `ID` = '1' "); } ?> your if is saying if the form was not submitted, try the above code... Quote Link to comment Share on other sites More sharing options...
Twister1004 Posted November 16, 2008 Author Share Posted November 16, 2008 I've tried them both, and Its still not getting anywhere. Also, Violent, The reason I had it as if (!isset($_POST['submit'])) Is because I ran into a lot of problems where it would just automatically visit the site it would just post what was on the page already. But if you left the page, it would just come out blank. I think it might be the IF statement. Quote Link to comment Share on other sites More sharing options...
Twister1004 Posted November 16, 2008 Author Share Posted November 16, 2008 I believe I was correct! I moved my code around. and I recieved a Parse error: syntax error, unexpected T_IF in C:\Documents and Settings\Compaq_Owner\Desktop\Server Files\Web Server\xampp\htdocs\fable.php on line 19 This is what I did. <?php error_reporting(E_ALL); mysql_connect("localhost", "root", "root"); mysql_select_db("fable"); ?> <html> <head> </head> <body> <?php $strength = $_POST['strength']; $magic = $_POST['magic']; $skill = $_POST['skill']; $sql = mysql_query("SELECT `strength`, `magic`, `skill`, `general` FROM `exp` WHERE `ID` = '1'"); $get = mysql_fetch_row($sql) if (isset($_POST['submit'])){ $replace1 = $strength - $get[0]; $replace2 = $magic - $get[1]; $replace3 = $skill - $get[2]; $update = mysql_query("UPDATE `exp` SET `strength` = $replace1, `magic` = $replace2, `skill` = $replace3 WHERE `ID` = '1' "); } ?> You currently need this much EXP in these stats:<br/> Strength: <?php echo $get[0] ?><br/> Skill: <?php echo $get[2] ?><br/> Magic: <?php echo $get[1] ?><br/> <form method = "POST" action=""> <table> <tr> <td>Strength: </td> <td> <input type ="text" name = "strength" value = "0" id = "strength" /></td> </tr> <tr> <td>Magic: </td> <td><input type ="text" name = "magic" value = "0" id = "magic" /></td> </tr> <tr> <td>Skill:</td> <td><input type ="text" name = "skill" value = "0" id = "skill" /></td> </tr> <tr> <td><input type = "submit" name = "submit" id ="submit" value = "Submit skill stats" /></td> </tr> </table> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
ratcateme Posted November 17, 2008 Share Posted November 17, 2008 that's because you missed the ; off the line before it put it ion and see what happens Scott. Quote Link to comment Share on other sites More sharing options...
Twister1004 Posted November 17, 2008 Author Share Posted November 17, 2008 Wow O_o, Didn't see that comming. I thought I had fixed that ._. Ok. Its fixed. No errors now, however It still isnt wanting to change the value. <?php error_reporting(E_ALL); mysql_connect("localhost", "root", "root"); mysql_select_db("fable"); ?> <html> <head> </head> <body> <?php $strength = $_POST['strength']; $magic = $_POST['magic']; $skill = $_POST['skill']; $sql = mysql_query("SELECT `strength`, `magic`, `skill`, `general` FROM `exp` WHERE `ID` = '1'"); $get = mysql_fetch_row($sql); if (!isset($_POST['submit'])){ $replace1 = $strength - $get[0]; $replace2 = $magic - $get[1]; $replace3 = $skill - $get[2]; $update = mysql_query("UPDATE `exp` SET `strength` = $replace1, `magic` = $replace2, `skill` = $replace3 WHERE `ID` = '1' "); } ?> You currently need this much EXP in these stats:<br/> Strength: <?php echo $get[0] ?><br/> Skill: <?php echo $get[2] ?><br/> Magic: <?php echo $get[1] ?><br/> <form method = "POST" action=""> <table> <tr> <td>Strength: </td> <td> <input type ="text" name = "strength" value = "0" id = "strength" /></td> </tr> <tr> <td>Magic: </td> <td><input type ="text" name = "magic" value = "0" id = "magic" /></td> </tr> <tr> <td>Skill:</td> <td><input type ="text" name = "skill" value = "0" id = "skill" /></td> </tr> <tr> <td><input type = "submit" name = "submit" id ="submit" value = "Submit skill stats" /></td> </tr> </table> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
gaza165 Posted November 17, 2008 Share Posted November 17, 2008 if (!isset($_POST['submit'])){ still need to get rid of your ! in that IF statement Quote Link to comment Share on other sites More sharing options...
Twister1004 Posted November 17, 2008 Author Share Posted November 17, 2008 I fixed the problem. It want the if (!isset($_POST['submit'])){ *however I still believe it fixed part of it?* It apparently had to do something the math part apparently O_O. Idk what but it did. *thinks to self positives before negitives this time >=O* Thanks everyone! I believe the BIG part was the fact I had it set like this, Negitive - Positive. Who knows, Thats how it worked for me finally O_o Quote Link to comment 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.