Jump to content

Form retrieved from a MySQL database disappears upon submit: Resource id#3, 4


DVigneault

Recommended Posts

Hey--New to the forum, and php.

 

I have a drop-down list populated by a MySQL database.  Submitting that selection retrieves php scripts which I run using eval().  One of them is a form.  The scripts evaluate fine, but when I submit the second form, it disappears and I get a resource id where the "result" script was.  The resource ids change between 3 and 4 when I refresh the page.

 

Any hints on what I'm doing wrong?  Here is the main script:

 

<?php

$host="localhost";
$username="root";
$password="root";
$con = mysql_connect($host,$username,$password);

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("calculators", $con);

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

while($row = mysql_fetch_array($result))
  {
echo "<option type='text' " . "value='" . $row['Title'] . "'>" . $row['Title'] . "</option>";
  }

echo "</select>";
echo "<br>";
echo "<input type='submit' name='submit1'>";
echo "</form>";

$title = mysql_real_escape_string($_GET['Title']);

$result = mysql_query('SELECT * FROM calculator WHERE Title = "' . $title . '"');

while($row = mysql_fetch_array($result))
  {
$header = $row['Header'];
$calculator = $row['Calculator'];
$result = $row['Result'];
$formula = $row['Formula'];
$reference = $row['Reference'];
  }

eval('?>' . $header . '<?php ');
echo "<h2>Calculator</h2>";
eval('?>' . $calculator . '<?php ');
echo "<h2>Result</h2>";
eval('?>' . $result . '<?php ');
echo "<h2>Formula</h2>";
eval('?>' . $formula . '<?php ');
echo "<h2>Reference</h2>";
eval('?>' . $reference . '<?php ');

mysql_close($con);

?>

 

Thanks,

 

Davis

I've written a bunch of calculators in php (BMI, age, things like that).  I've stored the scripts in a MySQL database and am using eval to run the scripts before putting them on the site.  Before I used eval, I just got the php as is.  I'd like the user to select a calculator from the drop-down list, and then have the calculator form appear below.  The calculator appears just fine, but disappears when I try to use it.

Here's an example of what would be in the database.  It calculates anion gap.  (I'm a med student, not a programmer, unfortunately.  I'm writing this as a kind of educational tool and also because I'd like to learn more about php).

 

Header:

<?php

function calculate($sodium,$potassium,$chloride,$bicarbonate)
{
echo ($sodium+$potassium)-($chloride+$bicarbonate);
}

?>

 

Calculator:

<form name="form" method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend>Blood Pressures:</legend>

<label for="sodium">Sodium: </label>
<input type="text" name="sodium" value="<?=$_GET['sodium'] ?>"><br>

<label for="potassium">Potassium: </label>
<input type="text" name="potassium" value="<?=$_GET['potassium'] ?>"><br>

<label for="chloride">Chloride: </label>
<input type="text" name="chloride" value="<?=$_GET['chloride'] ?>"><br>

<label for="bicarbonate">Bicarbonate: </label>
<input type="text" name="bicarbonate" value="<?=$_GET['bicarbonate'] ?>"><br>

<input type="submit" name="submit">

</fieldset>
</form>

 

Result:

<?php

echo "Anion Gap: ";
calculate($_GET["sodium"],$_GET["potassium"],$_GET["chloride"],$_GET["bicarbonate"]);

?>

I think the problem is conflict with 2 different $result's. You set $result to the result of the sql query, but in the loop over $result, you re-set $result to another value, overwriting the original $result. I suggest that you change the name of the $result variable within the loop and in the eval()

BlueSkyIS: Thanks a bunch for the reply.

 

It actually ended up having to do with the fact that the variable submitted with the first form was erased when the second form was submitted.  I just (three minutes ago) got it working by adding a hidden input in the second form that resends the variable from the first form.

 

I really appreciate your responses, though.  Makes a newbie feel welcome.  :-)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.