SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Paris
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamic Insert Problem

    I am trying to dynamically insert values into my table and cannot get it to work. I know that the form variables are being passed over correctly because I echo the output. So I'm guessing that it is somewhere in my insert statement. I don't get any errors and the page loads correctly, it's just that the table is still empty.

    Here is my code:
    PHP Code:
     $i 0;
    foreach(
    $_POST['cat_id'] as $key => $value)
    {
            
    $iMonth 0;
            
    $iMonthEnd 12;
            while (
    $iMonthEnd >= $iMonth) {
                
    $result mysql_query("INSERT INTO bu_tblforecast VALUES ('', '{$_POST[cat_id][$i]}', '$iMonth', '2004', '{$_POST[amount][$i]}', '1')");
    //the echo to test form variables
                
    echo "INSERT INTO bu_tblforecast VALUES ('', '{$_POST[cat_id][$i]}', '$iMonth', '2004', '{$_POST[amount][$i]}', '1')<br>";
                
    $iMonth++;
            }
        
    $i++;
        } 
    Also my table structure:

    CREATE TABLE `bu_tblforecast` (
    `forecast_id` int(11) NOT NULL auto_increment,
    `cat_id` int(11) NOT NULL default '0',
    `month` int(11) NOT NULL default '0',
    `year` int(11) NOT NULL default '2004',
    `amount` float NOT NULL default '0',
    `budget` int(11) NOT NULL default '0',
    PRIMARY KEY (`forecast_id`)
    ) TYPE=MyISAM AUTO_INCREMENT=1 ;

    Any ideas?

  2. #2
    SitePoint Addict sinapra's Avatar
    Join Date
    Sep 2004
    Location
    secunderabad
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, did you just try to print the insert statement and try to run it in your backend, i mean datamanager. Which database are you using?
    Regards

    Its Great to work and Be Merry!!!!!!!!

  3. #3
    SitePoint Addict rokc's Avatar
    Join Date
    Oct 2004
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you also have to pass the connection string in the mysql_query()

    like,

    $result = mysql_query("INSERT INTO bu_tblforecast VALUES ('', '{$_POST[cat_id][$i]}', '$iMonth', '2004', '{$_POST[amount][$i]}', '1')",$constring);

    where $constring is the connection string.
    hope this works

  4. #4
    SitePoint Addict sinapra's Avatar
    Join Date
    Sep 2004
    Location
    secunderabad
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    rok is right, i missed to see that, add the connection string else it would do the action, still it should have shown some error.
    Regards

    Its Great to work and Be Merry!!!!!!!!

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Paris
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah, thanks rokc, that was it. i figured it was something simple, but i just wasn't seeing it.


Bookmarks

Posting Permissions

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