Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-22-2002, 08:44 AM   PM User | #1
WA
Administrator


 
Join Date: Mar 2002
Posts: 2,596
Thanks: 2
Thanked 19 Times in 18 Posts
WA will become famous soon enough
Insert into table SET question

I have a question regarding the INSERT command, specifically, inserting sets of data. As an example:

PHP Code:
$sql="insert into thebooks set
    Bookname='$temp[0]',
    Bookimage='$temp[1]',
    Bookurl='$temp[2]'"

I noticed that the variable $temp contains only the data it was set to when the page first loaded, and in a for loop querying the above multiple times, even if I change $temp, the data that gets inserted remains that of the original $temp content.

My question are:

1) Is there a way to make the above query dynamic in the sense that variable $temp will be dynamically evalulated?
2) Also, what's the syntax to assigning a 2 dimensional array in place of $temp above? For example:

Bookname='$temp[0]["test"]',

The above doesn't work.

Thanks,
__________________
- George
- JavaScript Kit- JavaScript tutorials and 400+ scripts!
- JavaScript Reference- JavaScript reference you can relate to.
WA is offline   Reply With Quote
Old 11-24-2002, 10:56 AM   PM User | #2
Ökii
Regular Coder

 
Join Date: Jun 2002
Location: UK
Posts: 577
Thanks: 0
Thanked 0 Times in 0 Posts
Ökii is an unknown quantity at this point
Is it just me or is that a tad confusing?

As far as I have learned 'SET' is either for changing the server operation options or for use in UPDATE queries. If you are only inserting data into your database, lose the set part of the query...

$sql = "INSERT INTO `thebooks`
(Bookname,Bookimage,Bookurl) VALUES
('$temp[0]','$temp[1]','$temp[2]')";

If you wanted to overwrite an old row, use UPDATE `thebooks` SET ...... WHERE a='b'

Any chance you could give a quick idea of the array setup that you are trying to insert?
You are possibly going to be wanting to eval the value of your inserted data from an array, just would be heaps easier to help if we knew where the data is coming from and in what format it is reaching the query

a quicky eg

$temp[0] = array("TLOTR","hobbit.jpg","http:tolkien")
$temp[1] = array("CITR","holden.jpg","http:salinger")

would be easy as you could just increment the $temp[index] and attribute known second dimension indices.

PHP Code:
$temp_index = -1;
while(++
$temp_index count($temp))
   {
   
$quiz "INSERT INTO `thebooks`
(Bookname,Bookimage,Bookurl) VALUES
('$temp[$temp_index][0]','$temp[$temp_index][1]','$temp[$temp_index][2]')"
;
   } 
Last note: Your multidimensional syntax should be fine, as long as you have an index in $temp[0] named test
$temp[0] = array( test => "7", retest => "12")
__________________
Ökii - formerly pootergeist
teckis - take your time and it'll save you time.
Ökii is offline   Reply With Quote
Old 11-24-2002, 11:05 AM   PM User | #3
Ökii
Regular Coder

 
Join Date: Jun 2002
Location: UK
Posts: 577
Thanks: 0
Thanked 0 Times in 0 Posts
Ökii is an unknown quantity at this point
Subnote [after waking up a tad more] - you might find your multi-dim query would need the double quotes escaped (perhaps). Plus I sorta worked out what SET does in an insert
__________________
Ökii - formerly pootergeist
teckis - take your time and it'll save you time.
Ökii is offline   Reply With Quote
Old 11-26-2002, 11:57 AM   PM User | #4
WA
Administrator


 
Join Date: Mar 2002
Posts: 2,596
Thanks: 2
Thanked 19 Times in 18 Posts
WA will become famous soon enough
Hi Ökii:
Thanks for the interesting info. I'm still just experimenting with sql commands, so my questions are bound to be confusing. I've yet to have time to try out anything since my post above; I'll update this thread when I get to it again
__________________
- George
- JavaScript Kit- JavaScript tutorials and 400+ scripts!
- JavaScript Reference- JavaScript reference you can relate to.
WA is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:20 PM.


Advertisement
Log in to turn off these ads.