working with php arrays exported from mysql
Posted: Thu Aug 09, 2007 5:48 am
I have a simple application which consists of a number of checkboxes, the user checks the solvents that they want and the following php returns the details of the solvents which are stored in a simple mysql table named solvents which consists of four columns: name, dispersive, polar and hbonding. The first is the name of the solvent which the other three are coefficients i.e. simple numbers.
That was the simple part I guess; the solvents that have been checked are sent by the post method to the following php:
What I get when I run this depends on the number of checkboxes I have checked. If I check only one then I get the desired result. i.e. the name of the solvent followed by it's coefficients. However if I check more than one then I get no mysql input just a blank space where it should be.
I thought that imploding the post would put the solvents into a useable format but I must be wrong - I would greatly appreciate any help I can get on this.
I would also like to use the coeffiecients returned for each checked solvent in a calculation - again I am a bit lost on what format they should be received from the mysql table.
Thank you for reading this, I hope you may be able to help me.
Code: Select all
<html>
<head>
<title>Solvent Calculations</title>
</head>
<body>
<form name="myform" action="process1.php" method="POST">
<input type="hidden" name="check_submit" value="1" />
<br />
Choose the solvents:
<input type="checkbox" name="Solvents[]" value="1,1-dimethylhydrazine" checked="checked" /> 1,1-dimethylhydrazine
<input type="checkbox" name="Solvents[]" value="isobutene" /> isobutene
<input type="checkbox" name="Solvents[]" value="2-ethoxyethyl acetate" /> 2-ethoxyethyl acetate
<input type="checkbox" name="Solvents[]" value="methyl tert-butyl ether" /> methyl tert-butyl ether
<br /><br />
<input type="submit" />
</form>
</body>
</head>
</html>Code: Select all
<?php
if (array_key_exists('check_submit', $_POST)) {
if ( isset($_POST['Solvents']) ) {
$_POST['Solvents'] = implode(', ', $_POST['Solvents']); //Converts an
array into a single string
}
echo "Solvents you chose: {$_POST['Solvents']}<br />";
} else {
echo "You can't see this page without submitting the form.";
}
$con = mysql_connect("localhost","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("solvents", $con);
$result = mysql_query("SELECT * FROM solvents
WHERE Name='{$_POST['Solvents']}'");
while($row = mysql_fetch_array($result))
{
echo $row['Name'] . " " . $row['Dispersive'] . " " . $row['Polar'] . " " . $row['Hbonding'];
echo "<br />";
}
?>I thought that imploding the post would put the solvents into a useable format but I must be wrong - I would greatly appreciate any help I can get on this.
I would also like to use the coeffiecients returned for each checked solvent in a calculation - again I am a bit lost on what format they should be received from the mysql table.
Thank you for reading this, I hope you may be able to help me.