insert data into mysql table

Discussion in 'PHP' started by sssaudddahmed, Apr 18, 2006.

  1. #1
    i have created a test ecommerce website where people can order either single mp3 tracks or an whole album.

    The problem that i have got is that when a customer wants to order the whole album i want the each track from the album to be inserted into a order table in mysql.


    in the mysql database i have:

    tracks table // contains tracks of albums - foriegn key = albumid // contains album id
    album table // contains albums - primary key = albumid
    order table // contains customer id, tracks name, album id etc

    Once the customers hits the buy album buttons they are sent to the orderprocess.php page with a url paramter id = 'albumid'

    Within the orderprocess.php i do this code:

    $id = $_get["id"];
    $dum = date("D dS M,Y h:i a");
    $_SESSION["id"] = "1";
    $id = $_SESSION["id"];

    $host = "localhost";
    $username="root";
    $password="waterwater";
    $database="p36311";

    mysql_connect($host,$username,$password);
    @mysql_select_db($database) or die( "Can't retrieve database.");
    $query="SELECT * FROM tracks WHERE albumid = '$id' ";
    $result=mysql_query($query);

    $num=mysql_numrows($result);

    mysql_close();
    $i=0;

    while ( $i <= $num) {

    $line = mysql_fetch_array($result);
    $Trackid = $line[1];
    $Albumid = $line[6];
    $Cost = $line[7];

    mysql_connect($host,$username,$password);
    @mysql_select_db($database) or die( "Can't retrieve database.");
    $query="INSERT INTO order (cusid, trackid, albumid, cost, orderdate) values
    ('$id','$Trackid','$Albumid','$dum')";
    mysql_query($query);
    $i=$i+1;
    }

    }

    Basically i want to retrieve all tracks that belong to the url parameter (albumid). I then want to insert each track indivdually into the orders table.

    The above page gets processed but no data is entered into the database.

    Can someone please help me.
     
    sssaudddahmed, Apr 18, 2006 IP
  2. mnemtsas

    mnemtsas Super Dud

    Messages:
    497
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What data types are those four fields in the order table? Also, you have five fields in your insert statement (cusid, trackid, albumid, cost, orderdate) and are inserting only 4 values ('$id','$Trackid','$Albumid','$dum')
     
    mnemtsas, Apr 18, 2006 IP
  3. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #3
    A liberal sprinkling of calls like:
    print mysql_error();

    would definitely help out here. Try a few of those and see if the MySQL server itself can tell you the problem...
     
    TwistMyArm, Apr 18, 2006 IP
  4. hansi

    hansi Peon

    Messages:
    129
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $query="INSERT INTO order (cusid, trackid, albumid, cost, orderdate) values
    ('$id','$Trackid','$Albumid','$dum')";
    Code (markup):
    5 cols and only 4 values, maybe this is the error.

    *edit: sorry mnemtsas didn't see your response befor, shame on me.
     
    hansi, Apr 18, 2006 IP
  5. sssaudddahmed

    sssaudddahmed Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    cheers for the responses,

    you guys are right it was a problem with the sql line. Fixed it now.

    How come the simplest problems are the hardest to see??????


    thanks.
     
    sssaudddahmed, Apr 18, 2006 IP
  6. mnemtsas

    mnemtsas Super Dud

    Messages:
    497
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Sometimes a good nights sleep will help you find simple errors that you missed for hours the day before ..... well done finding the problem!
     
    mnemtsas, Apr 19, 2006 IP
  7. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #7
    lol, agreed completely! It seems that you try every response and every possible way, and the next thing you notice is that you take a simple rest and vualla! It comes to you the next day like it was nothing. Sleeping tends to waste time inbetween, but if the answer cannot be found, it is well worth it!
     
    drewbe121212, Apr 19, 2006 IP