Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts

    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.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •