working with php arrays exported from mysql

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
pyoungson
Forum Newbie
Posts: 18
Joined: Thu Aug 09, 2007 5:35 am

working with php arrays exported from mysql

Post by pyoungson »

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.

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>
That was the simple part I guess; the solvents that have been checked are sent by the post method to the following php:

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 />";
  }

?>
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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

1. You should always debug by echoing your query.
2. Your query expands to:

Code: Select all

SELECT * FROM solvents WHERE Name='Name1, Name2, Name3'
Use IN() instead of "="
There are 10 types of people in this world, those who understand binary and those who don't
pyoungson
Forum Newbie
Posts: 18
Joined: Thu Aug 09, 2007 5:35 am

Post by pyoungson »

Thank you,

I gave that a try but still can't get the right code to get the page to show more than one checkbox input
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Code, please.
There are 10 types of people in this world, those who understand binary and those who don't
pyoungson
Forum Newbie
Posts: 18
Joined: Thu Aug 09, 2007 5:35 am

Post by pyoungson »

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 IN('{$_POST['Solvents']}')");

while($row = mysql_fetch_array($result))
  {
  echo $row['Name'] . " " . $row['Dispersive'] . " " . $row['Polar'] . " " . 

$row['Hbonding'];
  echo "<br />";
  }

?>
I still get the same result as when I used the code from the first post.
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

change..

Code: Select all

implode(', ', $_POST['Solvents']);
to..

Code: Select all

implode('\', \'', $_POST['Solvents']);
and..

Code: Select all

$result = mysql_query("SELECT * FROM solvents WHERE Name IN('{$_POST['Solvents']}')");
to..

Code: Select all

$result = mysql_query("SELECT * FROM solvents WHERE Name IN($_POST['Solvents'])");
See if that works...
pyoungson
Forum Newbie
Posts: 18
Joined: Thu Aug 09, 2007 5:35 am

Post by pyoungson »

Thank you so much, that made my day.

It didn't work when I used both of the changes you suggested only the first one; but it has worked.

I have one more question:

If I want to use the results in calculations, (e.g. the first calc I need to make is to find the largest dispersive value from the selected solvents), how would I go about this. Are the coefficients returned as an array?
Post Reply