Jump to content

[SOLVED] A problem in either both MySQL or PHP? =P


Twister1004

Recommended Posts

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 :P).

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 :)

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

<?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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.