473,799 Members | 3,111 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

arrays and mySQL querys

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
0

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_arr ay($result))
extract($row);
echo "<tr>\n
<td WIDTH=10% align='middle'> <FONT SIZE=-1 face=Arial>$ite m</td>\n
<td WIDTH=25%><FONT SIZE=-1 face=Arial>&nbs p;$partNo</td>\n
<td WIDTH=50%><FONT SIZE=-1 face=Arial>&nbs p;$description</td>\n";
echo "<td WIDTH=5% align='center'> \n";
echo "<td WIDTH=10%> <input type='text' name='basket[$id]' value='0'
size='2'></td>\n";
echo "</tr>\n";
<tr>
<input type='submit' value='Add to basket'></form></td>\n";
</tr>

The table z350 contains columns headed

id (unique identifier numerical)
item
partNo
description

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.

Array
(
[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=\"{$basket[id]}\"";
$result = mysql_query($qu ery)
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

Stephen
Dec 5 '05 #1
3 1801
I can't get an $array[key] to be used as a select query on a mySQL database. 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=\"{$basket[id]}\"";
$result = mysql_query($qu ery)
or die ("Couldn't execute query.");


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 {$basket[id]} 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...
}

---
Steve

Dec 5 '05 #2
Stephen Preston wrote:
I can't get an $array[key] to be used as a select query on a mySQL database.
<sni
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.

Array
(
[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=\"{$basket[id]}\"";
$result = mysql_query($qu ery)
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.


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

HTH

--
Justin Koivisto, ZCE - ju****@koivi.co m
http://koivi.com
Dec 5 '05 #3
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.
"Steve" <go********@nas tysoft.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
I can't get an $array[key] to be used as a select query on a mySQL
database.

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=\"{$basket[id]}\"";
$result = mysql_query($qu ery)
or die ("Couldn't execute query.");


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 {$basket[id]} 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...
}

---
Steve

Dec 6 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1570
by: leegold2 | last post by:
Appreciate any help- I want to make a 1st DB query, then I want to feed those results into a second query - the results of the 2nd query are then my output. I know it's possible w/a temporary table of the 1st....but I think there's a cleaner way. I tried nested SQL w/MYSQL 4.1 but it hasn't worked. So maybe w/2 sucessive querys w/PHP?? Thanks. How can I in PHP take a first query eg: mysql> SELECT balloon_txt.access_no FROM...
0
3493
by: Marek Lewczuk | last post by:
Hello group, For everyone who thinks about moving from MySQL to PostgreSQL I have a realy bad news - It's not worth. Why, You may ask... A few days ago I have installed and tested PostgreSQL, becouse I realy need UTF-8 support and subselects. I thought that PostgreSQL will be as good as MySQL but also will give me that features, which aren't availble in MySQL. Well, after installation and moving my MySQL dbs into PostgreSQL I decided to...
2
2542
by: Brian | last post by:
Hello I am using version 4.0.12-nt of MySQL and when I hit the enter key rapidly I can't connect to the database. The result is a message is returned to me from mysql that says I can't connect because the database is down. If I only hit the 'enter' key once (to execute the page that my queries are on(their are four queries on this particular web page)) my queries execute just fine. We have noticed that this only happens when we:
6
22537
by: Matt Liverance | last post by:
I REALLY dont want to switch to oracle :( but I cant get these tables working any faster. I've got 2 dedicated servers, each with a slave, all run 32gig 15k rpm raid 5 on u320 perc raid cards, dell 2600/4600's with single channel backplanes (new ones will have dual channel) All have 2 gig of ram, but I've never seen mysql use more than 300mb of ram.
3
2272
by: Marek Lewczuk | last post by:
Hello, I have changed DB from MySQL to PostgreSQL. When I have run my application on PostgreSQL it was disaster - it was much slower than MySQL... I have tried to change PG configuration file etc.. no luck. After many long days of thinking what is wrong I have made several tests with "EXPLAIN" statement, and to my amusement there was many SeqScan - MySQL didn't show that things. I have made some changes in PG db structure (new indexes...
1
1781
by: Coney Coxwell | last post by:
Hopefully, someone will take the time to answer this. I have MYSQL back-end data source that I would like to link MSAccess to. My goal is to use Access to query specific pieces of information from the MYSQL database. I have downloaded the ODBC driver from MYSQL and managed to link Access to the MYSQL tables once. But, I cannot create querys to obtain any information. I think I am missing something. I am not a computer whiz, although I am...
0
1643
by: Daniel Crespo | last post by:
Hi to all, I'm using adodb for accessing mysql and postgres. My problem relies on the mysql access. Sometimes, when I try to execute a query (using ExecTrans method below), I get this error: 'NoneType' object has no attribute 'cursor'
1
1345
by: David | last post by:
Hi All, I'm having trouble accessing multiple databases using a single user. Basically, I have 2 databases, db1 and db2 and 2 users, user1 and user2. Originally these were setup so that user1 would only access db1 and user2 would only access db2. However, now I want user1 to access both db1 & db2. I've used cPanel to declare user1 as a user of db2. However, whenever I try to access db2 from user1, I get permission problems.
3
1699
by: brock797 | last post by:
hey im just starting to learn php and mysql, i am using CentOS 5 and have all the neccisary programs compiled correctly (apache, php, mysql, etc) i can use mysql from the command line with no flaws, however when i try to connect to mysql from php i get nothing, no errors no text at all just a blank page...i have a mysql_connect.php file and the php file i have created to access and change database variables, theres supposed to be an input box...
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9546
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10243
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9078
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5590
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.