arrays and mySQL querys

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I can't get an $array[key] to be used as a select query on a mySQL database.

I have a 'parts list' which displays on a page (works fine) and is a form.
At the end of each row is a input type='text' (quantity) cell with a default  

The quantity can be altered.
At the bottom of the table (form) is a submit to basket Button

echo "<form action='basket.php' method='POST'>\n";
while ($row = mysql_fetch_array($result))
echo "<tr>\n
      <td WIDTH=10% align='middle'><FONT SIZE=-1 face=Arial>$item</td>\n
      <td WIDTH=25%><FONT SIZE=-1 face=Arial>&nbsp;$partNo</td>\n
      <td WIDTH=50%><FONT SIZE=-1 face=Arial>&nbsp;$description</td>\n";
echo "<td WIDTH=5%  align='center'>\n";
echo "<td WIDTH=10%> <input type='text' name='basket[$id]' value='0'  
echo "</tr>\n";
      <input type='submit' value='Add to basket'></form></td>\n";

The table z350 contains columns headed

    id (unique identifier numerical)

When some values have been entered in the quantity I submit.


The 'basket' page displays.  I have used the

echo "<pre>";
print_r ($_POST['basket']);
echo "</pre>";

To display the contents of the posted array.
It is shown below how it prints out. The Key refers to a line entry (id) in  
the database.
The right hand entry is a quantity input by the user on the previous page.

    [63] => 5
    [64] => 0
    [65] => 6
    [66] => 25
    [67] => 0

The basket page needs to pull these requsted rows (id) from the database.

I have tried various combinations of MySQL queries but can't seem to get the  
data into a $query.

  $query = "SELECT * FROM z350 WHERE id=\"\"";
  $result = mysql_query($query)
       or die ("Couldn't execute query.");

I beleive the state of play with the above is that the key is not being  
looked at, its the value.
So how do I make the key into the values that will be used in the SQL query.

Ultimately my desire is to keep the Array in a session which will keep the  
keys and as the users adds the 'id' and
the value (quantity ordered), but on request to view the basket the Key will  
be used to get the partNo and description to display.
in a table (I feel confident I can do that part).

Thanks in antcipation


Re: arrays and mySQL querys

Quoted text here. Click to load it

The form is fine, your array $basket is fine, the problem is your use
of $basket in the SQL statement. In the context of this statement what
does mean? Not what you think it means...

Depending on your requirements you can either fetch all the rows at
once or process each row individually.

All at once:

   $query = 'SELECT * FROM z350 WHERE id IN ( ' . join( ',', $basket )
. ')';
   // process...

One at a time:

   foreach( $basket AS $id )
      $query = "SELECT * FROM z350 WHERE id = $id";
      // process...

Re: arrays and mySQL querys

Thanks very much for that guys.  I'd never have worked that out easily.  
This is my first foray into any form of programming and started a few months  
ago with PHP and MySQL for dummies, it was a good easy start, but even with  
a few library books open on PHP and MySQL I have struggled with this (array)  
interaction between php and mySQL.

Did you learn by just reading books or did you go on a course or study on  
the net?

Is there any easy reference ie a book that gives an explaination (giving  
hypothetical situations) of  problems/queries and the code in laymans terms  
for the php/mysql interaction.
I find the php and mysql reference assumes I know more of the basics, so  
they are quite a hard read.

Thanks once again.

Quoted text here. Click to load it

Re: arrays and mySQL querys

Stephen Preston wrote:
Quoted text here. Click to load it


Quoted text here. Click to load it

foreach($basket as $id=>$val){
    $query = "SELECT * FROM z350 WHERE id=''";


Justin Koivisto, ZCE -

Site Timeline