Jump to content

Recommended Posts

Sorry for two basic questions in one day but as I'm writing this I find myself at a loss as to how I am supposed to detect this form of input.

 

This is the page you start on:

<html>
<body>

<?php
$con = mysql_connect("127.0.0.1","xxxx","xxxxxxxxxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("train_db1", $con);

$result = mysql_query("SELECT * FROM Persons");

echo "<table border='1'>
<tr>
<th>Strength</th>
<th>Defense</th>
<th>Speed</th>
</tr>";

$row = mysql_fetch_array($result);
echo "<tr>";
echo "<td>" . $row['str'] . "</td>";
echo "<td>" . $row['def'] . "</td>";
echo "<td>" . $row['spd'] . "</td>";
echo "</tr>";


echo "<tr>
<td><form method='post' action='train.php'><input type='submit' name='full_strength' value='All Strength' action='train.php'></form></td>
<td><form method='post' action='train.php'><input type='submit' name='full_defense' value='All Defense' action='train.php'></form></td>
<td><form method='post' action='train.php'><input type='submit' name='full_speed' value='All Speed' action='train.php'></form></td></tr>";

echo "</table>";

?>

</body>
</html>

 

And this is the part that it will redirect you to once you click a button so it can "train" the stat you choose. What I am having a problem is how I'm trying to detect this type of input.  I was experimenting and was trying to figure out how I would detect this. Right now its training and adding to the database but no matter what button you push it only adds to the strength stat.

<html>
<body>


<?php
$doACTION = $_REQUEST['full_strength'];
if (!$doACTION)
{
$doACTION = $_REQUEST['full_defense'];
if (!$doACTION)
	{
	$doACTION = $_REQUEST['full_speed'];
	}
}
$train = rand(1,20);

$con = mysql_connect("127.0.0.1","xxxx","xxxxxxxxxxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("train_db1", $con);
$result = mysql_query("SELECT * FROM Persons");
$row = mysql_fetch_array($result);

if ($doACTION = 'full_strength')
{
$train = $train + $row['str'];
mysql_query("UPDATE Persons SET str ='$train'
WHERE id='1'");
}
elseif ($doACTION='full_defense')
{
$train = $train + $row['def'];
mysql_query("UPDATE Persons SET def ='$train'
WHERE id='1'");
}

elseif ($doACTION='full_speed')
{
$train = $train + $row['spd'];
mysql_query("UPDATE Persons SET spd ='$train'
WHERE id='1'");
}
mysql_close($con);
header( 'Location:read.php' ) ;
?>

</body>
</html>

 

Any help or insight on the problem would be much appreciated.

the problem is here

if ($doACTION = 'full_strength')

 

you are doing an assignment there, and because you assign $doACTION to a non empty string, the assignment returns a value that equates to true. You want to do a comparison (==).

if ($doACTION == 'full_strength')

 

same idea with the subsequent if statements

 

 

btw, if you want to check if something is set, instead of doing something like

$var = $_REQUEST['var']
if (!$var)
...

 

you can do

if (isset($_REQUEST['var'])){
$var = $_REQUEST['var'];
}

Well I tried what you said.  It's not doing anything now.

 

<html>
<body>


<?php

//$doACTION = $_REQUEST['full_strength'];
//if (!$doACTION)
//	{
$doACTION = $_REQUEST['full_defense'];
//	if (!$doACTION)
//		{
//		$doACTION = $_REQUEST['full_speed'];
//		}
//	}

if (isset($_REQUEST['full_strength']))
{
$doACTION = $_REQUEST['full_strength'];
}

if (isset($_REQUEST['full_defense']))
{
$doACTION = $_REQUEST['full_defense'];
}

if (isset($_REQUEST['full_speed']))
{
$doACTION = $_REQUEST['full_speed'];
}



$train = rand(1,20);

$con = mysql_connect("127.0.0.1","xxxx","xxxxxxxxxxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("train_db1", $con);
$result = mysql_query("SELECT * FROM Persons");
$row = mysql_fetch_array($result);

if ($doACTION == 'full_strength')
{
$train = $train + $row['str'];
mysql_query("UPDATE Persons SET str ='$train'
WHERE id='1'");
}
elseif ($doACTION=='full_defense')
{
$train = $train + $row['def'];
mysql_query("UPDATE Persons SET def ='$train'
WHERE id='1'");
}

elseif ($doACTION=='full_speed')
{
$train = $train + $row['spd'];
mysql_query("UPDATE Persons SET spd ='$train'
WHERE id='1'");
}
mysql_close($con);
header( 'Location:read.php' ) ;
?>

</body>
</html>

try echoing $doACTION before the if statements to see what it has. If its empty, try doing a print_r on $_REQUEST. if that doesn't work for some reason, try using $_POST instead of $_REQUEST (since your forms send the data through $_POST)

I changed this:

if (isset($_REQUEST['full_strength']))
   {
   $doACTION = $_REQUEST['full_strength'];
   }

if (isset($_REQUEST['full_defense']))
   {
   $doACTION = $_REQUEST['full_defense'];
   }

if (isset($_REQUEST['full_speed']))
   {
   $doACTION = $_REQUEST['full_speed'];
   }

 

 

To this:

 

if (isset($_REQUEST['full_strength']))
   {
   $doACTION = 1;
   }

if (isset($_REQUEST['full_defense']))
   {
   $doACTION = 2;
   }

if (isset($_REQUEST['full_speed']))
   {
   $doACTION = 3;
   }

 

And then changed the if statements at the bottom and now it works perfectly. Thanks for the help, that isset will help me in the future I'm sure  ;)

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.